laravel - 使用反向代理Nginx时出现502错误的网关

标签 laravel docker nginx

在过去的几天中,我一直在尝试使用docker和nginx设置反向代理,但收效甚微。当我运行docker-compose up并尝试通过我的IP地址访问我的服务器时,我确实获得了相应的页面。但是,当尝试 curl 在/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 conf文件
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;
}

运行docker network ls显示docker-container文件中列出的服务正在其分配的端口上运行,令人困惑的是,服务的Web部分显示它正在侦听80以及8080或8081,具体取决于该项目。我不确定这是否引起冲突。

我还尝试利用nginx-proxy项目获得类似的结果。任何帮助将非常感激。

最佳答案

因此问题最终通过使用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"

关于laravel - 使用反向代理Nginx时出现502错误的网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49596137/

相关文章:

bash - 商店软件 6 : How to set boolean config values with Docker exec in Git Bash

Nginx 代理通过重写和正则表达式

angular - 如何将 "ng serve"用于通过 nginx 反向代理托管的多个应用程序?

powershell - docker rmi : Error response from daemon: invalid reference format: repository name must be lowercase

laravel - 我做了订单管理系统,如何在laravel中合计价格?

laravel - 我如何在 lumen 5.2 上使用 Illuminate\Session\Middleware\StartSession::class

javascript - 日期选择器和 Laravel 5

directory - dokku - 我的应用程序目录在哪里

redirect - nginx http 到 https 导致重定向过多

php - 实时服务器上的 Laravel Coinbase API Http 异常错误但适用于 Xampp