【发布时间】:2022-01-20 18:21:48
【问题描述】:
我正在尝试使用 Nginx 作为反向代理(两个容器/dynos)在 Heroku 中部署一个 Angular 应用程序。
Web 容器 (Nginx) 因找不到工作容器(带 SSR 的 Node/Express)而崩溃:
顺便说一句,我还在为管道使用 dockerfiles 和 Gitlab CI/CD。
我收到以下错误:
2021-12-17T18:23:30.617790+00:00 heroku[web.1]: Process exited with status 1
2021-12-17T18:23:30.696851+00:00 heroku[web.1]: State changed from starting to crashed
2021-12-17T18:23:30.704189+00:00 heroku[web.1]: State changed from crashed to starting
2021-12-17T18:23:30.486129+00:00 app[web.1]: 2021/12/17 18:23:30 [emerg] 3#3: host not found in upstream "worker" in /etc/nginx/nginx.conf:9
2021-12-17T18:23:30.486148+00:00 app[web.1]: nginx: [emerg] host not found in upstream "worker" in /etc/nginx/nginx.conf:9
我的配置是:
gitlab-ci.yml
build:
image: trion/ng-cli:11.2.12
stage: build
script:
- npm install
- npm audit fix
- npm run-script build:ssr
artifacts:
paths:
- dist
only:
refs:
- tags
- master
deploy:
image: docker:20.10.7
stage: deploy
services:
- docker:20.10.7-dind
before_script:
# Install Heroku CLI
- apk update
- apk add bash
- apk add curl
- apk add --update nodejs npm
- curl https://cli-assets.heroku.com/install.sh | sh
- export HEROKU_API_KEY=$HEROKU_API_KEY
script:
- heroku container:login
- heroku container:push --recursive --app=$HEROKU_APP_PRODUCTION
- heroku container:release web worker --app=$HEROKU_APP_PRODUCTION
dependencies:
- build
only:
refs:
- tags
- master
Dockerfile.worker
FROM node:14-alpine3.12
WORKDIR /usr/src/app/
RUN mkdir -p /usr/src/app/dist/usermetrics/browser
COPY dist/usermetrics/browser/index.html /usr/src/app/dist/usermetrics/browser/index.html
COPY dist/usermetrics/server /usr/src/app
EXPOSE $PORT
CMD node main.js
Dockerfile.web
FROM nginx:1.17.8-alpine
COPY nginx.conf /etc/nginx/nginx.conf
WORKDIR /usr/share/nginx/html/
COPY dist/usermetrics/browser .
nginx.conf
worker_processes auto;
events {
worker_connections 8192;
}
http {
upstream nodeserver_upstream {
server worker; <---- here!
}
server {
listen ${PORT};
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;
# compression
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# # angular index.html location
location / {
try_files $uri $uri/ /index.html;
}
# # potential reverse proxy for sending api calls
location / {
proxy_set_header Host $host;
proxy_set_header Content-Type application/json;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://nodeserver_upstream/;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
管道完成时没有任何错误,我可以看到 dynos: web 和 worker up,但在日志中显示了该错误。
我错过了什么吗?如何正确设置 nginx.conf 以将流量重定向到其他测功机中的 Express 服务器?
【问题讨论】:
标签: docker nginx heroku devops gitlab-ci