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

관련 정보