NGINX: Proxy_pass 지시어를 사용하여 업스트림 서버 매핑을 사용할 수 있나요?

NGINX: Proxy_pass 지시어를 사용하여 업스트림 서버 매핑을 사용할 수 있나요?

여러 백엔드 IIS 서버에 대한 역방향 프록시 역할을 하는 NGINX 서버가 있습니다. 지정된 URI에 따라 들어오는 각 HTTPS 요청을 특정 백엔드(업스트림) 서버에 전달하고 싶습니다. 동시에 주소 표시줄에 동일한 URL을 유지합니다(백엔드 서버 이름 또는 주소가 사용자에게 보이지 않도록 유지하기 위해).

예를 들어
주소 표시줄 요청은 다음과 같습니다.
https://test.blahblah.com/url_a
실제로는 -->로 이동
https://upstreamserver_a/url_a
하지만 주소 표시줄에는 여전히 다음과 같습니다.
https://test.blahblah.com/url_a

지금까지 내 nginx.conf는 다음과 같습니다.

    ## Upstreamserver_A backend for test.blahblah.com
    upstream upstreamserver_a {
            server 10.10.1.12:80; #upstreamserver_a.blahblah.com
    }

    map_hash_bucket_size 128;
    map_hash_max_size 2048;


    # URI to Server Map
    map $1 $upstream_host {
            default         whatever;
            url_a       upstreamserver_a;
    }


    ## OUR HTTP SERVER AT PORT 80
    server  {
            listen      80;
            server_name test.blahblah.com;
            index       index.html;
            root        /usr/share/nginx/html;

            ## redirect http to https ##
            proxy_redirect http:// https://;
    }


    server  {
            listen      443 ssl;
            server_name test.blahblah.com;
            #root        /usr/share/nginx/html;
        ### ssl config - customize as per your setup ###
            ssl_certificate      ssl/blahblah.com/blahblah.pem;
            ssl_certificate_key  ssl/blahblah.com/blahblah.key;
            ssl_protocols        SSLv3 TLSv1 TLSv1.1 TLSv1.2;
            ssl_ciphers RC4:HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers on;
            keepalive_timeout    70;
            ssl_session_cache    shared:SSL:10m;
           ssl_session_timeout  10m;


        ## PROXY backend
            location ~ ^/(.*)$ {
                    rewrite ^/([^/]+)$ https://test.blahblah.com/webapps        /Application.html?firm=$1#login break;

                    proxy_pass http://$upstream_host$uri;
            }
     }

물론, 하나의 서버가 작동하게 되면 upstreamserver_b, upstreamserver_c 등과 같은 여러 업스트림 서버를 추가할 것입니다.
SSL 부분에 대해서는 도움이 필요하지 않으며 매핑 및/또는 변수 참조만 있으면 됩니다. 나는 다음 줄을 which works 로
대체하여 SSL이 작동한다는 것을 증명했습니다 . 일명, URL 'test.blahblah.com' 아래에 위장된 URL을 유지하면서 upstreamserver_a로 리디렉션됩니다.
proxy_pass http://$upstream_host$uri;

proxy_pass http://upstreamserver_a;

어떤 도움이라도 정말 감사하겠습니다!
나는 내가 필요로 하는 것과 매우 가까운 답을 가진 많은 예를 찾았지만, 그것을 내 특별한 필요에 맞게 만들기에는 너무 어리석었습니다!

답변1

정규식으로 URI 패턴을 설명하고 해당 위치에서 이를 사용하여 rewrite 또는 Proxy_pass 대상에서 사용할 수 있는 부분을 캡처해야 합니다. 예:

location ~ ^/url_(.).*$ {
  #this will capture your /url_a or /url_b URI's 
  #and will store the "a" or "b" into $1 variable
  #which you can use to proxy the request to a specific server
  proxy_pass http://$1.$upstreams$uri;
  #the above will result in a.<upstreams>/url_a for your example.
  #don't forget the resolver configuration for domain names.
}

도움이 되길 바라요

답변2

원하는 것이 정확히 무엇인지에 따라 마음속에 떠오르는 몇 가지 지시문이 있습니다.

예를 들어 기본적으로 nginx가 인터넷 이름을 완전히 인식하지 못하는 다양한 업스트림 서버에 모든 것을 프록시하고 내부 이름과 외부 이름을 매핑하려는 경우 다음과 같이 해야 합니다.

map $host $host_upstream_mapped {
    hostnames;

    test.example.su  test_iis.internal:8070;
    example.com  bleeding_edge.internal:9999;

    default  localhost:8080;
}
server {
    listen [::]:80;
    location / {
        proxy_pass http://$host_upstream_mapped$request_uri;
    }
}

PS 참고: 사용하는 것이 중요합니다.$request_uri이런 맥락에서 (그리고 단지$uri, 그렇지 않으면 뒤처지게 될 것이기 때문입니다 . 또는 지시어도 $args사용하는 경우에는 좋은 옵션입니다(둘은 동일하지 않습니다).rewrite$uri$is_args$args

관련 정보