Nginx 代理請求到 GET 變數中指定的 url

Nginx 代理請求到 GET 變數中指定的 url

我需要 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 代理程式。

將以下if區塊新增到您的 Nginxserver區塊中以根據需要重新導向它們

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;
}

相關內容