nginx的反向代理搭建配置以及搭建過程中的一些思維發散

首先來介紹下nginx的反向代理。

代理服務器一般分爲正向代理(通常直接稱爲代理服務器)和反向代理。

畫個圖我們就好理解了。

正向代理:可以想象成是路由器,我們要通過它來上網的那種。(可以說是客戶端的代理)

wKioL1Szx63y90WxAADYfYkxk8Y874.jpg

反向代理:客戶端的請求過來之後交給反向代理服務器,然後反向代理服務器再交給後臺真實的服務器。(這個是服務器端的代理)

wKiom1Szxlvho3-bAADf08_aRyE126.jpg

我們今天說的是nginx的反向代理功能的實現。同時,反向代理還可以實現負載均衡的功能。可以自己思考下。

由於實驗比較簡單,這邊環境就簡單處理。

http-server1:192.168.10.156(Apache服務)

http-server2:192.168.10.157(nginx服務)

nginx-proxy:192.168.10.159



保證http服務器搭建完成,並能正常訪問

nginx-proxy安裝(其他依賴條件自己安裝)。版本:nginx-1.4.7.tar.gz

tar -zxvf nginx-1.4.7.tar.gz /home

./configure 

--prefix=/usr/local/nginx \

--sbin-path=/usr/local/bin  \

--conf-path=/etc  \

--error-log-path=/usr/local/nginx/error.log  \

--pid-path=/usr/local/nginx/ngnix.pid \

--lock-path=/usr/local/nginx/nginx.lock  \

--with-http_ssl_module  \

--with-http_gzip_static_module  \

--with-http_perl_module \

--http-log-path=/usr/local/nginx/access.log \

--http-fastcgi-temp-path=/usr/local/nginx/html \

--with-pcre --with-zlib=/usr --with-openssl=/usr

make && make install 

編輯nginx的配置文件。

vim /etc/nginx.conf

#user  nobody;

worker_processes  1;


events {

    worker_connections  1024;

}



http {

     upstream loadbance {

                server 192.168.10.156;

                server 192.168.10.157;(如果其其他端口,後面請加端口號:X.X.X.X:8888)

        }

     server {

                listen 80;

                location / {

                        proxy_pass http://loadbance;

                }

        }

    }



紅色部分爲搭建反向代理服務器所需要的簡單配置。(注意,nginx行尾的分號)

upstream:反向代理的關鍵字

loadbance:你可以理解爲後臺一組服務器的名稱

server: 後臺真實的服務器地址,可以是IP或者FQDN(如果是FQDN,別忘記解析)

listen:監聽的端口,如果沒有把原來nginx做web服務器的配置內容註銷掉,建議改爲其他端口

proxy_pass:後面跟http://loadbance   此一定要和upstream後面(服務器組名稱)的名稱保持一致


OK,大功告成。訪問進行測試。

訪問代理服務器地址:http://192.168.10.159,然後刷新

wKiom1SzzRfjOOr7AAKK-n1Wnac484.jpg


wKioL1Szzl6TfjKHAANjR-U4TMo811.jpg



這就是nginx的反向代理。其實我們還可以延伸下。

nginx的反向代理功能我認爲就可以當做一個簡單的負載均衡來使用,我們可以指定nginx的調度算法:

nginx的官網上說(其實個人認爲不止4中,你可以在服務器的ip地址後面加上服務器的權值,但是按官網上加上權值是不算一種。)

NGINX supports four load balancing methods:

1):The round-robin method    輪詢

2):The least_conn method   最少連接數

a request is sent to the server with the least number of active connections with server weights taken into consideration。請求會送到活躍鏈接最少的服務器上(服務器的權值要考慮進來的)

3):The ip_hash method   

The method guarantees that requests from the same address get to the same server unless it is not available。這種方法保證了來自同一個IP地址的請求會得到同一個服務器的響應,除非掛了。(其是通過客戶端的ip(IPV4 or IPV6)地址來計算出此IP的hash值的)

4);The generic hash method:

 the server to which a request is sent is determined from a user-defined key which may be a text, variable, or their combination. For example, the key may be a source IP and port,

請求會發送到一個用戶定義鍵值(可以是text,變量,或者混合的)的服務器上


再來看一個官網說的:

If a method other than the default one is used, the corresponding directive (least_conn,ip_hash or hash) should be specified inside the upstream before the server directives.如果你要使用其他的調度算法(默認使用的round-robin),相應的指令必須在upstream內部指定,並且要在server指令之前。

比如:最小數鏈接/ip_hash/hash $request_uri consistent;(如果是使用輪詢的方式的話,就不用寫了,因爲默認算法就是輪詢)

upstream loadbance {

                least_conn;   //或者是ip_hash和(hash $request_uri consistent)

                server 192.168.10.156 weight=3;  //可以設置權值的哦

                server 192.168.10.157;(如果其其他端口,後面請加端口號:X.X.X.X:8888)

                }


哎呀,我擦,這就扯遠了。其實,nginx還有一個健康檢查的功能,如果有臺服務器掛了,請求不會分發到該服務器上。比如下面的寫法:

upstream loadbance {

                server 192.168.10.156 weight=3

                server 192.168.10.157;

                server 192.168.10.158 backup;//當備機使用

                server 192.168.10.155 down;  //如果你要臨時移除一臺服務器進行升級或者其他操作,可以把其標記爲down的狀態,這樣請求就不會到其上。

                }


其實,我們還可以通過server內部的location指令來指定不同的路徑分發到不同的服務器上去。來實現分流。

比如,可以定義多個upstream。upstream1,upstream2.......然後

location  /mp3 {

    proxy_pass http://upstream1;

    }

location /flv {

    proxy_pass http://upstream2;

    }

等等等等。location後面的路徑你可以使用精確路徑,通配符,正則表達式扥等。OK,到此爲止。要不然就剎不住了,東西太多了。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章