文章出處

一、啟用gzip

1     gzip  on;
2     gzip_min_length 1k;
3     gzip_buffers 4 16k;
4     gzip_http_version 1.1;
5     gzip_comp_level 2;
6     gzip_types text/plain application/x-javascript text/css application/xml application/json;
7     gzip_vary on;
View Code

將上面這段放在nginx.conf文件 http{...}之間即可

 

二、虛擬主機

 1     server {
 2         listen       80;
 3         server_name  www.aaa.com;
 4         access_log logs/aaa.access.log combined;
 5         location / {
 6             root   r:/www/aaa.com;
 7             index  index.html index.htm;
 8         }       
 9     }
10 
11     server {
12         listen       80;
13         server_name  www.bbb.com;
14         access_log logs/bbb.access.log combined;
15         location / {
16             root   r:/www/bbb.com;
17             index  index.html index.htm;
18         }       
19     }
View Code

這里配置了二個虛擬主機,分別綁定到域名www.aaa.com及www.bbb.com

 

三、請求轉發

如上圖,為了安全起見,通常會把真正的web 應用服務器放在內網,不允許外界直接訪問,這時可利用nginx的反向代理將請求轉到內部服務器

 1     server {
 2         listen       80;
 3         server_name  www.aaa.com;    
 4     location / {
 5         proxy_pass http://192.168.1.201:8080 ;
 6         proxy_set_header Host $host:8080;
 7         proxy_set_header X-Real-IP $remote_addr;
 8         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 9         proxy_set_header Via "nginx";
10     }
11     access_log off;    
12     }
View Code

這樣,訪問 http://www.aaa.com/home/index.aspx 就相當于訪問http://192.168.1.201:8080/home/index.aspx

有些時候,我們希望將共用的靜態資源(比如:jquery,共用css),直接放在nginx下,背后的web app server,只處理動態資源(比如:.aspx/.do/.jsp),可以改進一下:

 1     server {
 2         listen       80;
 3         server_name  www.aaa.com;
 4     root R:/www/aaa.com;
 5 
 6     location / {
 7         proxy_pass http://192.168.1.201:8080 ;
 8         proxy_set_header Host $host:8080;
 9         proxy_set_header X-Real-IP $remote_addr;
10         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
11         proxy_set_header Via "nginx";
12     }
13 
14     location ~ .*\.(js|css)?$ {
15         expires 1h;
16     }
17 
18     access_log off;    
19     }
View Code

這樣,訪問http://www.aaa.com/content/site.css 實際上讀取的是R:/www/aaa.com/content/site.css 這個文件

 

四、負載均衡

如上圖,nginx在前端利用反向代理實現軟件負載均衡,nginx不僅能實現負載均衡,當某一個node失效時,還能自動故障轉移,關鍵配置如下:

 1     proxy_connect_timeout 5; 
 2     
 3     upstream aaa_server_group{    
 4     server 172.21.129.181:9091 max_fails=1 fail_timeout=60s; 
 5     server 172.21.129.181:9092 max_fails=1 fail_timeout=60s; 
 6     server 172.21.129.57:9012 max_fails=1 fail_timeout=60s;
 7     ip_hash;
 8     }
 9 
10     server {
11         listen       80;
12         server_name  www.aaa.com;
13     root R:/www/aaa.com;
14 
15     location / {
16         proxy_next_upstream http_502 http_504 error timeout invalid_header;
17         proxy_pass http://aaa_server_group ;            
18         proxy_set_header X-Forwarded-For $remote_addr;        
19     }
20 
21     location ~ .*\.(js|css)?$ {
22         expires 1h;
23     }
24 
25     access_log off;    
26     }
View Code

解釋一下:

第1行, proxy_connect_timeout 5;表示nginx連接后端服務器時,如果超過5秒沒反應,則認為超時

第3行,這里定義了一組服務器aaa_server_group,其中max_fails=1 fail_timeout=60s 表示如果該節點轉發失敗1次,接下來的60秒內,將不再轉發到這臺服務器

第6行,表示如果后端服務器返回502,504,超時等錯誤時,即認為節點失效,自動將請求轉發到其它節點

nginx負載均衡的方案有好幾種,這里我們配置為ip_hash;表示會根據客戶端的IP生成hash,固定轉發到某一臺node,這樣可解決集群環境中session丟失的問題。如果web應用中沒有使用session,去掉ip_hash;即變成輪訊方案。此外,4-6行上,還可以加weight=N來指定權重,按權重轉發,如果服務器配置不均衡,比如某一臺服務器配置特別強,可以考慮將這臺服務器的weight值加大。

如果某臺服務器暫時不想加入負載均衡,在最后加 down即可。

 

最后附送一個小技巧:nginx -t 可以檢查配置文件是否正確 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


全站熱搜
創作者介紹
創作者 大師兄 的頭像
大師兄

IT工程師數位筆記本

大師兄 發表在 痞客邦 留言(0) 人氣()