GET変数で指定されたURLへのNginxプロキシリクエスト

GET変数で指定されたURLへのNginxプロキシリクエスト

次のようなリクエストにNginxが応答する必要があります

https://example.org/proxy/?url=https://othersite.com/path/file.php%%a=test123%%b=321tset

または同様の方法、例えば

https://example.org/proxy/https://othersite.com/path/file.php?a=test123&b=321tset

リクエストをプロキシすることで

https://othersite.com/path/file.php?a=test123&b=321tset

書き直しや別のルールでこれを行う方法はありますか? ご協力いただければ幸いです。よろしくお願いします。

答え1

SE へようこそ。これらのリダイレクトはステートメントを使用して実行できますifが、nginx プロキシを使用して試したことはありません。

必要に応じてリダイレクトするには、ifNginxブロックに次のブロックを追加します。server

server {
    ...
    ...

    #For https://example.org/proxy/?url=https://othersite.com/path/file.php%%a=test123%%b=321tset

    if ($request_uri ~ ^/proxy/\?url=(.*)$) {
        return 301 $1;
    }

    #For https://example.org/proxy/https://othersite.com/path/file.php?a=test123&b=321tset

    if ($request_uri ~ ^/proxy/(https\:\/\/(.*))$) {
        return 301 $1;
    }
}

アップデート:

locationブロックを使用する代わりにプロキシを使用することができますif

location ~ /proxy/\?url=(.*)$ {
    proxy_pass $1;
    proxy_set_header Host $host;
}

location ~ /proxy/https\:\/\/(.*)$ {
    proxy_pass $1;
    proxy_set_header Host $host;
}

関連情報