问题描述
我有一个 Nginx 反向代理,将 /docs
下的所有内容代理到另一个 Nginx 实例(在 /
上)。这个其他实例响应 DNS 名称 wiki-pages
。两个实例都在 Docker 中运行。当我像 proxy_pass http://wiki-pages:80/;
这样对 DNS 名称进行硬编码时,它工作得很好。
Nginx: v1.21.0
,构建详述如下。
解决方法:我在 location
块中执行 DNS 解析,因此即使 wiki-pages
站点关闭,反向代理也可以启动。
阻塞问题
当我将 proxy_pass
与硬编码的 DNS 名称 (wiki-pages
) 一起使用时,它运行良好。当我使用 set $target http://wiki-pages:80/;
和 proxy_pass $target;
时,所有查询的文件都返回 /index.html
的内容(在 wiki-pages
上),带有 Content-Type: text/html
。状态代码:200
。
完整的服务器块
server {
listen 80;
# listen [::]:80;
server_name www.mysite.com;
# Redirect everything to HTTPS
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
server_name www.mysite.com;
root /usr/share/Nginx/html;
ssl_certificate /ssl/live/www.mysite.com/cert.pem;
ssl_certificate_key /ssl/live/www.mysite.com/privkey.pem;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
#access_log /var/log/Nginx/reverse-access.log;
#error_log /var/log/Nginx/reverse-error.log;
location / {
}
location /docs {
# Add trailing / when needed
rewrite ^([^.]*[^/])$ $1/ permanent;
resolver 127.0.0.11;
set $target http://wiki-pages:80/;
proxy_pass $target;
}
}
我试过了:
location /docs {
rewrite ^([^.]*[^/])$ $1/ permanent;
resolver 127.0.0.11;
set $target wiki-pages;
proxy_pass http://$target:80/;
}
以下块完美运行(无变量)
location /docs {
rewrite ^([^.]*[^/])$ $1/ permanent;
proxy_pass http://wiki-pages:80/;
}
我还尝试了一系列代理标头更改,使用尾随 /
(在 location
参数中,在 proxy_pass
uri 中),使用 $uri
,{ {1}} 等。同样的行为,所有查询都返回 $request_uri
的内容。
有趣的是,如果我在我的 /index.html
站点根目录中创建符号链接 docs -> ./
,以下块按预期工作:
wiki-pages
但是,我想避免创建此符号链接。
我现在不知道该尝试什么。 location /docs {
# Add trailing / when needed
rewrite ^([^.]*[^/])$ $1/ permanent;
resolver 127.0.0.11;
set $target http://wiki-pages:80/;
proxy_pass $target$request_uri;
}
没有显示任何有趣的内容,只有 Nginx-debug
响应,但所有查询都返回相同的内容...
我真的不明白 200
是如何工作的,以及为什么将硬编码值 proxy_pass
替换为具有相同 硬编码值 abc
的变量会发生变化行为。
问题:
如果您需要更多信息,请告诉我。
Nginx 版本/构建
proxy_pass
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)