NGINX:将URI路径转换为查询字符串

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

我的目标是将我的Nginx服务器的所有非/请求都转换为/?data={uri-path}

我正在尝试以下配置:

server {
        server_name example.com;
        listen 80;

        location = / {
                proxy_set_header   X-Forwarded-For $remote_addr;
                proxy_set_header   Host $http_host;

                alias /home/....;
                expires $expires;
        }

        location / {
                return 301 http://example.com/?data=$request_uri;
        }
}

问题是我以这样的URI结尾:http://example.com/?data=?data=?data=?data=?data=?data=

浏览器抱怨很多重定向。

根据文档,location =应该有优先权,但似乎我做错了。

感谢您的反馈。

nginx nginx-location
1个回答
0
投票

取决于location = /的作用。

[如果它指向一个目录并且该目录包含index.html文件,则Nginx将在内部重写URI,然后搜索location以处理该修改后的请求。这将导致重定向循环。

您可以通过添加另一个location块来处理最终URI来打破循环,例如:

location = /index.html { root ...; }

如果location = /块旨在代理请求,则您缺少proxy_pass语句。

© www.soinside.com 2019 - 2024. All rights reserved.