我有一个基于 docker 容器(在 boot2docker 中)构建的环境。我有以下 docker-compose.yml 文件来快速设置 nginx 和 nexus 服务器:
version: '3.2'
services:
nexus:
image: stefanprodan/nexus
container_name: nexus
ports:
- 8081:8081
- 5000:5000
nginx:
image: nginx:latest
container_name: nginx
ports:
- 5043:443
volumes:
- /opt/dm/nginx2/nginx.conf:/etc/nginx/nginx.conf:ro
Nginx 有以下配置(nginx.conf)
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
proxy_send_timeout 120;
proxy_read_timeout 300;
proxy_buffering off;
keepalive_timeout 5 5;
tcp_nodelay on;
server {
listen 80;
server_name demo.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name demo.com;
# allow large uploads of files - refer to nginx documentation
client_max_body_size 1024m;
# optimize downloading files larger than 1G - refer to nginx doc before adjusting
#proxy_max_temp_file_size 2048m
#ssl on;
#ssl_certificate /etc/nginx/ssl.crt;
#ssl_certificate_key /etc/nginx/ssl.key;
location / {
proxy_pass http://nexus:8081/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto "https";
}
}
}
Nexus 似乎运行良好。我调用成功curl http://localhost:8081
在 docker 主机上。这将返回 Nexus 登录站点的 html。现在我想尝试一下 nginx 服务器。它被配置为监听 443 端口,但 SSL 现在已禁用(我想在深入了解 SSL 配置之前对其进行测试)。正如您所注意到的,我的 ngix 容器将端口 443 映射到端口 5043。因此,我尝试使用以下curl 命令:curl -v http://localhost:5043/
。现在我希望我的 http 请求将发送到 nginx 并代理到 proxy_pass http://nexus:8081/;
关系。 Nexus 主机名在 docker 容器网络中可见,并且可以从 nginx 容器访问。不幸的是,我收到的回复是:
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5043 (#0)
> GET / HTTP/1.1
> Host: localhost:5043
> User-Agent: curl/7.49.1
> Accept: */*
>
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server
我正在检查 nginx 日志、错误、访问,但这些日志是空的。有人可以帮我解决这个问题吗?它应该只是代理请求的一个简单示例,但也许我误解了一些概念?
最佳答案
您的 nginx
conf
中是否有 upstream
指令(位于 http
指令内)?
upstream nexus {
server <Nexus_IP>:<Nexus_Port>;
}
只有这样nginx
才能正确解析。 docker-compose
服务名称 nexus
不会在运行时注入(inject)到 nginx
容器中。
您可以在 docker-compose 中尝试链接
:
https://docs.docker.com/compose/compose-file/#links
这将为您的 /etc/hosts
中的链接容器提供一个别名
。 但您仍然需要 更新:如果可以解析,您也可以直接在 upstream 指令
。nginx 指令中使用名称
就像位置
。
https://serverfault.com/questions/577370/how-can-i-use-environment-variables-in-nginx-conf
关于docker - Nginx 作为 Nexus 的反向代理服务器 - 无法在 docker 环境中连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46908099/