NGINX:我可以使用proxy_pass指令使用上游服务器的映射吗?

我有一个NGINX服务器,作为一群后端IIS服务器的反向代理。 我希望将每个传入的HTTPS请求都传递给某个后端(上游)服务器,具体取决于指定的URI,同时在地址栏中保留相同的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部分的帮助,只需要映射和/或variables引用。
我已经certificate,SSL的工作原理是用这一行代替:
proxy_pass http://$upstream_host$uri;

proxy_pass http://upstreamserver_a;
哪个工作。 AKA,它redirect到upstreamserver_a,同时保持在URL“test.blahblah.com”伪装的URL。

任何帮助将非常感激!
我find了很多与我所需要的答案非常接近的例子,但是我太愚蠢了,无法满足我的特殊需求!

您需要使用正则expression式来描述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. } 

希望能帮助到你

根据你想要的究竟是什么,有一些指令可以想到:

例如,如果你基本上只想让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也是一个不错的select(注意两者不是等价的)。