我有以下设置
- 主服务器 - 调用它
https://master.com
- 从属服务器 - 调用它
https://slave.com
两者都在 Ubuntu 16.04 上运行 Nginx
在主服务器上,我在我的目录中创建了以下配置块/etc/nginx/sites-available/default
file
location /test
{
rewrite ^/test(.*) /$1 break;
proxy_pass https://slave.com;
proxy_read_timeout 240;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
A service nginx reload
稍后的master.com
我可以执行以下操作
- 浏览至
https://master.com/test
并查看输出slave.com\index.php
.
- 浏览至
https://master.com/test/test.txt
并查看文件中的文本slave.com\test.txt
- 浏览至
https://master/com/test/test.jpg
并查看文件中的图像slave.com\test.jpg
.
但是,我无法执行以下任何操作
- 浏览至
https://master.com/test/test.php
而不是向我显示输出https://slave.com/test.php
显示 404 错误消息
- 浏览至
https://master.com/test/adminer/adminer.php
它没有向我显示从站上管理员实例的登录屏幕,https://slave.com/adminer/adminer.php
显示管理员实例的登录屏幕master.com
i.e. https://master.com/adminer/adminer.php
这显然是因为我的 Nginx 配置中缺少一些内容master.com
。但是,我看不出那可能是什么。
为了完整起见,这是我在两台服务器上的配置:
Ubuntu- 16.04.3Nginx- 1.10.3PHP- 7.0.22
我应该解释一下为什么^~
是必需的,因为从我原来的问题来看这并不清楚。我有另一个块设置来处理 PHP 脚本master.com
.
location ~ \.php$
{
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
由于 Nginx 处理这些指令的方式,该块在处理时具有优先权.php
文件和master.com
最终在本地寻找.php
实际运行的脚本slave.com
。避免这种情况的唯一方法是使用^~
你的做法是错误的。在处理的块内/test
你重写它并将其发送出块。这proxy_pass
实际上从未发生过,因为新的 URL 没有/test
在里面。解决办法很简单,不要使用rewrite
location /test/
{
proxy_pass https://slave.com/;
proxy_read_timeout 240;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
追加/
在位置路径的末尾以及proxy_pass
服务器将确定之后的内容/test/
已发送至您的proxy_pass
address
Edit-1
这是我在发布此答案之前设置的示例测试用例。
events {
worker_connections 1024;
}
http {
server {
listen 80;
location /test1 {
proxy_pass http://127.0.0.1:81;
}
location /test2 {
proxy_pass http://127.0.0.1:81/;
}
location /test3/ {
proxy_pass http://127.0.0.1:81;
}
location /test4/ {
proxy_pass http://127.0.0.1:81/;
}
}
server {
listen 81;
location / {
echo "$request_uri";
}
}
}
现在结果解释了所有 4 个位置块之间的差异
$ curl http://192.168.33.100/test1/abc/test
/test1/abc/test
$ curl http://192.168.33.100/test2/abc/test
//abc/test
$ curl http://192.168.33.100/test3/abc/test
/test3/abc/test
$ curl http://192.168.33.100/test4/abc/test
/abc/test
正如你所看到的/test4
代理服务器只能看到的url/abc/test
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)