gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:38:51 25 4
gpt4 key购买 nike

在过去的几天中,我一直在尝试使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com