Nginx Linux環境下安裝及負載均衡配置

在正式介紹Nginx之前先簡單說明下什麼是正向代理和反向代理,Nginx實現負載均衡功能正是基於其反向代理功能實現的。

正向代理類似一個跳板機,代理訪問外部資源。正向代理 是一個位於客戶端和原始服務器之間的服務器,爲了從原始服務器取得內容,客戶端向代理髮送一個請求並指定目標(原始服務器),然後代理向原始服務器轉交請求並將獲得的內容返回給客戶端。客戶端必須要進行一些特別的設置才能使用正向代理。

反向代理的代理對象是服務端,客戶端不需要做任何配置。反向代理實際運行方式是指以代理服務器來接受internet上的連接請求,然後將請求轉發給內部網絡上的其它服務器,並將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現爲一個服務器。

一、Nginx安裝

1)下載Nginx及相關組件

[root@localhost mnt]# wget http://nginx.org/download/nginx-1.13.6.tar.gz

[root@localhost mnt]# wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz

[root@localhost mnt]# wget http://zlib.net/zlib-1.2.11.tar.gz

[root@localhost mnt]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz

安裝c++編譯環境,如已安裝可略過

[root@localhost src]# yum install gcc-c++

2)安裝Nginx及相關組件

openssl安裝

[root@localhost mnt]# tar zxvf openssl-fips-2.0.10.tar.gz

[root@localhost mnt]# cd openssl-fips-2.0.10

[root@localhost openssl-fips-2.0.10]# ./config && make && make install

pcre安裝

[root@localhost mnt]# tar zxvf pcre-8.40.tar.gz

[root@localhost mnt]# cd pcre-8.40

[root@localhost pcre-8.40]# ./configure && make && make install

zlib安裝

[root@localhost mnt]# tar zxvf zlib-1.2.11.tar.gz

[root@localhost mnt]# cd zlib-1.2.11

[root@localhost zlib-1.2.11]# ./configure && make && make install

nginx安裝

默認安裝到/usr/local/nginx,也可以指定安裝目錄./configure --prefix=/mnt/nginx

[root@localhost mnt]# tar zxvf nginx-1.13.6.tar.gz

[root@localhost mnt]# cd nginx-1.13.6

[root@localhost nginx-1.13.6]# ./configure && make && make install

3)啓動停止Nginx

[root@localhost mnt]# cd /usr/local/nginx/sbin

啓動nginx,-c表示指定nginx.conf的文件。如果不指定,默認爲NGINX_HOME/conf/nginx.conf

[root@localhost sbin]# ./nginx -c /usr/local/nginx/conf/nginx.conf

發送信號到nginx進程後,nginx進程會等待處理完畢當前請求,同時將不會接受新請求

[root@localhost sbin]# kill -QUIT 進程號

立即停止當前正在處理的請求

[root@localhost sbin]# kil -TERM 進程號

停止nginx

[root@localhost sbin]# ./nginx -s stop

退出nginx

[root@localhost sbin]# ./nginx -s quit

重新加載nginx.conf

[root@localhost sbin]# ./nginx -s reload

啓動成功,在瀏覽器訪問http://localhost,如果出現如下Nginx歡迎頁面則表示Nginx安裝成功。

二、Nginx負載均衡配置

Nginx核心配置文件爲/usr/local/nginx/conf/nginx.conf,其默認配置如下,反向代理默認端口是80,服務器名稱localhost,這也就是爲什麼上文提到的瀏覽器中直接訪問http://localhost可以看到歡迎頁面的原因。

worker_processes 1;
events {
 worker_connections 1024;
}
http {
 include mime.types;
 default_type application/octet-stream;
 sendfile on;
 keepalive_timeout 65;
 
 server {
 listen 80;
 server_name localhost;
 location / {
 root html;
 index index.html index.htm;
 }
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root html;
 }
 }
 }

Nginx反向代理配置說明:

1)listen 80;代表監聽80端口

2)server_name xxx.xxx.com;代表外網訪問的域名

3)location / {};代表一個過濾器,/匹配所有請求,還可以根據自己的情況定義不同的過濾,比如對靜態文件js、css、image制定專屬過濾

4)root html;代表站點根目錄

5)index index.html;代表默認主頁

負載均衡功能往往在接收到某個請求後分配到後端的多臺服務器上,如果要藉助Nginx實現負載均衡就需要upstream{}塊來配合使用。配置示例如下:

http {
 ......此處省略
 upstream www.feixiang.com {
 	ip_hash;
 server 139.129.196.11 weight=2;
 server 139.129.196.12 weight=1;
 }
 
 server {
 listen 80;
 server_name www.wf.com;
 location / {
 proxy_pass http://www.feixiang.com;
 }
 }
 }

1)upstream xxx.xxx.com{};upstream模塊是命名一個後端服務器組,組名必須爲後端服務器站點域名,內部可以寫多臺服務器ip和port,還可以設置跳轉規則及權重等等。

ip_hash;代表使用ip地址方式分配跳轉後端服務器,同一ip請求每次都會訪問同一臺後端服務器。

server;代表後端服務器地址,服務器狀態包括:down(表示單前的server暫時不參與負載)、weight(默認爲1.weight越大,負載的權重就越大)、max_fails(允許請求失敗的次數默認爲1.當超過最大次數時返回失敗)、fail_timeout(max_fails 次失敗後,暫停的時間)、backup(其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕)。

2)server{};server模塊依然是接收外部請求的部分。

server_name;代表外網訪問域名。

location / {};同樣代表過濾器,用於制定不同請求的不同操作。

proxy_pass;代表後端服務器組名,此組名必須爲後端服務器站點域名。

server_name和upstream{}的組名可以不一致,server_name是外網訪問接收請求的域名,upstream{}的組名是跳轉後端服務器時站點訪問的域名。

另外還需要在本機配置一個域名映射,即C:WindowsSystem32driversetc中條件一行139.129.196.11 www.wf.com。自此一個簡單的基於Nginx的負載均衡已經配置完畢,直接在瀏覽器中訪問http://www.wf.com,可以看到實際顯示的爲域名www.feixiang.com的內容。事實上Nginx已經把用戶請求採用ip_hash方式分發到www.feixiang.com的多個服務器上。

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