我正在嘗試在 Docker 中構建我的 Angular 網站,這也可以,但是每當網站發出請求時,URL 就會從 http://url_to_api 更改為 http://localhost:8080/url_to_api。
我對 NGINX 和 docker 非常陌生,并且嘗試了許多不同的配置,但都沒有奏效。
我也在瀏覽器中收到錯誤:
Failed to load resource: net::ERR_CONNECTION_REFUSED /assets/js/env.js
nginx會阻止它嗎?
這是我的基本 nginx.conf
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
}
}
這里是 docker 檔案
#stage 1
FROM node:12-alpine as build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build --prod
#stage 2
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist/webapp /usr/share/nginx/html
EXPOSE 80
#To provide environment variables in angular
CMD ["/bin/sh", "-c", "envsubst < /usr/share/nginx/html/assets/js/env.template.js > /usr/share/nginx/html/assets/js/env.js && exec nginx -g 'daemon off;'"]
uj5u.com熱心網友回復:
# Create image based on the official Node 10 image from dockerhub
FROM node:10
# Create a directory where our app will be placed
RUN mkdir -p /app
# Change directory so that our commands run inside this new directory
WORKDIR /app
# Copy dependency definitions
COPY package*.json /app/
# Install dependencies
RUN npm install
# Get all the code needed to run the app
COPY . /app/
# Expose the port the app runs in
EXPOSE 4200
# Serve the app
CMD ["npm", "start"]
檢查此處的步驟https://www.digitalocean.com/community/tutorials/create-a-mean-app-with-angular-2-and-docker-compose 為 angular2 客戶端學習和構建 docker
uj5u.com熱心網友回復:
現在發現了我的錯誤。組態檔沒問題,但是犯了一個非常愚蠢的錯誤,我在 docker 檔案末尾的腳本交換了 API URL,但我忘記在 URL 的開頭指定 HTTP,所以 Nginx 請求了 localhost 上的 URL。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/422867.html
標籤:
