【问题标题】:How to rewrtite in nginx URI with a Question Mark如何用问号重写 nginx URI
【发布时间】:2020-01-20 23:55:31
【问题描述】:

我正在尝试使用 nginx 重写带有问号的 URI? 我怎样才能避开问号?

如果我使用没有“?”的测试 URI,则重写有效。 但是有一个我得到了 404

    listen 80 default_server;
    listen [::]:80 default_server;

        root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name example.com;

    #rewrite [^\/]+\/get\.php\?username=([^\&]+)&password=([^\&]+).*$ http://$host:8080/server/get_user_videolist?username=$1:password=$2 permanent;

    rewrite ^/get.phpusername=TEST123&password=SECRET123 http://$host:8080/server/get_user_chlist?username=TEST123:password=SECRET123;

    rewrite ^/get.php?Fusername=TEST123&password=SECRET123 http://$host:8080/server/get_user_chlist?username=TEST123:password=SECRET123;

}

我的预期输出应该从第一个 URL 到第二个 URL

http://example.com/get.php?username=TEST123&password=SECRET123

->

http://example.com:8080/server/get_user_chlist?username=TEST123:password=Secret123

错误日志

2019/09/19 22:12:15 [error] 7059#7059: *183 open() "/var/www/html/get.php" failed (2: No such file or directory), client: *.*.*.*, server: example.com, request: "GET /get.php?username=TEST123&password=SECRET123 HTTP/1.1", host: "example.com"

【问题讨论】:

标签: regex nginx url-rewriting webserver


【解决方案1】:

我自己解决了 http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

location / {
if ($args ~* "/?username=([a-zA-Z0-9]+)&password=([a-zA-Z0-9]+)") {
        set $username $1;
        set $password $2;
        rewrite ^ http://$host:8080/server/get_user_chlist?username=$username:password=$password? last;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-30
    • 2019-05-26
    • 2018-08-27
    • 2017-04-07
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多