先介绍一下背景。我正在将网站从使用 Apache 的旧服务器迁移到使用 NGINX 代理的 Apache 新 Ubuntu 服务器。将有一个过渡期,在此期间网站代码库将在新旧服务器上同时运行。
该网站的搜索 URL 带有过滤器,这些过滤器以斜线分隔,通常是可选的,例如
www.example.com/search/deals/q1/q2/q3/q4/q5/q6/
它映射到以下 apache.conf 重写规则:
RewriteRule ^/search/deals/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/ /results.php?q1=$1&q2=$2&q3=$3&q4=$4&q5=$5&q6=$6 [L,QSA]
类似下面的 URL 并不罕见
www.example.com/search/deals/q1////q5/q6/
www.example.com/search/deals/q1/q2/q3///q6/
www.example.com/search/deals/q1/q2/q3/q4///
在新服务器上,我已像这样配置 NGINX:两个站点启用了默认服务器 apache 文件和 example.com 文件
/etc/nginx/sites-enabled/apache -> ../sites-available/apache
/etc/nginx/sites-enabled/example.com -> ../sites-available/example.com
apache 看起来像这样(真实 IP 替换为 10.10.10.10):
server {
listen 10.10.10.10:80 default_server;
merge_slashes off; #have tried with/without
location / {
proxy_redirect off; #have tried with/without
port_in_redirect off; #have tried with/without
proxy_pass http://10.10.10.10:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
example.com 看起来像这样
server {
listen 10.10.10.10:80 ;
server_name example.com www.example.com;
root /var/www/live/example.com/frontend/htdocs;
merge_slashes off;
location / {
fastcgi_pass unix:/var/run/php5-fpm.sock; #have tried with/without
include fastcgi_params; #have tried with/without
proxy_redirect off; #have tried with/without
port_in_redirect off; #have tried with/without
proxy_pass http://10.10.10.10:8080 ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#have tried with/without
proxy_set_header X-Accel-Internal /internal-nginx-static-location; #have tried with/without
access_log off;
}
}
做出任何更改后,我已通过以下方式重新启动 NGINX
service nginx restart
当我加载页面(例如 www.example.com/search/deals/q1/q2////q6/)时,出现“未找到文件”提示,然后查看日志级别设置为 3 的 Apache 日志,得到以下内容:
[Wed Oct 07 22:52:10.178436 2015] [rewrite:trace1] [pid 4186:tid 123456789] mod_rewrite.c(468): [client 10.10.10.10:33468] 10.10.10.10 - - [www.example.com/sid#sddsaddsa][rid#sddsaddsa/subreq] pass through /search/deals/q1/q2/q5/
这表明所有多个斜杠在某个时候都已通过代理删除。但我需要 URL 保持完整,以便 Apache 规则可以正确路由参数。
我查看了具有类似标题的其他答案,但没有一个能解决我的问题,例如: 与乘客合作时保留双斜线
https://stackoverflow.com/questions/4320774/nginx-how-to-keep-double-slashes-in-urls
https://stackoverflow.com/questions/14832780/nginx-merge-slashes-redirect
https://stackoverflow.com/questions/22759345/nginx-trailing-slash-in-proxy-pass-url
https://stackoverflow.com/questions/5834025/how-to-preserve-request-url-with-nginx-proxy-pass
...
如果有人能有任何建议或者能给我指明正确的方向那就太好了?
提前致谢