Nginx基本使用方法及各模塊基本功能

一、Nginx介紹

    Nginx是由俄羅斯軟件工程師Igor Sysoev開發的一個高性能的HTTP和反向代理服務器,具備IMAP/POP3和SMTP服務器功能,Nginx最大的特點是對高併發的支持和高效的 負載均衡,在高併發的需求場景下,是Apache服務器不錯的替代品。目前,包括新浪、騰訊等知名網站都已使用Nginx作爲Web應用服務器。下面我簡 單介紹一下:

    nginx是一個高性能的Web和反向代理服務器,它具有很多非常優越的特性;

    作爲Web服務器;相比較與Apache,Nginx使用更少的資源,支持更多的併發連接,體現更高的效率,這點使Nginx尤爲受到虛擬主機提供商的歡迎,能夠支持高達50000個併發的連接數的響應。

    作爲負載均衡服務器器:Nginx既可以在內部直接支持Rails和PHP,也可以支持作爲HTTP代理服務器對外驚醒服務,Nginx用C語言編寫,不論是系統資源開銷還是CPU使用效率都比Perlbal要好的多。

    作爲郵件代理服務器,Nginx同時也是一個非常優秀的郵件代理服務器(最早開發這個產品的目的之一也是作爲郵件代理服務器),Last.fm描述了成功並且美妙的使用經驗。

    Nginx安裝非常簡單,配置文件非常簡介(還能夠支持perl語法),Bugs非常少的服務器:Nginx啓動特別容易,並且幾乎可以做到7*24不間斷運行,即使運行數月也不需要重新啓動。還能夠在不間斷服務的情況下進行軟件版本平滑升級。

二、軟件獲得及幫助文檔

    官方地址:http://nginx.org

    下載穩定版本:http://nginx.org/download/nginx-1.8.0.tar.gz

    幫助文檔:http://nginx.org/en/docs

    編譯參數說明:http://nginx.org/en/docs/configure.html

三、Nginx的功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
1、Nginx的特性
   模塊化設計、較好的擴展性
   高可靠性:一個master啓動一或多個worker,每個worker響應多個請求
   低內存消耗:10000個keepalive連接在Nginx中僅消耗2.5MB內存(官方數據)
   支持熱部署:不停機更新配置文件、更新日誌文件、更新服務器程序版本
    
2、Nginx的基本功能
   靜態web資源服務器,能夠緩存打開的文件描述符
   支持http/imap/pop3/smtp的反向代理;支持緩存、負載均衡
   支持fastcgi(fpm)
   模塊化,非DSO機制,支持過濾器zip壓縮,SSI以及圖像大小調整
   支持SSL
    
3、Nginx的擴展功能
   基於名稱和IP的虛擬主機
   支持keepalive的保持機制
   支持平滑升級
   定製訪問日誌,支持使用日誌緩存區提高日誌存儲性能
   支持url rewrite
   支持路徑別名(root或alias指定)
   支持基於IP以及用戶的訪問控制
   支持傳輸速率限制,併發限制
    
4、Nginx的基本架構
   一個master進程,生成一個或者多個worker進程,每個worker響應多個請求
   事件驅動:epoll,kqueue,poll,select,rt signals
   支持sendfile,sendfile64
   支持AIO
   支持mmap
    
5、Nginx模塊類型
   Nginx core module: nginx的核心模塊
   Standard HTTP modules:nginx的標準模塊
   Optional HTTP modules:nginx的可選模塊
   Mail modules :nginx的郵件模塊
   3rd party modules:nginx的第三方模塊
    
6、Nginx進程詳解
 
   主進程主要完成如下工作:
       讀取並驗正配置信息;
       創建、綁定及關閉套接字;
       啓動、終止及維護worker進程的個數;
       無須中止服務而重新配置工作特性;
       控制非中斷式程序升級,啓用新的二進制程序並在需要時回滾至老版本;
       重新打開日誌文件,實現日誌滾動;
       編譯嵌入式perl腳本;
       worker進程主要完成的任務包括:
       接收、傳入並處理來自客戶端的連接;
       提供反向代理及過濾功能;
       nginx任何能完成的其它任務;
     
   cache loader進程主要完成的任務包括:
       檢查緩存存儲中的緩存對象;
       使用緩存元數據建立內存數據庫;
     
   cache manager進程的主要任務:
       緩存的失效及過期檢驗;

四、Nginx安裝配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
1、安裝依賴包(CentOS 6.7)
[root@mail soft]# yum -y groupinstall "Development tools,Server platform development,Desktop platform development"
[root@mail soft]# yum -y install pcre-devel openssl-devel
 
2、編譯安裝Nginx
[root@mail soft]# tar xf nginx-1.8.0.tar.gz 
[root@mail soft]# cd nginx-1.8.0
[root@mail nginx-1.8.0]# groupadd -r nginx
[root@mail nginx-1.8.0]# useradd -g nginx -s /sbin/nologin -M nginx
[root@mail nginx-1.8.0]# ./configure \
   --prefix=/usr/local/nginx \
   --sbin-path=/usr/sbin/nginx \
   --conf-path=/etc/nginx/nginx.conf \
   --error-log-path=/var/log/nginx/error.log \
   --http-log-path=/var/log/nginx/access.log \
   --pid-path=/var/run/nginx/nginx.pid  \
   --lock-path=/var/lock/nginx.lock \
   --user=nginx \
   --group=nginx \
   --with-http_ssl_module \
   --with-http_flv_module \
   --with-http_stub_status_module \
   --with-http_gzip_static_module \
   --http-client-body-temp-path=/var/tmp/nginx/client/ \
   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
   --http-scgi-temp-path=/var/tmp/nginx/scgi \
   --with-pcre
 [root@mail nginx-1.8.0]# make && make install
      
 ##注意:編譯後有些文件夾不會自動創建
 
[root@mail nginx-1.8.0]# mkdir -pv /var/tmp/nginx/{client,proxy,fcgi,uwsgi,scgi}
mkdir: created directory `/var/tmp/nginx'
mkdir: created directory `/var/tmp/nginx/client'
mkdir: created directory `/var/tmp/nginx/proxy'
mkdir: created directory `/var/tmp/nginx/fcgi'
mkdir: created directory `/var/tmp/nginx/uwsgi'
mkdir: created directory `/var/tmp/nginx/scgi'
 
這裏的二進制文件是直接指定在PATH環境變量裏面的,所有可以直接使用,不用導出:
 
配置vim,使其編輯nginx配置文件時語法着色,默認沒有
[root@mail nginx-1.8.0]# cd 
[root@mail ~]# mkdir .vim
[root@mail ~]# cp -ra /u01/soft/nginx-1.8.0/contrib/vim/* .vim
[root@mail ~]# ls .vim
ftdetect  indent  syntax
 
編寫啓動腳本:
[root@mail ~]# vim /etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# pidfile:     /var/run/nginx.pid
# Source function library.
/etc/rc.d/init.d/functions
# Source networking configuration.
/etc/sysconfig/network
# Check that networking is up.
"$NETWORKING" "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
   [ -x $nginx ] || exit 5
   [ -f $NGINX_CONF_FILE ] || exit 6
   echo -n $"Starting $prog: "
   daemon $nginx -c $NGINX_CONF_FILE
   retval=$?
   echo
   [ $retval -eq 0 ] && touch $lockfile
   return $retval
}
stop() {
   echo -n $"Stopping $prog: "
   killproc $prog -QUIT
   retval=$?
   echo
   [ $retval -eq 0 ] && rm -f $lockfile
   return $retval
}
restart() {
   configtest || return $?
   stop
   start
}
reload() {
   configtest || return $?
   echo -n $"Reloading $prog: "
   killproc $nginx -HUP
   RETVAL=$?
   echo
}
force_reload() {
   restart
}
configtest() {
 $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
   status $prog
}
rh_status_q() {
   rh_status >/dev/null 2>&1
}
case "$1" in
   start)
       rh_status_q && exit 0
       $1
       ;;
   stop)
       rh_status_q || exit 0
       $1
       ;;
   restart|configtest)
       $1
       ;;
   reload)
       rh_status_q || exit 7
       $1
       ;;
   force-reload)
       force_reload
       ;;
   status)
       rh_status
       ;;
   condrestart|try-restart)
       rh_status_q || exit 0
           ;;
   *)
       echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-
reload|configtest}"
       exit 2
esac
 
[root@mail ~]# chmod +x /etc/rc.d/init.d/nginx   #添加可執行權限
[root@mail ~]# chkconfig --add nginx   #添加到服務列表
[root@mail ~]# chkconfig nginx on      #設置開機自啓動
 
[root@mail ~]# service nginx start   #啓動nginx
Starting nginx:                                            [  OK  ]
[root@mail ~]# ss -tnl     #查看是否監聽80端口
[root@mail ~]# ss -tnl
State       Recv-Q Send-Q                Local Address:Port                  Peer Address:Port 
LISTEN      0      128                               *:80                               *:*     
LISTEN      0      128                              :::22                              :::*     
LISTEN      0      128                               *:22                               *:* 
##查看頁面
[root@mail ~]# curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
 
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
 
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
#######################編譯安裝已經完成。
 
配置文件介紹
  主要有兩部分:分別是
    main:主體部分
    http{}:虛擬主機配置部分
     
   配置指令主要以分號結尾;配置語法:directive value1 [value2 ....]
   支持使用的變量
       模塊內置的變量
       自定義變量:set var_name value
    
   主配置段的指令類別:
  用於調試和定位問題:
   (1)daemon [on|off]:  是否以守護進程的方式啓動nginx;
   (2)master_press [on|off]:  是否以master/worker模型來運行nginx;
   (3)error_log /path/to/error_loglevel:  指明錯誤日誌文件級別,處於調試目的,可以使用debug級別,但次級別只有在編譯nginx時使用了--with-debug選項纔有效 ;
        
  正常運行必備的配置:
   (1)user USERNAME [GROUPNAME]:指定運行worker的用戶和用戶組;例如 user nginx nginx 
   (2)pid /path/to/nginx.pid : 指定pid文件
   (3)worker_rlimit_nofile # : 指定一個worker進程能夠打開的最大文件句柄數
   (4)worker_rlimit_sigpending # : 指定每個用戶能夠發往worker信號的數量
        
  優化性能相關的配置:
   (1)worker_processes # :worker進程的個數,通常是cpu核心數減1
   (2)worker_cpu_affinity cpuumask :綁定worker進程至指定的CPU上
   (3)timer-resolution t :時間解析度,在x86服務器上可以不用配置
   (4)worker_priority NICE :調整nice值(-20,19);nice值越大,越優先分配cpu
        
  事件相關的配置;
   (1)accept_mutex [on|off] :內部調動用戶請求至各worker時的負載均衡鎖;啓用時表示能夠讓多個worker輪流的、序列化的響應請求
   (2)lock_file /path/to/lock_file :指定鎖文件
   (3)accept_mutex_delay #ms: 取得負載均衡鎖的時間
   (4)use [epoll|poll|select|rgsig]:定義使用的事件模型,建議讓nginx自動選擇
   (5)worker_connections #:每個worker進程所能夠響應的最大併發請求數

五、Nginx的一些基本功能實現

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1、基於用戶認證:
(1)、修改配置文件
server {
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   html;
            index  index.html index.htm;
        auth_basic "www.bjwf125.com";
        auth_basic_user_file /www/html/.passwd;
        }
 
(2)、創建文檔根目錄以及使用httpd-tools中的htpasswd工具創建用戶
[root@mail ~]# mkdir -pv /www/html
mkdir: created directory `/www'
mkdir: created directory `/www/html'
[root@mail ~]# echo "Welcome to bjwf125" > /www/html/index.html
[root@mail ~]# yum -y install httpd-tools
[root@mail ~]# htpasswd -c -m /www/html/.passwd centos    #創建centos用戶
New password:                                    #輸入密碼
Re-type new password:                            #再次輸入
Adding password for user centos
(3)、重新載入配置文件
[root@mail ~]# nginx -t    #檢查Nginx語法
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@mail ~]# service nginx reload     #重新載入

wKioL1aVrVfgNYf4AAGSN0NIzVQ440.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2、基於IP認證
server {
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   html;
            index  index.html index.htm;
        deny 192.168.9.0/24;
        allow all;
        }
 
 3、基於gzip壓縮
 server {
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   /www/html;
            index  index.html index.htm;
        gzip on;
        gzip_http_version 1.0;
        gzip_min_length 1000;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain application/xml text/css application/x-javascript text/xml
             application/xml+rss text/javascript application/javascript application/json;
        gzip_disable msie6 safari;
 
        }
 
[root@mail ~]# nginx -t
[root@mail ~]# service nginx reload
[root@mail ~]# cp /etc/rc.d/rc.sysinit /www/html/zip.html
[root@mail ~]# ll /www/html/zip.html -h
-rwxr-xr-x 1 root root 20K Jan 13 10:01 /www/html/zip.html

wKiom1aVs12CRESrAAElOeZcn4I017.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
4、定製響應頭部
server {
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   /www/html;
            index  index.html index.htm;
        expires 24h;
        add_header bjwf125 mymail;
        }
[root@mail ~]# nginx -t
[root@mail ~]# service nginx reload

wKioL1aVtYbQ5ibvAAFWdcXTzZ8041.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
5、URL重定向
   語法格式:
   rewrite grgex replacement [flages]
   flages
   last:一旦被當前規則匹配並重寫後立即停止檢查其他後續的rewrite的規則,而後通過重寫後的規則重寫發起請求;
   bleak:一旦被當前規則匹配並重寫後立即停止後續的其他rewrite的規則,而後由nginx進行後續操作;
   redirect:返回302臨時重定向
   permanent:返回301永久重定向
例如:
[root@mail ~]# vim /etc/nginx/nginx.conf
 server {
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   /www/html;
            index  index.html index.htm;
        rewrite ^/admin/(.*)$ /web/$1;
        }
  
   
[root@mail ~]# mkdir -pv /www/html/{admin,web}
[root@mail ~]# echo "mail.bjwf125.com" > /www/html/web/index.html
[root@mail ~]# nginx -t
[root@mail ~]# service nginx reload

wKiom1aVuBrSz58EAACIOfL4jvY427.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
6、虛擬主機
[root@mail nginx]# vim /etc/nginx/nginx.conf
#註釋掉http{}段中的預定義server{}段中的所有內容;
在文件末尾}前一行添加一條:
include conf.d/nginx-vhost.conf;
#方便後面定義,根據個人習慣而已,也可以直接在/etc/nginx/nginx.conf中配置
[root@mail nginx]# vim /etc/nginx/conf.d/nginx-vhost.conf 
server {
   listen       80;
   server_name  www.a.com;
         
   location / {
        root    /www/html/a;
        index   index.html index.htm;
        }
}
  
server {
   listen       80;
   server_name  www.b.com;
         
   location / {
        root    /www/html/b;
        index   index.html index.htm;
        }
}
 
[root@mail nginx]# mkdir /www/html/{a,b} -pv
mkdir: created directory `/www/html/a'
mkdir: created directory `/www/html/b'
[root@mail nginx]# echo "www.a.com" > /www/html/a/index.html
[root@mail nginx]# echo "www.b.com" > /www/html/b/index.html
[root@mail nginx]# vim /etc/hosts
192.168.9.9     www.a.com
192.168.9.9     www.b.com
[root@mail nginx]# service nginx reload
[root@mail nginx]# curl http://www.a.com
www.a.com
[root@mail nginx]# curl http://www.b.com
www.b.com
###虛擬主機最簡單的方式已經配置完成,但是虛擬主機裏面還有很多參數。
7、防盜鏈
(1)、定義合規的引用
        valid_referers none | blocked | server_names | string ...;
 (2)、拒絕不合規的引用
         if ($invalid referer) {
          rewrite ^/ http://www.b.com/403.html;
         }
  ##具體示例如下:
  [root@mail conf.d]# vim nginx-vhost.conf
   server {
   listen       80;
   server_name  www.b.com;
         
   location / {
        root    /www/html/b;
        index   index.html index.htm;
        valid_referers none blocked www.b.com *.b.com;
        if ($invalid_referer) {
        rewrite ^/ http://www.b.com/403.html;
        }
      
   
    
   [root@mail conf.d]# vim /www/html/a/index.html
   www.a.com
   <img src="http://www.b.com/p_w_picpaths/1.jpg">   #在a.com中引用
    [root@mail conf.d]# vim /www/html/b/index.html
   www.b.com
   <img src="http://www.b.com/p_w_picpaths/1.jpg">   #b.com自己引用

測試結果:

wKioL1aVwwTyoLWTAABho2r65aA758.pngwKioL1aVwxiiPePJAAGFvZDq19c001.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
8、Nginx的反向代理
   Nginx可以通過proxy模塊實現反向代理功能,在作爲web反向代理服務器時,Nginx複製接收客戶端請求,並能夠根據URL、客戶端參數或者其它的處理邏輯將用戶請求調度至上游服務器上(upstream server)。
   Nginx在實現反向代理功能時最重要的指令爲proxy_pass,它能夠將location中定義的某URI代理至指定的上游服務器(組)上。如下面的示例中,location的URI將被替換爲上游服務器上的newURI。
### 例如:
[root@mail conf.d]# vim nginx-vhost.conf 
server {
   listen       80;
   server_name  www.a.com;
   add_header X-Via $server_addr;
 
   location / {
        root    /www/html/a;
        index   index.html index.htm;
        }
   location = /node2 {
        proxy_pass http://192.168.9.11/;
        }
}
##上游服務器必須要配置相應服務及頁面
[root@mail conf.d]# service nginx reload
[root@mail conf.d]# curl  www.a.com
<img src="http://www.b.net/p_w_picpaths/1.jpg"
[root@mail conf.d]# curl http://www.a.com/node1
node1.bjwf125.com
 
 
(1)、緩存:Nginx作爲反向代理時,能夠將上游服務器的響應緩存至本地,並在後續的客戶端請求同樣的內容時直接從本地構造響應報文。具體參數如下:
    proxy_cache zone|off:定義一個用於緩存的共享內存區域,其可被多個地方調用;
    proxy_cache_path:定義一個保存緩存響應報文的目錄,及一個保存緩存對象的鍵及響應元數據的共享內存區域(keys_zone=name:size),其可選參數有:
          levels:每級子目錄名稱的長度,有效值爲1或2,每級之間使用冒號分隔,最多爲3級;
          inactive:非活動緩存項從緩存中剔除之前的最大緩存時長;
          max_size:緩存空間大小的上限,當需要緩存的對象超出此空間限定時,緩存管理器將基於LRU算法對其進行清理;
          loader_files:緩存加載器(cache_loader)的每次工作過程使用爲多少個文件加載元數據;
          loader_sleep:緩存加載器的每次迭代工作之後的睡眠時長;
          loader_threashold:緩存加載器的最大睡眠時長;
    proxy_cache_valid [ code ... ] time:用於爲不同的響應設定不同時長的有效緩存時長,例如: 
          proxy_cache_valid 200  302  10m;
    proxy_cache_methods [GET HEAD POST]:爲哪些請求方法啓用緩存功能;
    proxy_cache_bypass string:設定在哪種情形下,nginx將不從緩存中取數據。
示例:
# vim /etc/nginx/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
    server {
        listen 80;
        server_name node1;
        add_header X-Via $server_addr;
        location / {
                root /www/html/b;
                index index.html index.htm;
        proxy_pass http://192.168.9.11;
        proxy_set_header Host $host;
        proxy_cache STATIC;
        proxy_cache_valid 200 1d;
        proxy_cache_valid 301 302 10m;
        proxy_cache_valid any 1m;
        proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
        }
   
}   
 
[root@mail nginx]# mkdir -pv /data/nginx/cache
[root@mail nginx]# nginx -t
[root@mail nginx]# service nginx reload


緩存前請求時間


wKiom1aV7-SAyWEQAAGrwmlONBg169.png


緩存後請求時間


wKiom1aV8AOBELMsAAGrp1jsVVY909.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
####主要文件太小,效果不是太明顯,有強迫症的同學可以自己測試。
##此時可以查看緩存目錄是否有文件生成
[root@mail ~]# ll /data/nginx/cache/
drwx------ 3 nginx nginx 4096 Jan 13 14:32 1
 
(2)、負載均衡:Nginx可以利用自身的upstream模塊實現,upstream模塊的負載均衡算法主要有三種,輪調(round-robin)、ip哈希(ip_hash)和最少連接(least_conn)三種。
    upstream模塊常用的指令有:
        ip_hash:基於客戶端IP地址完成請求的分發,它可以保證來自於同一個客戶端的請求始終被轉發至同一個upstream服務器;
        keepalive:每個worker進程爲發送到upstream服務器的連接所緩存的個數;
        least_conn:最少連接調度算法;
        server:定義一個upstream服務器的地址,還可包括一系列可選參數,如;
            weight:權重;
            max_fails:最大失敗的連接次數,失敗連接的超時時長由fail_timeout指定;
            fail_timeout:等待請求的目錄服務器發送響應的時長;
            backup:用於fallback的目的,所有服務均故障時才啓動此服務器;
            down:手動標記其不再處理任何請求;
示例:
# vim /etc/nginx/nginx.conf
http {
    upstream web {
        server 192.168.9.11:80 max_fails=3 fail_timeout=10s;
        server 192.168.9.13:80 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:8080 backup;
        }
    server {
        listen 80;
        server_name www.c.net;
        add_header X-Via $server_addr;
        location / {
                root /www/html/b;
                index index.html index.htm;
        proxy_pass http://web;
        }
   }
   server {
        listen 8080;
        server_name 127.0.0.1;
        location / {
                root /www/html/b;
                index index.html index.htm;
        }
   }
    
[root@mail ~]# curl http://www.c.net
192.168.9.11
[root@mail ~]# curl http://www.c.net
192.168.9.13 
##停掉上游兩臺服務器後:
[root@mail ~]# curl http://www.c.net
Sorry
 
#####Nginx的基本功能基本完成

The end

有關Nginx的基本配置及一般用法就寫到這裏了,第一次寫Nginx的博客,寫的比較亂,比較雜,敬請諒解,有什麼不對的地方,麻煩朋友們告訴我。以上爲個人學習整理,如有錯漏,大神勿噴。。。。。


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