Nginx代理上游服务器Docker Compose-拒绝502错误的网关连接

时间:2019-01-06 11:42:53

标签: docker nginx docker-compose reverse-proxy jwilder-nginx-proxy

我正在尝试使用Docker Compose容器化与我的Web应用程序相关的所有内容,包括Nginx和SSL证书。为此,我正在使用Nginx Proxy image from JWilderLetsEncrypt Companion,但是在使Nginx代理正常工作时遇到了麻烦,结果最终是:

  

Nginx 502错误的网关

     

[错误] 31160#0:* 35连接到上游时,connect()失败(111:连接被拒绝),客户端:xx.xx.xx.xx,服务器:domain.com,请求:“ GET / dev / HTTP / 1.1”,上游:“ webapp://127.0.0.1:8080”,主机:“ domain.com”

这仅在尝试设置Nginx代理和SSL证书时发生,因此我知道这是这两个容器中一个或两个的配置问题。如果有人能发现我要去哪里错了,我将万分感谢!

以下是有问题的容器:

version: '3.1'

networks:
  mywebapp:

services:
  nginx-proxy:
    container_name: nginx-proxy
    build: ./env/nginx-proxy
    networks:
      - mywebapp
    ports:
      - 80:80
      - 443:443
    env_file:
      - ./env/nginx-proxy/.env
    depends_on:
      - webapp
    tty: true
    volumes:
      - ./src:/home/www/mywebapp/src
      - ./storage:/home/www/storage/mywebapp
      - ./data/nginx-proxy/logs:/var/log/nginx
      - ./env/nginx-proxy/webserver/nginx.conf:/etc/nginx/nginx.conf
      - ./env/nginx-proxy/webserver/conf.d:/etc/nginx/conf.d
      - ./env/nginx-proxy/webserver/vhost.d:/etc/nginx/vhost.d
      - ./env/nginx-proxy/webserver/defaults:/etc/nginx/defaults
      - ./env/nginx-proxy/webserver/global:/etc/nginx/global
      - ./env/nginx-proxy/ssl/certs:/etc/nginx/certs
      - ./env/nginx-proxy/share:/usr/share/nginx/html
      - /var/run/docker.sock:/tmp/docker.sock:ro
    labels:
      com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy: "true"

  letsencrypt:
    restart: always
    container_name: letsencrypt
    image: jrcs/letsencrypt-nginx-proxy-companion
    env_file:
      - ./env/letsencrypt/.env
    volumes:
      - ./data/letsencrypt/logs:/var/log
      - ./env/nginx-proxy/webserver/nginx.conf:/etc/nginx/nginx.conf
      - ./env/nginx-proxy/webserver/conf.d:/etc/nginx/conf.d
      - ./env/nginx-proxy/webserver/vhost.d:/etc/nginx/vhost.d
      - ./env/nginx-proxy/webserver/defaults:/etc/nginx/defaults
      - ./env/nginx-proxy/webserver/global:/etc/nginx/global
      - ./env/nginx-proxy/ssl/certs:/etc/nginx/certs
      - ./env/nginx-proxy/share:/usr/share/nginx/html
      - /var/run/docker.sock:/var/run/docker.sock:ro
    depends_on:
      - nginx-proxy

  webapp:
    container_name: webapp
    build: ./env/webapp
    hostname: webapp
    networks:
      - mywebapp
    ports:
      - 9000:9000
      - 8080:8080
    volumes:
      - ./env/composer:/home/www/.composer
      - ./env/global/bashrc:/home/www/.bashrc
      - ./data/bash/.bash_history:/home/www/.bash_history
      - ~/.ssh:/home/www/.ssh:ro
      - ~/.gitconfig:/home/www/.gitconfig:ro
      - ./storage:/home/www/storage/mywebapp
      - ./src:/home/www/mywebapp/src

要点

  • Webapp是我的Web应用程序的源代码,该应用程序正在运行PHP,MySQL和Nginx Web服务器。 webapp网络服务器公开并在端口8080上进行监听,以提供PHP文件。
  • Nginx代理暴露标准端口443和80 ,并且代理通过端口8080传递到webapp
  • LetsEncrypt随播生成证书并续订。

Nginx代理服务器配置:

upstream webapp {
    server 127.0.0.1:8080;
}

server {

    listen 80;
    listen [::]:80;

    server_name webapp.localhost;

    location / {
        proxy_pass http://webapp;
    }
}

server {

    # SSL configuration
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl_certificate certs/default.crt;
    ssl_certificate_key certs/default.key;

    include /etc/nginx/global/ssl-params.conf;

    server_name webapp.localhost;

    location / {
        proxy_pass http://webapp;
    }
}

Webapp Web服务器配置:

server {
    listen 8080;
    listen [::]:8080;

    server_name webapp.localhost;

    root /home/www/webapp/src;
    index index.php;

    include /etc/nginx/defaults/php.conf;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
}

访问http://webapp.localhost:8080时,我可以看到webapp Web服务器为该页面提供服务没有问题,因此我怀疑Nginx代理服务器配置有问题。

感谢阅读。

1 个答案:

答案 0 :(得分:2)

由于nginx和webapp位于两个单独的容器上,因此nginx无法通过为上游配置的本地主机(127.0.0.1)上的webapp:

upstream webapp {
    server 127.0.0.1:8080;
}

将其更改为webapp:8080