【问题标题】:How to make Nginx in a web dyno to find a node app in a worker dyno?如何在网络测功机中制作 Nginx 以在工作测功机中找到节点应用程序?
【发布时间】: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


    【解决方案1】:

    默认情况下,测功机是隔离的,无法相互访问。此外,工人测功机不打算被其他测功机直接访问。只有网络测功机可以接收来自路由器的流量。

    要像这样在您的测功机之间进行路由,您需要使用private spaces 和单独的应用程序。

    请参阅Networking documentation 和Runtime documentation,了解有关测功机之间联网的更多信息。

    如果您想支持类似于docker-compose 或 Docker Swarm 的接口,其中每个容器自动联网在一起,请参阅this answer,其中解释了一种解决方法。

    【讨论】:

      猜你喜欢
      • 2015-08-31
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      • 2012-01-15
      • 2015-04-17
      • 2013-04-26
      • 2017-03-12
      相关资源
      最近更新 更多