我用python写了这样的代码:
from urllib.parse import quote_plus
file_name = quote_plus(filename)
我的最终
url
是这样的http://example.com/name+is+oscar
在服务器上我存储了空间“名字是奥斯卡”
我的网络服务器在
nginx
,我想在用户点击url
时返回文件,如何在nginx
中用空格替换“+”?!
我看到重写可以提供帮助,但我不知道如何使用。
虽然 Nginx 不支持仅通过配置直接将 URI 中的“+”替换为空格,但您可以通过前端控制器路由请求并在应用程序代码中执行必要的替换:
首先将所有请求重定向到单个前端控制器(例如,
index.py
):
location / {
try_files $uri $uri/ /index.py?$query_string;
}
index.py
中的第二个,在处理之前手动将
file_name
参数中的“+”替换为空格:
from urllib.parse import unquote_plus
import os
# Assuming you have access to the original QUERY_STRING environment variable
query_string = os.environ.get('QUERY_STRING', '')
# Decoding plus signs to spaces happens here
decoded_query = unquote_plus(query_string)
# Then, you would parse 'decoded_query' to get your parameters and values