NGINX:我可以使用 proxy_pass 指令來對應上游伺服器嗎?

NGINX:我可以使用 proxy_pass 指令來對應上游伺服器嗎?

我有一個 NGINX 伺服器,它充當一堆後端 IIS 伺服器的反向代理。我想根據指定的 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 的工作原理:
proxy_pass http://$upstream_host$uri;
with
proxy_pass http://upstreamserver_a;
which Works。又名,它重定向到upstreamserver_a,同時將URL 隱藏在“test.blahblah.com”下。

任何幫助將不勝感激!
我發現了許多例子,其答案非常接近我的需要,但我太愚蠢了,無法將它們塑造成我的特殊需要!

答案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也是一個不錯的選擇(請注意,兩者並不等同)。

相關內容