variables - nginx - 以变量为主机名的 proxy_pass 将所有查询指向 index.html
问题描述
我有一个 nginx 反向代理将所有内容代理/docs
到另一个 nginx 实例(on /
)。这个其他实例响应 DNS name wiki-pages
。两个实例都在 Docker 中运行。当我对 DNS 名称进行硬编码时proxy_pass http://wiki-pages:80/;
,它可以完美运行。
nginx: v1.21.0
,详细构建如下。
初始问题:不可解析的 DNS 名称阻止 nginx 启动。
解决方法:我在我的location
块中进行了 DNS 解析,因此即使wiki-pages
站点关闭,反向代理也可以启动。
阻塞问题
当我使用proxy_pass
硬编码的 DNS 名称 ( wiki-pages
) 时,它可以完美运行。当我使用set $target http://wiki-pages:80/;
andproxy_pass $target;
时,所有查询的文件都返回/index.html
(on wiki-pages
), with的内容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
,$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
的变量替换硬编码值会改变行为。abc
问题:
- 如何完成我想做的事情?例如:
- 即使我的后端 DNS 名称不可解析,也让我的反向代理启动
- 将
proxy_pass
变量用作目标,并获得与硬编码值相同的行为
- 为什么
proxy_pass
会有这样的行为?
如果您需要更多信息,请告诉我。
nginx 版本/构建
root@reverse-proxy:/# nginx -V
nginx version: nginx/1.21.0
built by gcc 8.3.0 (Debian 8.3.0-6)
built with OpenSSL 1.1.1d 10 Sep 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.21.0/debian/debuild-base/nginx-1.21.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
root@reverse-proxy:/# nginx-debug -V
nginx version: nginx/1.21.0
built by gcc 8.3.0 (Debian 8.3.0-6)
built with OpenSSL 1.1.1d 10 Sep 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.21.0/debian/debuild-base/nginx-1.21.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --with-debug
解决方案
推荐阅读
- sapb1 - 从 1 个表中提取值,即使它存在于另外 5 个连接表中
- android - 如何获取按钮以显示地图活动
- javascript - 如何从 Javascript 的下拉列表中选择对象的键
- python - 当字典的值为列表时如何过滤字典
- python - 任何人都可以解释我制作的这个 discord.py 程序中的错误吗?如何在我的 Python discord 机器人代码中找到错误?
- json - Flutter中如何以列表的形式显示复杂的json数据
- ldap - 在 Keycloak 中为联合用户启用 OTP
- javascript - 如何检测一些兄弟组件是否存在于 React 中?
- json - 按子键对 JSON 对象进行排序
- vue.js - 自定义搜索和过滤功能 VueJS