【问题标题】:502 Bad Gateway when using reverse proxy nginx使用反向代理 nginx 时出现 502 Bad Gateway
【发布时间】:2018-04-02 01:01:39
【问题描述】:

过去几天我一直在尝试使用 docker 和 nginx 设置反向代理,但收效甚微。当我运行docker-compose up 并尝试通过我的IP 地址访问我的服务器时,我确实得到了相应的页面。但是,当尝试卷曲我的/etc/hosts 文件中列出的域时,它只返回一个502 Bad Gateway。我的主要项目是一系列 laravel 项目,我使用以下 docker-compose.yml 文件。

version: '2'
services:
    web:
        build:
            context: ./
            dockerfile: docker/web.docker           
        volumes:
            - ./:/var/www
        expose:
            - "8080"
        links:
            - app
    app:
        build:
            context: ./
            dockerfile: docker/app.docker
        volumes:
            - ./:/var/www
        links:
            - mysql
            - redis
            - beanstalk
            - cache
        environment:
            - "DB_PORT=3307"
            - "DB_HOST=mysql"
            - "REDIS_PORT=6379"
            - "REDIS_HOST=redis"
    mysql:
        image: mysql:5.7.18
        environment:
            - "MYSQL_ROOT_PASSWORD=secret"
            - "MYSQL_DATABASE=db"
        ports:
            - "3307:3307"
    redis:
        image: redis:3.0
        ports:
            - "6379:6379"
    beanstalk:
        image: schickling/beanstalkd
        ports:
            -  "11300:11300"
    cache:
        image: memcached:alpine
        ports:
            - "11211:11211"

使用以下 nginx 配置文件

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;

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

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

我的代理的docker-compose.yml 目前是:

version: '3'
services:
  proxy:
    build: ./
    networks:
      - project1
      - project2
    ports:
      - 80:80

networks:
  project1:
    external:
      name: projec1_default
  project2:
    external:
      name: project2_default

我还有一个 nginx conf 文件,我在其中以以下方式定义所有项目

server {
  listen 80;
  server_name project1.xxx.tech www.project1.xxx.tech;

  location / {
    include /etc/nginx/includes/proxy.conf;
    proxy_pass http://project1_web_1:8080/;
  }

  access_log off;
  error_log  /var/log/nginx/error.log error;
}

running docker network ls 表明 docker-container 文件中列出的服务正在其分配的端口上运行,一件相当令人困惑的事情是服务的 web 部分显示它正在侦听 80 和 8080或 8081,具体取决于项目。我不确定这是否会导致冲突。

我也尝试过使用 nginx-proxy 项目来获得类似的结果。任何帮助将不胜感激。

【问题讨论】:

  • 我认为proxy_pass http://project1_web_1:8080/;应该是proxy_pass http://web:8080/;,如果它在容器内运行的话
  • 包含该 nginx conf 文件的容器是我的反向代理,因此这两个容器是完全独立的。不过我可以试一试——这似乎不起作用
  • 外部 nginx 容器是在 docker-compose 创建的同一网络上启动还是在默认桥接网络上启动?
  • 我在启动 nginx 容器时没有指定任何内容,但是当我用docker-compose down 停止它时,它确实说我的网络是外部的,并且进程正在跳过这些。所以我假设 nginx 容器对于实际看到 laravel 项目没有问题
  • 你能发布你用来启动外部nginx容器的命令吗?

标签: laravel docker nginx


【解决方案1】:

所以问题最终通过使用 jwilder 的 nginx-proxy 项目解决了

我只需要指定虚拟主机和虚拟端口即可。我不确定为什么虚拟端口必须明确设置为 80,但这似乎解决了问题。这是我的 laravel 项目的 docker-compose.yml 文件:

version: '2'
services:
    web:
        build:
            context: ./
            dockerfile: docker/web.docker           
        volumes:
            - ./:/var/www
        links:
            - app
        environment: 
            - VIRTUAL_HOST=appname.com
            - VIRTUAL_PORT=80
        expose:
            - "80"
    app:
        build:
            context: ./
            dockerfile: docker/app.docker
        volumes:
            - ./:/var/www
        links:
            - mysql
        environment:
            - "DB_PORT=3307"
            - "DB_HOST=mysql"
            - "REDIS_PORT=6379"
            - "REDIS_HOST=redis"
    mysql:
        image: mysql:5.7.18
        environment:
            - "MYSQL_ROOT_PASSWORD=secret"
            - "MYSQL_DATABASE=db"
        ports:
            - "3307:3307"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-29
    • 2018-04-15
    • 2021-05-07
    • 2020-10-24
    • 2018-05-23
    • 2019-07-09
    • 2017-12-30
    • 2015-11-02
    相关资源
    最近更新 更多