在 Nginx 端将 python quote_plus 创建的 url 中的“+”替换为空格

问题描述 投票:0回答:1

我用python写了这样的代码:

from urllib.parse import quote_plus

file_name = quote_plus(filename)

我的最终

url
是这样的http://example.com/name+is+oscar

在服务器上我存储了空间“名字是奥斯卡”

我的网络服务器在

nginx
,我想在用户点击
url
时返回文件,如何在
nginx
中用空格替换“+”?!

我看到重写可以提供帮助,但我不知道如何使用。

python nginx urllib
1个回答
0
投票

虽然 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
    
© www.soinside.com 2019 - 2024. All rights reserved.