Nginx的proxy_pass及upstream的小型負載均衡

proxy_pass

Nginx的proxy_pass將請求代理到其餘的後端服務器。例如html

listen 9999;
        server_name wyc.com;

        location /test/aaa {
            content_by_lua_block {
             ngx.say('new:9999/test/aaa')
         }
        }
        location /test/aaa/bbb {
            content_by_lua_block {
             ngx.say('new:9999/test/aaa/bbb')
         }
        }
        location /ttt {
            proxy_pass http://wyc.com:9999/test;
        }

上面請求http://localhost:9999/ttt/aaa/bbb,會匹配到/ttt進入,規則是去掉ttt,將後面的url和參數拼接到proxy_pass的後面,因而返回的new:9999/test/aaa/bbb,即location爲/test/aaa/bbb的內容。nginx

  • 請求http://wychao.com:8888/user/wyc/wyc/aaa?aa=11&bb=22 (匹配到域名和端口和對應的location爲/user/wyc,而後將後面的/wyc/aaa?aa=11&bb=22proxy_pass到須要代理到的服務,正好匹配到另外一個server的location裏)
server {
        server_name wychao.com;
        listen 8888;

        location /user/wyc {
            proxy_pass http://aaa.wyc.com:8888/user;
        }
        location = /user/name {
            echo $request_uri;
        }
    }
    server {
        server_name aaa.wyc.com;
        listen 8888;
        location /user/wyc {
            echo $request_uri;
        }
    }

結果:/user/wyc/aaa?aa=11&bb=22後端

url location proxy_pass target_url
aaa.com/aa/bb?a=1 /aa http://bbb.com/cc http://bbb.com/cc/bb?a=1
  • 即最終代理到的url:將請求的uri去掉匹配到的location部分,其他部分拼接到proxy_pass後面,包括query_string部分。

若是proxy_pass http://aaa.wyc.com:8888不帶url,則是將匹配到的url附加到host以後服務器


upstream

upstream能夠作簡單的負載均衡等,下面以百度爲例,upstream在http段,負載均衡

upstream search {
         server 180.149.132.47;
     }

    server {
        # List port
        listen 8888;
        server_name wyc.com;
        
       location /s {
            #add_header 'Access-Control-Allow-Credentials' 'true';

            proxy_redirect   off;
            proxy_set_header Host            "www.baidu.com";
            proxy_set_header X-Real-Ip       $remote_addr;
            proxy_set_header X-Forwarded-for $remote_addr;

            proxy_pass http://search;
        }
    }

上面請求域名wyc.com,端口8888,location將代理到search的upstream中,訪問:lua

http://wyc.com:8888/s?wd=nba

http://baidu.com/s?wd=nba
的結果是同樣的url

upstream支持多種負載均衡,下面一個最簡單的根據權重分配:代理

#server1:
    server {
        server_name wwyycc.com;
        listen 8888;
        location /user {
            content_by_lua_block {
             ngx.say('wwyycc.com:host')
             }
        }
    }

#server2:

    server {
        server_name wwyycc.com;
        listen 9999;
        location /user {
            echo "user wwyycc user";
        }
    }

#負載:server3:server_name:wyc.com 端口:8888
     upstream manageserver {
         server wwyycc.com:8888 weight=2;
         server wwyycc.com:9999;
     }

       location /user {
            #add_header 'Access-Control-Allow-Credentials' 'true';

            proxy_redirect   off;
            proxy_set_header Host            "wwyycc.com";
            proxy_set_header X-Real-Ip       $remote_addr;
            proxy_set_header X-Forwarded-for $remote_addr;

            proxy_pass http://manageserver;
        }

請求http://wyc.com:8888/user,結果按照順序返回兩次server1的結果,再返回一次server2的結果。(上面的例子要成功執行須要nginx的resolver解析,詳細見另外一篇文章http://www.cnblogs.com/mentalidade/p/6934162.html),echo等指令也是第三方模塊echo-nginx-module,這些都是openresty自帶的。
rest

相關文章
相關標籤/搜索