nginx: How to keep double slashes in urls

2019-02-17 00:50发布

I have web service which takes several filter parameters, something like :

http://mydomain.com/filter1/value1/filter2/value2/filter3/value3

The tricky thing is sometimes some of the filter variables are absent, so urls as such could be passed to this service:

http://mydomain.com/filter1//filter2//filter3/value3

Now I need to configure my nginx (or fastcgi) to keep the double slashes. Currently it's replacing double slashes to single ones. I'm new to nginx & fastcgi configuration and I don't know how to do that. I captured the request_uri from my php script when I requested the second url, and I got

http://mydomain.com/filter1/filter2/filter3/value3

Plz help me. Thanks in advance.

标签: nginx
4条回答
孤傲高冷的网名
2楼-- · 2019-02-17 01:20

I had a similar issue with nginx+passenger (for Ruby on Rails / Rack / etc.), and I confirm that by default, multiple slashes are collapsed (in both PATH_INFO and REQUEST_URI).

Adding merge_slashes off; in the server context of the nginx configuration fixed it (I now see multiple slashes alright).

查看更多
爷的心禁止访问
3楼-- · 2019-02-17 01:27

Also, make sure you don't have a trailing slash in the proxy_pass line!

  • GOOD: proxy_pass http://localhost:3000;

  • BAD: proxy_pass http://localhost:3000/;

查看更多
姐就是有狂的资本
4楼-- · 2019-02-17 01:28

As per previous answers, the solution is to use merge_slashes off; in your Nginx config. However, if you use virtual hosts, then it looks like you need to include it in your first host. That is the case on v1.4.1 and v1.4.6.

查看更多
看我几分像从前
5楼-- · 2019-02-17 01:31
syntax: merge_slashes [on|off]
default: merge_slashes on
context: http, server

You must use: merge_slashes off;

查看更多
登录 后发表回答