首页 > 解决方案 > 在 nginx+uwsgi 中运行时,django url 模式会去除正斜杠

问题描述

对于以下 url 模式:

re_path(r'proxy/(?P<url>.*)', myview)

当我发送proxy/http://www.google.com

myview 函数接收 url 为http:/www.google.com(使用 single /

它发生在 uwsgi+nginx setup ,当使用 runserver url is 运行时http://www.google.com

标签: djangonginx

解决方案


这是因为nginx会自动将 URL 中的双斜杠合并为一个:

http://nginx.org/en/docs/http/ngx_http_core_module.html#merge_slashes

启用或禁用将 URI 中的两个或多个相邻斜杠压缩为单个斜杠。

请注意,压缩对于正确匹配前缀字符串和正则表达式位置至关重要。没有它,“//scripts/one.php”请求将不匹配

你应该在你的禁用它nginx.conf

merge_slashes off;

推荐阅读