使用Nginx的Dockerized应用程序(通过docker-compose)-502错误的网关错误

时间:2019-03-20 04:07:15

标签: docker nginx docker-compose phoenix-framework

我有一个Phoenix应用程序MyApp,我正在尝试对它进行Dockerize并在Ubuntu 18.04上进行部署。我尝试遵循一些指南来正确设置Nginx,但是我会收到502 Bad Gateway错误或导致过多的重定向,具体取决于我尝试设置Nginx反向代理的方式。

理想的情况是我可以使用docker-compose up,它将初始化Web应用程序和Nginx。这是我当前的docker-compose.yml文件:

docker-compose.yml

version: '3.5'

services:
  nginx:
    image: nginx:latest
    restart: unless-stopped
    volumes:
      - ./data/nginx/app.conf:/etc/nginx/nginx.conf
      - ./data/nginx/error.log:/etc/nginx/error_log.log
      - ./data/nginx/cache/:/etc/nginx/cache
      - /etc/letsencrypt/:/etc/letsencrypt/
    environment:
      - ENV=production
      - APPLICATION_URL=http://myapp.domain
    ports:
      - 80:80
      - 443:443

  ...

  web:
    depends_on:
      - nginx
    image: "myapp:0.1.0"
    restart: unless-stopped
    expose:
      - "80"
    env_file:
      - config/docker.env

不包括Nginx,与ports: - 80:4000相同的myapp映像版本也可以与events { } http { client_max_body_size 20m; proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m; server { proxy_cache one; listen 80; listen 443 ssl; server_name myapp.domain; location / { proxy_pass http://localhost:4000; rewrite ^/myapp.domain(.*)$ $1 break; } ssl_certificate /etc/letsencrypt/live/myapp.domain/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/myapp.domain/privkey.pem; include /etc/letsencrypt/options-ssl-nginx.conf; } } 一起使用,因为Phoenix配置为期望端口4000上的流量。以下是我相信的nginx应用程序配置,由docker-中的卷指定- compose.yml。

data / nginx / app.conf

docker-compose up

我不完全知道如何设置端口,以便通过Nginx正确发送Web流量,然后反向代理到myapp。我相信本地主机可以工作,因为当我运行Running MyAppWeb.Endpoint with cowboy 2.6.1 at http://localhost:4000时,我看到[error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 162.158.119.89, server: myapp.domain, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4000/", host: "myapp.domain"

当我尝试访问该站点时,我在服务器日志中看到了这一点 PosOrderPayment

任何建议都值得赞赏。

1 个答案:

答案 0 :(得分:0)

您需要在nginx容器上链接Web服务,docker-compose如下所示:

services:
  nginx:
    image: nginx:latest
    restart: unless-stopped
    volumes:
      - ./data/nginx/app.conf:/etc/nginx/nginx.conf
      - ./data/nginx/error.log:/etc/nginx/error_log.log
      - ./data/nginx/cache/:/etc/nginx/cache
      - /etc/letsencrypt/:/etc/letsencrypt/
    environment:
      - ENV=production
      - APPLICATION_URL=http://myapp.domain
    ports:
      - 80:80
      - 443:443
    links:
      - web

  web:
      depends_on:
        - nginx
      image: "myapp:0.1.0"
      restart: unless-stopped
      expose:
        - "4000"
      env_file:
        - config/docker.env

然后更新data/nginx/app.conf

events {

}

http {
  client_max_body_size 20m;

  proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m;

  server {
      proxy_cache one;
      listen 80;
      listen 443 ssl;
      server_name myapp.domain;

      location / {
          proxy_pass http://web:4000;
          rewrite ^/myapp.domain(.*)$ $1 break;
      }

      ssl_certificate /etc/letsencrypt/live/myapp.domain/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/myapp.domain/privkey.pem;
      include /etc/letsencrypt/options-ssl-nginx.conf;
  }
}