过去几天我一直在尝试使用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;
}
运行docker network ls
表明docker-container文件中列出的服务正在其分配的端口上运行,有一点令人困惑的是,服务的Web部分显示它正在监听80如8080或8081,取决于项目。我不确定这是否会导致冲突。
我也尝试将nginx-proxy项目用于类似的结果。任何帮助将不胜感激。
答案 0 :(得分:0)
因此,问题最终通过使用来自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"