我使用 nginx 反向代理多个 dockerized 应用程序,效果不错。但我无法让 portainer 工作。
我的 nginx 配置包括:
location ^~ /apps/portainer {
proxy_http_version 1.1;
proxy_set_header Connection "";
set $upstream portainer:9000;
proxy_pass http://$upstream;
}
页面加载不正确(css、js无法加载)。nginx错误日志:
[错误] open()“/var/www/html/apps/main.11b0c8b84d24581.js”失败(2:没有此文件或目录),请求:“GET /apps/main.11b0c8b84d24581.js HTTP/2.0”
我不明白为什么 nginx 尝试提供静态文件 - 所有内容都应该代理到 portainer。(尽管无关紧要,但路径是错误的)。
有没有不涉及正则表达式的常规修复方法?顺便说一句,如果您不介意在位置块中使用正则表达式,@EchoMike444 在下面给出的答案非常好。
答案1
通过遵循配置 https://portainer.readthedocs.io/en/stable/faq.html,我能够连接到http://127.0.0.1/apps/portainer/
你缺少管理的部分网络套接字連接
我default.conf
的ngnix
upstream portainer {
server portainer:9000;
}
server {
listen 80;
location /apps/portainer/ {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://portainer/;
}
location /apps/portainer/api/websocket/ {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_pass http://portainer/api/websocket/;
}
}
我的docker-compose.yaml
version: '3.7'
services:
portainer:
image: portainer/portainer
restart: always
command: -H tcp://10.10.0.1:2375
ports:
- target: 8000
published: 8000
protocol: tcp
- target: 9000
published: 9000
protocol: tcp
volumes:
- portainer_data:/data
nginx:
image: nginx
volumes:
- ${PWD}/default.conf:/etc/nginx/conf.d/default.conf:ro
command: ["/bin/sh","-c","exec nginx -g 'daemon off;'"]
restart: always
ports:
- target: 80
published: 80
protocol: tcp
mode: host
volumes:
portainer_data:
评论后更新
因为nginx在启动时进行dns解析,要使用变量,所以就default.conf
变成这样。
resolver 127.0.0.11 valid=30s;
resolver_timeout 5s;
server {
listen 80;
location ~* ^(/apps/portainer)(/api/websocket/.*)$ {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
set $upstream http://portainer:9000$2;
proxy_pass $upstream ;
}
location ~* ^(/apps/portainer)(/.*)$ {
proxy_http_version 1.1;
proxy_set_header Connection "";
set $upstream http://portainer:9000$2;
proxy_pass $upstream;
}
}