nginx線上配置

1.前後端分離之靜態網站配置

(1)假如我們有一個網站
在這裏插入圖片描述
主頁路徑:/usr/local/hadluo/qiqi/qiqi-phone/index.html
(2)nginx配置

       server {
                listen       80;
               	##你網站的ip,或者域名
                server_name  localhost;
                log_not_found off;
                #add_header Access-Control-Allow-Origin $corsHost;
                add_header Access-Control-Allow-Credentials true;
                add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
                add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";

         		## 訪問localhost/qiqi
                location /qiqi {
                ##alias :指定網站在本機的絕對路徑
                        alias  /usr/local/hadluo/qiqi/qiqi-phone/;
                        index  index.html;
                }
        }

(3)測試訪問http://localhost/qiqi

2.負載均衡配置

(1)內置負載策略

  1. 輪循(默認):Nginx根據請求次數,將每個請求均勻分配到每臺服務器。
  2. 最少連接: 將請求分配給連接數最少的服務器。Nginx會統計哪些服務器的連接數最少。
  3. IP Hash : 綁定處理請求的服務器。第一次請求時,根據該客戶端的IP算出一個HASH值,將請求分配到集羣中的某一臺服務器上。後面該客戶端的所有請求,都將通過HASH算法,找到之前處理這臺客戶端請求的服務器,然後將請求交給它來處理。

(2)定義一個upstream集羣

 upstream hadluo-zull{
	server 127.0.0.1:8882 weight=10;
	server 127.0.0.1:8883 weight=10;
	server www.examp.com:8883 weight=10;
	server 192.168.0.102:8080 backup;
}

hadluo-zull集羣名稱,server爲機器,weight爲權重。
backup : 指定備份機,所有服務掛了就走backup 機。

(3)使用這個集羣

server {
           listen       80;
           location / {
           	   # upstream指定的集羣名稱
               proxy_pass http://hadluo-zull;
           }
        }

當訪問nginx機器的80端口時,就會映射到hadluo-zull集羣裏面。

3.集成upstream_check_module模塊

模塊點的作用
nginx自帶的針對後端節點健康檢查的功能比較簡單,無法主動識別後端節點狀態,後端即使有不健康節點, 負載均衡器依然會把該請求轉發給該不健康節點,只能等待超時時間後轉發到其他節點,這樣就會造成響應延遲性能降低的問題。

安裝使用
(1)下載最新的穩定版本nginx-1.16.1源碼包並解壓

[root@nginx ~]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@nginx ~]# tar -zxvf nginx-1.16.1.tar.gz

(2)nginx_upstream_check_module模塊下載
如果沒有unzip命令可以通過’yum -y install unzip’安裝。

[root@nginx ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[root@nginx ~]# yum -y install unzip
[root@nginx ~]# unzip master.zip

(3)安裝相關包
分別安裝依賴包gcc、pcre、zlib、OpenSSL。

[root@nginx ~]# yum -y install gcc-c++  pcre pcre-devel zlib zlib-devel openssl openssl-devel

(4)進入nginx源碼目錄,打上模塊補丁

[root@nginx ~]# yum -y install patch
[root@nginx ~]# cd nginx-1.16.1
[root@nginx nginx-1.16.1]# patch -p1 < /root/nginx_upstream_check_module-master/check_1.16.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h

(5)編譯安裝nginx

[root@nginx nginx-1.16.1]# ./configure --add-module=/root/nginx_upstream_check_module-master
[root@nginx nginx-1.16.1]# make && make install

(6)安裝到了/usr/local/nginx目錄下,檢驗並設置軟連接

[root@nginx nginx-1.16.1]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
[root@nginx nginx-1.16.1]# nginx -v
nginx version: nginx/1.16.1
[root@nginx nginx-1.16.1]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --add-module=/root/nginx_upstream_check_module-master
[root@nginx nginx-1.16.1]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

(7)配置模塊
配置一個集羣
在這裏插入圖片描述

配置監控地址在這裏插入圖片描述

訪問成功在這裏插入圖片描述

4.ip限流

(1)定義http全侷限流規則

limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
  • limit_req_zone定義在http塊中,$binary_remote_addr表示保存客戶端IP地址的二進制形式。
  • Zone定義IP狀態及URL訪問頻率的共享內存區域。zone=keyword標識區域的名字,以及冒號後面跟區域大小。16000個IP地址的狀態信息約1MB,所以示例中區域可以存儲160000個IP地址。
  • Rate定義最大請求速率。示例中速率不能超過每秒10個請求。

(2)設置限流

location / {
        limit_req zone=mylimit burst=20 nodelay;
        proxy_pass http://real_server;
}

burst排隊大小,nodelay不限制單個請求間的時間

5.指定白名單ip內 不限流


geo $limit {
default              1;
192.168.2.0/24  0;
}
 
map $limit $limit_key {
1 $binary_remote_addr;
0 "";
}
 
limit_req_zone $limit_key zone=mylimit:10m rate=1r/s;
 
location / {
        limit_req zone=mylimit burst=1 nodelay;
        proxy_pass http://real_server;
}

上述配置中,192.168.2.0/24網段的IP訪問是不限流的,其他限流。
IP後面的數字含義:

  • 24表示子網掩碼:255.255.255.0
  • 16表示子網掩碼:255.255.0.0
  • 8表示子網掩碼:255.0.0.0

6.nginx接口緩存

(1)http塊聲明緩存

#############################################################################
###cache聲明
##############################################################################
proxy_temp_path /opt/tmp;
proxy_cache_path /opt/neicun levels=1:2 keys_zone=my_cache:20m inactive=30 max_size=2g;
  • proxy_cache_path 緩存文件路徑。
  • levels 設置緩存文件目錄層次;levels=1:2 表示兩級目錄。
  • keys_zone 設置緩存名字和共享內存大小。
  • inactive 在指定時間內沒人訪問則被刪除。
  • max_size 最大緩存空間,如果緩存空間滿,默認覆蓋掉緩存時間最長的資源。

(2)配置location

location / {
proxy_pass http://hadluo-zull;
}
## nginx接口緩存
include /opt/include/nginx-cache-hadluo-zull.conf;

這裏配置到了外部文件

(3)nginx-cache-hadluo-zull.conf

#/clients 爲你的接口 路徑  http://hadluo-zull/clients 
location ^~ /clients {
	proxy_pass   http://hadluo-zull;
	proxy_cache my_cache; 
	# 對於返回code爲200 的緩存60s
	proxy_cache_valid 200 60s;
	#緩存的http method類型
	proxy_cache_methods GET HEAD POST;
	#當多個客戶端請求一個緩存中不存在的文件(或稱之爲一個MISS),只有這些請求中的第一個被允許發送至服務器。>其他請求在第一個請求得到滿意結果之後在緩存中得到文件。
	proxy_cache_lock on;
	proxy_cache_use_stale updating error timeout http_404 http_500 http_502 http_503 http_504;
	add_header Access-Control-Allow-Credentials true; 
	add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
	##### $arg_xx : 爲參數是否參與緩存
	#proxy_cache_key "$scheme$host$uri=$arg_trackType=$arg_pageSize=$arg_storeType=$arg_max=$arg_cursor=$arg_hideNoStock"; 
	proxy_cache_key "$scheme$host$uri";
	# 接口帶cookie時加這行,忽略 cookie
	proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
}

proxy_cache_use_stale updating error timeout http_404 http_500 http_502 http_503 http_504 參數說明:

  • updating:當緩存內容過期,有一個請求正在訪問上游試圖更新緩存時,其他請求直接使用過期內容返回客戶端。
  • error:當與上游建立連接、發送請求、讀取響應頭部等情況出錯時,使用緩存。
  • timeout:當與上游建立連接、發送請求、讀取響應頭部等情況出現超時,使用緩存。
  • http_xxx:緩存以上錯誤響應嗎的內容。

(4)nginx reload 測試
訪問你的接口:http://hadluo-zull/clients 就會生成nginx緩存目錄:
在這裏插入圖片描述

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