Linux-Nginx簡單入門

一、Nginx概念        

       Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。由俄羅斯的程序設計師Igor Sysoev所開發,供俄國大型的入口網站及搜索引擎Rambler(俄文:Рамблер)使用。其特點是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等。

二、負載均衡策略

       1、使用硬件複雜均衡策略實現,如使用F5、Array等負載均衡器

       2、使用軟件進行負載均衡

            2.1 使用阿里雲服務器均衡負載SLB

           2.2 使用Nginx+Keepalived

            2.3 其它軟件負載均衡如LVS(Linux Virtual Server)、haproxy等技術。

三、Nginx的優點

      1、Nginx 可以在大多數 UnixLinux OS 上編譯運行,並有 Windows 移植版。 Nginx 的1.4.0穩定版已經於2013年4月24日發佈,一般情況下,對於新建站點,建議使用最新穩定版作爲生產版本,已有站點的升級急迫性不高。Nginx 的源代碼使用 2-clause BSD-like license。
      2、Nginx 是一個很強大的高性能Web和反向代理服務器,它具有很多非常優越的特性:
在連接高併發的情況下,Nginx是Apache服務器不錯的替代品:Nginx在美國是做虛擬主機生意的老闆們經常選擇的軟件平臺之一。能夠支持高達 50,000 個併發連接數的響應,感謝Nginx爲我們選擇了 epoll and kqueue作爲開發模型。

四、Nginx安裝

       如果大家是剛新建的虛擬機並且最小化安裝的話,需要先配置靜態IP並且要能上網,大家可以參考:http://blog.csdn.net/u012453843/article/details/52839105這篇博客進行學習,編輯文章用vi即可。配置好靜態IP並且能上網之後需要先安裝wget、vim和gcc。

[html] view plain copy
  1. yum install wget  
  2. yum install vim-enhanced  
  3. yum install make cmake gcc gcc-c++  

       1、下載nginx安裝包

[html] view plain copy
  1. [root@nginx1 ~]# wget http://nginx.org/download/nginx-1.6.2.tar.gz  
  2. --2017-04-07 01:44:55--  http://nginx.org/download/nginx-1.6.2.tar.gz  
  3. 正在解析主機 nginx.org... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ...  
  4. 正在連接 nginx.org|206.251.255.63|:80... 已連接。  
  5. 已發出 HTTP 請求,正在等待迴應... 200 OK  
  6. 長度:804164 (785K) [application/octet-stream]  
  7. 正在保存至: “nginx-1.6.2.tar.gz”  

      2、安裝依賴,其中pcre(perl compatible regular expressions)是一個pert庫,包括perl兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。pcre-devel是使用pcre開發的一個二次庫,nginx也需要此庫。zlib庫提供了很多種壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。openssl是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的祕鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序提供測試或其它目的的使用。nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。

[html] view plain copy
  1. yum install -y pcre pcre-devel  
[html] view plain copy
  1. yum install -y zlib zlib-devel  
[html] view plain copy
  1. yum install -y openssl openssl-devel  
         3、解壓nginx-1.6.2.tar.gz到/usr/local/目錄下

[html] view plain copy
  1. [root@nginx1 ~]# tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/  

         4、進入到/usr/local目錄下,可以看到我們解壓後的nginx-1.6.2文件夾了,然後我們進行configure配置,命令:cd nginx-1.6.2 && ./configure --prefix=/usr/local/nginx。可以看出,這條命令是組合命令,先進入nginx-1.6.2目錄然後在執行./configure命令。如下圖所示。

          5、編譯安裝

[html] view plain copy
  1. [root@nginx1 nginx-1.6.2]# make && make install  
          6、啓動Nginx,啓動完之後檢查nginx是否已經正常啓動,看到如下信息說明正常啓動。

[html] view plain copy
  1. [root@nginx1 nginx-1.6.2]# /usr/local/nginx/sbin/nginx   
  2. [root@nginx1 nginx-1.6.2]# ps -ef | grep nginx  
  3. root       3640      1  0 04:40 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx  
  4. nobody     3641   3640  0 04:40 ?        00:00:00 nginx: worker process        
  5. root       3643   1368  0 04:40 pts/0    00:00:00 grep nginx  
  6. [root@nginx1 nginx-1.6.2]#  
          如果要關閉nginx,我們可以使用如下命令:

[html] view plain copy
  1. [root@nginx1 nginx-1.6.2]# /usr/local/nginx/sbin/nginx -s stop  
          如果想要重新熱啓動nginx,則使用如下命令:

[html] view plain copy
  1. [root@nginx1 nginx-1.6.2]# /usr/local/nginx/sbin/nginx -s reload  
          7、nginx默認的端口是80,我們需要在防火牆配置中添加80端口:"-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT"從而可以讓外界訪問80端口,否則防火牆將禁止外界訪問80端口。如下所示。

[html] view plain copy
  1. [root@nginx1 nginx-1.6.2]# vim /etc/sysconfig/iptables  
  2.   
  3. # Firewall configuration written by system-config-firewall  
  4. # Manual customization of this file is not recommended.  
  5. *filter  
  6. :INPUT ACCEPT [0:0]  
  7. :FORWARD ACCEPT [0:0]  
  8. :OUTPUT ACCEPT [0:0]  
  9. -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  
  10. -A INPUT -p icmp -j ACCEPT  
  11. -A INPUT -i lo -j ACCEPT  
  12. -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT  
  13. -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT  
  14. -A INPUT -j REJECT --reject-with icmp-host-prohibited  
  15. -A FORWARD -j REJECT --reject-with icmp-host-prohibited  
  16. COMMIT  
          添加完80端口之後,我們重啓防火牆,如下所示。

[html] view plain copy
  1. [root@nginx1 nginx-1.6.2]# service iptables restart  
  2. iptables:將鏈設置爲政策 ACCEPT:filter                    [確定]  
  3. iptables:清除防火牆規則:                                 [確定]  
  4. iptables:正在卸載模塊:                                   [確定]  
  5. iptables:應用防火牆規則:                                 [確定]  

          8、通過瀏覽器訪問nginx歡迎頁,我們在地址欄輸入:http://192.168.156.11/(80端口不用輸也可以)或http://192.168.156.11:80/,如下圖所示。


五、學習nginx配置

       1、當啓動過nginx之後,我們到/usr/local/nginx目錄下,可以看到有conf、html、logs、sbin四個文件這四個文件是剛解壓後就有的,是nginx重要的文件,還可以看到幾個_temp結尾的文件,這些都是啓動後生成的文件,我們暫且不用去管它們。我們使用cd conf命令進入conf目錄,該目錄下有個nginx.conf文件,這是nginx最重要的文件,我們修改nginx就是修改該文件,如下所示。

[html] view plain copy
  1. [root@nginx1 nginx-1.6.2]# cd /usr/local/nginx  
  2. [root@nginx1 nginx]# ll  
  3. 總用量 36  
  4. drwx------. 2 nobody root 4096 4月   7 05:16 client_body_temp  
  5. drwxr-xr-x. 2 root   root 4096 4月   7 04:37 conf  
  6. drwx------. 2 nobody root 4096 4月   7 05:16 fastcgi_temp  
  7. drwxr-xr-x. 2 root   root 4096 4月   7 04:37 html  
  8. drwxr-xr-x. 2 root   root 4096 4月   7 05:16 logs  
  9. drwx------. 2 nobody root 4096 4月   7 05:16 proxy_temp  
  10. drwxr-xr-x. 2 root   root 4096 4月   7 04:37 sbin  
  11. drwx------. 2 nobody root 4096 4月   7 05:16 scgi_temp  
  12. drwx------. 2 nobody root 4096 4月   7 05:16 uwsgi_temp  
  13. [root@nginx1 nginx]# cd conf  
  14. [root@nginx1 conf]# ll  
  15. 總用量 60  
  16. -rw-r--r--. 1 root root 1034 4月   7 04:37 fastcgi.conf  
  17. -rw-r--r--. 1 root root 1034 4月   7 04:37 fastcgi.conf.default  
  18. -rw-r--r--. 1 root root  964 4月   7 04:37 fastcgi_params  
  19. -rw-r--r--. 1 root root  964 4月   7 04:37 fastcgi_params.default  
  20. -rw-r--r--. 1 root root 2837 4月   7 04:37 koi-utf  
  21. -rw-r--r--. 1 root root 2223 4月   7 04:37 koi-win  
  22. -rw-r--r--. 1 root root 3957 4月   7 04:37 mime.types  
  23. -rw-r--r--. 1 root root 3957 4月   7 04:37 mime.types.default  
  24. -rw-r--r--. 1 root root 2656 4月   7 04:37 nginx.conf  
  25. -rw-r--r--. 1 root root 2656 4月   7 04:37 nginx.conf.default  
  26. -rw-r--r--. 1 root root  596 4月   7 04:37 scgi_params  
  27. -rw-r--r--. 1 root root  596 4月   7 04:37 scgi_params.default  
  28. -rw-r--r--. 1 root root  623 4月   7 04:37 uwsgi_params  
  29. -rw-r--r--. 1 root root  623 4月   7 04:37 uwsgi_params.default  
  30. -rw-r--r--. 1 root root 3610 4月   7 04:37 win-utf  
  31. [root@nginx1 conf]#   
         nginx.conf文件的內容如下(注:我把註釋給加上了):

[html] view plain copy
  1. #user  nobody;  
  2.   
  3. #開啓進程數 <=CPU數   
  4. worker_processes  1;  
  5.   
  6. #錯誤日誌保存位置  
  7. #error_log  logs/error.log;  
  8. #error_log  logs/error.log  notice;  
  9. #error_log  logs/error.log  info;  
  10.   
  11. #進程號保存文件  
  12. #pid        logs/nginx.pid;  
  13.   
  14. #每個進程最大連接數(最大連接=連接數x進程數)每個worker允許同時產生多少個鏈接,默認1024  
  15. events {  
  16.     worker_connections  1024;  
  17. }  
  18.   
  19.   
  20. http {  
  21.     #文件擴展名與文件類型映射表  
  22.     include       mime.types;  
  23.     #默認文件類型  
  24.     default_type  application/octet-stream;  
  25.   
  26.     #日誌文件輸出格式 這個位置相於全局設置  
  27.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  28.     #                  '$status $body_bytes_sent "$http_referer" '  
  29.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  30.   
  31.     #請求日誌保存位置  
  32.     #access_log  logs/access.log  main;  
  33.       
  34.     #打開發送文件  
  35.     sendfile        on;  
  36.     #tcp_nopush     on;  
  37.   
  38.     #keepalive_timeout  0;  
  39.     #連接超時時間  
  40.     keepalive_timeout  65;  
  41.   
  42.     #打開gzip壓縮  
  43.     #gzip  on;  
  44.       
  45.     server {  
  46.         #監聽端口,默認是80端口  
  47.         listen       80;  
  48.         #監聽域名  
  49.         server_name  localhost;  
  50.   
  51.         #charset koi8-r;  
  52.           
  53.         #nginx訪問日誌放在logs/host.access.log下,並且使用main格式(還可以自定義格式)  
  54.         #access_log  logs/host.access.log  main;  
  55.   
  56.         #如果沒有location更明確的匹配訪問路徑的話,訪問請求都會被該location處理。  
  57.         location / {  
  58.             #root指定nginx的根目錄爲/usr/local/nginx/html  
  59.             root   html;  
  60.             #默認訪問文件,歡迎頁先去html目錄下找index.html,如果找不到再去找index.htm  
  61.             index  index.html index.htm;  
  62.         }  
  63.           
  64.         #error_page  404              /404.html;  
  65.         # redirect server error pages to the static page /50x.html  
  66.         #  
  67.           
  68.         #錯誤頁面及其返回地址,錯誤碼爲500、502、503、504都會返回50.html錯誤頁面。  
  69.         error_page   500 502 503 504  /50x.html;  
  70.         #location後面是"="的話,說明是精確匹配  
  71.         location = /50x.html {  
  72.             root   html;  
  73.         }  
  74.   
  75.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  76.         #  
  77.         #location ~ \.php$ {  
  78.         #    proxy_pass   http://127.0.0.1;  
  79.         #}  
  80.   
  81.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  82.         #  
  83.         #location ~ \.php$ {  
  84.         #    root           html;  
  85.         #    fastcgi_pass   127.0.0.1:9000;  
  86.         #    fastcgi_index  index.php;  
  87.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  88.         #    include        fastcgi_params;  
  89.         #}  
  90.   
  91.         # deny access to .htaccess files, if Apache's document root  
  92.         # concurs with nginx's one  
  93.         #  
  94.         #location ~ /\.ht {  
  95.         #    deny  all;  
  96.         #}  
  97.     }  
  98.       
  99.   
  100.     # another virtual host using mix of IP-, name-, and port-based configuration  
  101.     #  
  102.     #server {  
  103.     #    listen       8000;  
  104.     #    listen       somename:8080;  
  105.     #    server_name  somename  alias  another.alias;  
  106.   
  107.     #    location / {  
  108.     #        root   html;  
  109.     #        index  index.html index.htm;  
  110.     #    }  
  111.     #}  
  112.   
  113.   
  114.     # HTTPS server  
  115.     #  
  116.     #server {  
  117.     #    listen       443 ssl;  
  118.     #    server_name  localhost;  
  119.   
  120.     #    ssl_certificate      cert.pem;  
  121.     #    ssl_certificate_key  cert.key;  
  122.   
  123.     #    ssl_session_cache    shared:SSL:1m;  
  124.     #    ssl_session_timeout  5m;  
  125.   
  126.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  127.     #    ssl_prefer_server_ciphers  on;  
  128.   
  129.     #    location / {  
  130.     #        root   html;  
  131.     #        index  index.html index.htm;  
  132.     #    }  
  133.     #}  
  134.   
  135. }  
       其實那些註釋掉的信息我們都可以刪掉,這樣看起來就會很簡潔明瞭了。配置文件裏可以添加多個server,server監聽的端口不同,我們可以根據需要讓nginx代理多個端口,當訪問某個端口的時候,指定去做某些事情。我這裏添加了一個server,這個server監聽的端口爲1234,server_name我指定爲了test.com,也就是域名爲test.com,當訪問1234端口時會自動導航到/usr/local/nginx/tester/tester111.html頁面,如下所示。

[html] view plain copy
  1. #user  nobody;  
  2.   
  3. #開啓進程數 <=CPU數   
  4. worker_processes  1;  
  5.   
  6. #錯誤日誌保存位置  
  7. #error_log  logs/error.log;  
  8. #error_log  logs/error.log  notice;  
  9. #error_log  logs/error.log  info;  
  10.   
  11. #進程號保存文件  
  12. #pid        logs/nginx.pid;  
  13.   
  14. #每個進程最大連接數(最大連接=連接數x進程數)每個worker允許同時產生多少個鏈接,默認1024  
  15. events {  
  16.     worker_connections  1024;  
  17. }  
  18.   
  19.   
  20. http {  
  21.     #文件擴展名與文件類型映射表  
  22.     include       mime.types;  
  23.     #默認文件類型  
  24.     default_type  application/octet-stream;  
  25.   
  26.     #日誌文件輸出格式 這個位置相於全局設置  
  27.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  28.     #                  '$status $body_bytes_sent "$http_referer" '  
  29.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  30.   
  31.     #請求日誌保存位置  
  32.     #access_log  logs/access.log  main;  
  33.       
  34.     #打開發送文件  
  35.     sendfile        on;  
  36.     #tcp_nopush     on;  
  37.   
  38.     #keepalive_timeout  0;  
  39.     #連接超時時間  
  40.     keepalive_timeout  65;  
  41.   
  42.     #打開gzip壓縮  
  43.     #gzip  on;  
  44.       
  45.     server {  
  46.         #監聽端口  
  47.         listen       80;  
  48.         #監聽域名  
  49.         server_name  localhost;  
  50.   
  51.         #charset koi8-r;  
  52.           
  53.         #nginx訪問日誌放在logs/host.access.log下,並且使用main格式(還可以自定義格式)  
  54.         #access_log  logs/host.access.log  main;  
  55.   
  56.         #如果沒有location更明確的匹配訪問路徑的話,訪問請求都會被該location處理。  
  57.         location / {  
  58.             #root指定nginx的根目錄爲/usr/local/nginx/html  
  59.             root   html;  
  60.             #默認訪問文件,歡迎頁先去html目錄下找index.html,如果找不到再去找index.htm  
  61.             index  index.html index.htm;  
  62.         }  
  63.           
  64.         #error_page  404              /404.html;  
  65.         # redirect server error pages to the static page /50x.html  
  66.         #  
  67.           
  68.         #錯誤頁面及其返回地址,錯誤碼爲500、502、503、504都會返回50.html錯誤頁面。  
  69.         error_page   500 502 503 504  /50x.html;  
  70.         #location後面是"="的話,說明是精確匹配  
  71.         location = /50x.html {  
  72.             root   html;  
  73.         }  
  74.   
  75.         server {  
  76.             listen 1234;  
  77.             server_name test.com;  
  78.             location / {  
  79.                 #正則表達式匹配uri方式:在/usr/local/nginx/tester下 建立一個tester111.html 然後使用正則匹配  
  80.                 root tester;  
  81.                 index tester111.html;  
  82.             }  
  83.         }  
  84.     }  
  85. }  
       我們需要在/usr/local/nginx/目錄下新建tester目錄,在tester目錄下新建一個tester111.html文件,在文件中隨便寫段html代碼主要是爲了判斷是否訪問的是我們寫的html頁面。如下所示。

[html] view plain copy
  1. [root@nginx1 nginx]# mkdir tester  
  2. [root@nginx1 nginx]# ll  
  3. 總用量 40  
  4. drwx------. 2 nobody root 4096 4月   7 05:16 client_body_temp  
  5. drwxr-xr-x. 2 root   root 4096 4月   7 06:32 conf  
  6. drwx------. 2 nobody root 4096 4月   7 05:16 fastcgi_temp  
  7. drwxr-xr-x. 2 root   root 4096 4月   7 04:37 html  
  8. drwxr-xr-x. 2 root   root 4096 4月   7 05:16 logs  
  9. drwx------. 2 nobody root 4096 4月   7 05:16 proxy_temp  
  10. drwxr-xr-x. 2 root   root 4096 4月   7 04:37 sbin  
  11. drwx------. 2 nobody root 4096 4月   7 05:16 scgi_temp  
  12. drwxr-xr-x. 2 root   root 4096 4月   7 06:37 tester  
  13. drwx------. 2 nobody root 4096 4月   7 05:16 uwsgi_temp  
  14. [root@nginx1 nginx]# cd tester/  
  15. [root@nginx1 tester]# vim tester111.html  
  16.   
  17. <html>  
  18.    <body>Hello This is Nginx Test!</body>  
  19. </html>  
         下面我們需要在防火牆中添加1234端口從而讓瀏覽器可以訪問1234端口,這個步驟在上面寫的清楚了,這裏就不再囉嗦了。

         在防火牆配置中添加完1234端口後,我們需要重啓nginx,如下所示。

[html] view plain copy
  1. [root@nginx1 nginx]# /usr/local/nginx/sbin/nginx -s reload  
  2. [root@nginx1 nginx]# ps -ef|grep nginx  
  3. root       3654      1  0 05:16 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx  
  4. nobody     3870   3654  0 06:47 ?        00:00:00 nginx: worker process        
  5. root       3872   1368  0 06:47 pts/0    00:00:00 grep nginx  
  6. [root@nginx1 nginx]#   
          由於我們在配置文件中配置的域名test.com並不是真實存在的,我們要想讓windows系統識別該“域名”,我們需要修改下HOST文件,如下圖所示。


          下面我們在地址欄輸入:http://test.com:1234/便可以訪問到我們創建的tester111.html文件的內容了,如下圖所示,這說明我們配置的server是生效的。



          2.下面我們再來說下logs,nginx的log文件存在的目錄爲/usr/local/nginx/logs目錄下,在logs目錄下有三個文件,分別是access.log,error.log,nginx.pid,access.log很明顯,就是記錄瀏覽器訪問的記錄,error.log就是記錄錯誤日誌,nginx.pid則是記錄當前的進程號,如下所示。

[html] view plain copy
  1. [root@nginx1 conf]# cd /usr/local/nginx/  
  2. [root@nginx1 nginx]# ll  
  3. 總用量 40  
  4. drwx------. 2 nobody root 4096 4月   7 05:16 client_body_temp  
  5. drwxr-xr-x. 2 root   root 4096 4月   7 07:00 conf  
  6. drwx------. 2 nobody root 4096 4月   7 05:16 fastcgi_temp  
  7. drwxr-xr-x. 2 root   root 4096 4月   7 04:37 html  
  8. drwxr-xr-x. 2 root   root 4096 4月   7 05:16 logs  
  9. drwx------. 2 nobody root 4096 4月   7 05:16 proxy_temp  
  10. drwxr-xr-x. 2 root   root 4096 4月   7 04:37 sbin  
  11. drwx------. 2 nobody root 4096 4月   7 05:16 scgi_temp  
  12. drwxr-xr-x. 2 root   root 4096 4月   7 06:45 tester  
  13. drwx------. 2 nobody root 4096 4月   7 05:16 uwsgi_temp  
  14. [root@nginx1 nginx]# cd logs/  
  15. [root@nginx1 logs]# ll  
  16. 總用量 16  
  17. -rw-r--r--. 1 root root 4121 4月   7 07:01 access.log  
  18. -rw-r--r--. 1 root root 3435 4月   7 07:01 error.log  
  19. -rw-r--r--. 1 root root    5 4月   7 05:16 nginx.pid  
  20. [root@nginx1 logs]#   
          我們可以查看下nginx.pid文件的內容,然後再查看當前nginx的進程,發現兩個進程號都是3654。這個文件的一個用處就是當需要殺掉當前nginx進程的時候,可以使用shell腳本去nginx.pid文件中獲取到該進程號然後再使用kill -9 pid進程號命令殺掉nginx。

[html] view plain copy
  1. [root@nginx1 logs]# cat nginx.pid  
  2. 3654  
  3. [root@nginx1 logs]# ps -ef | grep nginx  
  4. root       3654      1  0 05:16 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx  
  5. nobody     4085   3654  0 07:01 ?        00:00:00 nginx: worker process        
  6. root       4090   1368  0 07:23 pts/0    00:00:00 grep nginx  
  7. [root@nginx1 logs]#   
          logs目錄下的access.log和error.log兩個文件都是在nginx啓動的時候自動生成的,我們不能刪掉nginx.pid否則啓動nginx會報錯!!如果你人爲刪掉access.log和error.log文件,需要重啓nginx才能重新生成,訪問頁面是無法生成這兩個文件的。
[html] view plain copy
  1. [root@nginx1 logs]# ls  
  2. nginx.pid  
  3. [root@nginx1 logs]# ls  
  4. nginx.pid  
  5. [root@nginx1 logs]# /usr/local/nginx/sbin/nginx -s reload  
  6. [root@nginx1 logs]# ll  
  7. 總用量 8  
  8. -rw-r--r--. 1 root root  0 4月   7 07:30 access.log  
  9. -rw-r--r--. 1 root root 60 4月   7 07:30 error.log  
  10. -rw-r--r--. 1 root root  5 4月   7 05:16 nginx.pid  
  11. [root@nginx1 logs]#   
        我們可以在不同的server配置不同的log文件,我在80端口所在的server還是使用默認的這個access.log,在1234端口所在的server使用test.com.log。如下圖所示。



       爲了不讓原來log文件中的內容影響我們的結果,我們先把access.log和error.log文件刪除,由於修改了配置文件並且要生成log文件,因此我們需要重啓nginx,重啓後,我們到/usr/local/nginx/logs/目錄下查看生成的日誌文件,如下所示。可以看到已經生成我說的幾個日誌文件了,這時候日誌文件都是空的。

[html] view plain copy
  1. [root@nginx1 conf]# /usr/local/nginx/sbin/nginx -s reload  
  2. [root@nginx1 conf]# ps -ef | grep nginx  
  3. root       3654      1  0 05:16 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx  
  4. nobody     4141   3654  0 08:01 ?        00:00:00 nginx: worker process        
  5. root       4143   1368  0 08:02 pts/0    00:00:00 grep nginx  
  6. [root@nginx1 conf]# cd /usr/local/nginx/logs/  
  7. [root@nginx1 logs]# ll  
  8. 總用量 8  
  9. -rw-r--r--. 1 root root  0 4月   7 08:01 access.log  
  10. -rw-r--r--. 1 root root 60 4月   7 08:01 error.log  
  11. -rw-r--r--. 1 root root  5 4月   7 05:16 nginx.pid  
  12. -rw-r--r--. 1 root root  0 4月   7 08:01 test.com.log  
  13. [root@nginx1 logs]#   
         我們可以在瀏覽器訪問一下1234端口對應的server,訪問之後,我們查看test.com.log文件,可以看到一條訪問記錄,提示IP爲192.168.156.100(我本機配置與虛擬機同一網段的IP)訪問了一次該服務。

[html] view plain copy
  1. [root@nginx1 logs]# cat test.com.log   
  2. 192.168.156.100 - - [07/Apr/2017:08:04:23 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; LCTE)" "-"  
  3. [root@nginx1 logs]#   
         我們再訪問下80端口服務,然後查看access.log文件,如下圖所示,也查看到了我們的訪問記錄。由於瀏覽器有緩存,當我們第二次或更多次刷新頁面的時候日誌不會累加。想要每次刷新頁面都有日誌信息的話,可以設置讓瀏覽器不保存緩存信息,大家可以參考http://blog.csdn.net/u012453843/article/details/69499563這篇博客進行設置。


         下面來說下日誌切割,我們在日常生活中,對nginx日誌的分析非常的重要,通常需要運維去對nginx的日誌進行切割和分析處理。比如實現一個定時任務,去處理nginx日誌等。爲什麼要進行日誌拆分?這是因爲對於一個訪問量大的網站來說,日誌的增長是非常快的,如果都放在一個文件當中,查看將非常不方便,不利於問題的解決,我們根據公司的實際情況定時對日誌進行拆分,像天貓、京東這樣的大型互聯網站,每隔多少分鐘可能就需要進行一次拆分,對於一般的公司來說,可以每小時或者每天拆分一次。

          分割日誌,我們肯定不能靠人工去分割,我們採用的是shell腳本來進行分割,做法就是先把日誌文件移動到另一個地方並根據時間命名,然後再重新生成一個新的日誌文件。由於需要定時做分割,因此我們在contab裏面定時去調用shell腳本

          首先我們在/usr/local/nginx/sbin/目錄下創建一個log.sh的腳本,如下所示。

[html] view plain copy
  1. [root@nginx1 sbin]# pwd  
  2. /usr/local/nginx/sbin  
  3. [root@nginx1 sbin]# vim log.sh  

           log.sh腳本內容如下:

[html] view plain copy
  1. #基礎路徑:/usr/local/nginx  
  2. BASE_DIR=/usr/local/nginx  
  3. #原文件名稱:test.com.access.log  
  4. BASE_FILE_NAME=test.com.access.log  
  5.   
  6. #當前日誌文件路徑:/usr/local/nginx/logs  
  7. CURRENT_PATH=$BASE_DIR/logs  
  8. #備份日誌文件地址:/usr/local/nginx/datalogs  
  9. BAK_PATH=$BASE_DIR/datalogs  
  10.   
  11. #當前日誌文件:/usr/local/nginx/logs/test.com.access.log  
  12. CURRENT_FILE=$CURRENT_PATH/$BASE_FILE_NAME  
  13. #備份今天的日誌,爲了方便測試我們的定時器功能,這裏把時間精確到分鐘,名稱如:201704071720  
  14. BAK_TIME=`/bin/date -d today +%Y%m%d%H%M`  
  15. #備份文件:/usr/local/nginx/datalogs/201704071720-test.com.access.log  
  16. BAK_FILE=$BAK_PATH/$BAK_TIME-$BASE_FILE_NAME  
  17. #輸出備份文件信息  
  18. echo $BAK_FILE  
  19.   
  20. #備份之前先關閉nginx,關閉命令:/usr/local/nginx/sbin/nginx -s stop  
  21. $BASE_DIR/sbin/nginx -s stop  
  22.   
  23. #將/usr/local/nginx/logs/test.com.access.log移動到/usr/local/nginx/datalogs/201704071720-test.com.access.log  
  24. mv $CURRENT_FILE $BAK_FILE  
  25. #啓動nginx,命令:/usr/local/nginx/sbin/nginx,重啓之後會自動再生成一個test.com.access.log文件  
  26. $BASE_DIR/sbin/nginx  
       由於我們在腳本中設置了日誌備份目錄datalogs,而該目錄目前還沒有創建,因此我們需要創建該目錄,如下所示。

[html] view plain copy
  1. [root@nginx1 nginx]# mkdir datalogs  
  2. [root@nginx1 nginx]# ll  
  3. 總用量 40  
  4. drwx------. 2 nobody root 4096 4月   7 02:59 client_body_temp  
  5. drwxr-xr-x. 2 root   root 4096 4月   7 02:58 conf  
  6. drwxr-xr-x. 2 root   root 4096 4月   7 08:41 datalogs  
  7. drwx------. 2 nobody root 4096 4月   7 02:59 fastcgi_temp  
  8. drwxr-xr-x. 2 root   root 4096 4月   7 02:58 html  
  9. drwxr-xr-x. 2 root   root 4096 4月   7 02:59 logs  
  10. drwx------. 2 nobody root 4096 4月   7 02:59 proxy_temp  
  11. drwxr-xr-x. 2 root   root 4096 4月   7 02:58 sbin  
  12. drwx------. 2 nobody root 4096 4月   7 02:59 scgi_temp  
  13. drwx------. 2 nobody root 4096 4月   7 02:59 uwsgi_temp  
  14. [root@nginx1 nginx]#   
         在定時調用log.sh腳本文件之前,我們需要給予該腳本文件執行權限,我們先看看我們創建的log.sh默認是什麼權限,如下所示,可以卡到,只有讀的權限。

[html] view plain copy
  1. [root@nginx1 sbin]# ll  
  2. 總用量 3048  
  3. -rw-r--r--. 1 root root    1016 4月   7 09:11 log.sh  
  4. -rwxr-xr-x. 1 root root 3115344 4月   7 02:58 nginx  
  5. [root@nginx1 sbin]#   

        我們給log.sh文件賦予所有權限,如下所示。

[html] view plain copy
  1. [root@nginx1 sbin]# chmod 777 log.sh   
  2. [root@nginx1 sbin]# ll  
  3. 總用量 3048  
  4. -rwxrwxrwx. 1 root root    1016 4月   7 10:12 log.sh  
  5. -rwxr-xr-x. 1 root root 3115344 4月   7 02:58 nginx  
  6. [root@nginx1 sbin]#   
 

       下面我們便使用crontab -e命令去設置定時任務,如下所示。

[html] view plain copy
  1. #每天凌晨兩點備份前一天的日誌  
  2. #* 2 * * * sh /usr/local/nginx/sbin/log.sh  
  3. #爲了測試方便,我們暫且設置成每分鐘執行一次備份  
  4. */1 * * * * sh /usr/local/nginx/sbin/log.sh  
         設置好定時任務之後,保存退出,這樣每隔一分鐘便會執行一次log.sh腳本,同時會生成一個備份文件,我們到/usr/local/nginx/datalogs目錄下進行查看,如下所示,可以看到,每隔一分鐘便會有一個備份文件產生,命名規則與我們設置的完全一致。

[html] view plain copy
  1. [root@nginx1 sbin]# cd /usr/local/nginx/datalogs/  
  2. You have new mail in /var/spool/mail/root  
  3. [root@nginx1 datalogs]# ll  
  4. 總用量 0  
  5. -rw-r--r--. 1 root root 0 4月   7 10:12 201704071013-test.com.access.log  
  6. -rw-r--r--. 1 root root 0 4月   7 10:13 201704071014-test.com.access.log  
  7. -rw-r--r--. 1 root root 0 4月   7 10:14 201704071015-test.com.access.log  
  8. -rw-r--r--. 1 root root 0 4月   7 10:15 201704071016-test.com.access.log  
  9. -rw-r--r--. 1 root root 0 4月   7 10:16 201704071017-test.com.access.log  
  10. -rw-r--r--. 1 root root 0 4月   7 10:17 201704071018-test.com.access.log  
  11. -rw-r--r--. 1 root root 0 4月   7 10:18 201704071019-test.com.access.log  
  12. -rw-r--r--. 1 root root 0 4月   7 10:19 201704071020-test.com.access.log  
  13. -rw-r--r--. 1 root root 0 4月   7 10:20 201704071021-test.com.access.log  
  14. -rw-r--r--. 1 root root 0 4月   7 10:21 201704071022-test.com.access.log  
  15. -rw-r--r--. 1 root root 0 4月   7 10:22 201704071023-test.com.access.log  
  16. -rw-r--r--. 1 root root 0 4月   7 10:23 201704071024-test.com.access.log  
  17. [root@nginx1 datalogs]#   
          如果發現沒有備份文件產生,那肯定是有錯誤了,那麼怎麼看錯誤信息呢?其實系統已經告訴我們了,如下所示,提示我們去/var/spool/mail/root文件中進行查看。
[html] view plain copy
  1. [root@nginx1 sbin]# cd /usr/local/nginx/datalogs/  
  2. You have new mail in /var/spool/mail/root  

          比如我在剛開始的時候把獲取日期的那行的`搞成'了,可以看到,該文件中明確告訴我們是日期那行出了錯誤。

          

      3.nginx配置實現動靜分離

       對於nginx來說,實現動靜分離是件非常容易的事,我們只需要配置多個location便可以達到這個目的。location的基礎語法有三種:

1.location = pattern {}精確匹配

2.location pattern {} 一般匹配

3.location ~ pattern {} 正則匹配

        下面我們就使用正則表達式舉個例子,我們打開nginx.conf配置文件,並修改端口爲1234的server的location,如下所示,用來匹配以tester開頭的文件。

[html] view plain copy
  1. server {  
  2.         listen 1234;  
  3.         server_name test.com;  
  4.         location ~ tester {  
  5.             root tester;  
  6.             index tester111.html;  
  7.         }  
  8.         access_log  logs/test.com.log  main;  
  9.     }  
       修改完之後,我們重啓nginx。

[html] view plain copy
  1. [root@nginx1 nginx]# /usr/local/nginx/sbin/nginx -s reload  
       重啓nginx之後,我們這時再訪問http://test.com:1234的話,將不能訪問到tester111.html了,而是訪問到index.html(即nginx歡迎頁了),如下圖所示。出現這種情況的原因就是我們輸入http://test.com:1234後,它會去匹配location /{},結果發現在端口爲1234的server當中沒有location /{},於是便會去試着找其它有location /{}的server,發現80端口可以匹配成功,因此便去訪問80端口所對應的歡迎頁了。      

           當我們在地址欄輸入http://test.com:1234/tester111.html時,頁面如下圖所示。這時之所以可以訪問到/usr/local/nginx/tester目錄下的tester111.thtml,就是因爲我們在端口爲1234的server當中設置了正則表達式即:location ~ tester {},它可以匹配以tester開頭的請求,而且必須注意的是,訪問的文件必須真實存在,比如訪問tester111.html文件可以,這是因爲在tester目錄下確實有該文件,如果訪問tester123.html就會報404,因爲找不到這麼一個文件。那麼爲什麼這時不去訪問80端口的歡迎頁了呢?這是因爲此時地址欄輸入的請求信息是要具體訪問某個文件了,不適合再導向歡迎頁。


      下面再說下在nginx中if條件的使用,if(條件爲:=~~*)、return、break、rewrite。-f是否爲文件、-d是否爲目錄、-e是否存在。我們就舉個if條件的例子,我們在nginx.conf文件的1234所對應的server的location當中添加if條件,如下所示,添加的判斷是如果發起請求的IP地址是192.168.156.100(我本地的IP地址)的話,就返回401錯誤。

[html] view plain copy
  1. server {  
  2.         listen 1234;  
  3.         server_name test.com;  
  4.         location ~ tester {  
  5.             if ($remote_addr = 192.168.156.100) {  
  6.                return 401;  
  7.             }  
  8.             root tester;  
  9.             index tester111.html;  
  10.         }  
  11.         access_log  logs/test.com.log  main;  
  12.     }  
       修改完配置文件,我們重啓nginx

[html] view plain copy
  1. [root@nginx1 conf]# /usr/local/nginx/sbin/nginx -s reload  
       重啓完nginx之後,我們再訪問http://test.com:1234/tester111.html這個地址就會出現如下圖所示的結果。這說明我們配置的攔截條件生效了。

       下面再舉個if條件的例子,我們把if條件改成如下所示的樣子,它的意思是訪問的瀏覽器如果是chrome瀏覽器的話,不管你是什麼請求都定位到chrome.html頁面,然後break跳出(如果不加break的話,每次請求過來都會重新讓瀏覽器發起重定向請求,而且永遠不會結束,從而無法真正訪問到chrome.html頁面,導致提示404錯誤)。

[html] view plain copy
  1. server {  
  2.         listen 1234;  
  3.         server_name test.com;  
  4.         location ~ tester {  
  5.             if ($http_user_agent ~* chrome) {  
  6.                rewrite ^.*$ /chrome.html;  
  7.                break;  
  8.             }  
  9.             root tester;  
  10.             index tester111.html;  
  11.         }  
  12.         access_log  logs/test.com.log  main;  
  13.     }  
       既然要重定向到chrome.html頁面,那麼我們便需要先創建該文件,如下所示。

[html] view plain copy
  1. [root@nginx1 nginx]# cd tester/  
  2. [root@nginx1 tester]# pwd  
  3. /usr/local/nginx/tester  
  4. [root@nginx1 tester]# ll  
  5. 總用量 4  
  6. -rw-r--r--. 1 root root 57 4月   7 06:45 tester111.html  
  7. [root@nginx1 tester]# vim chrome.html  
  8.   
  9. <html>  
  10. <body>Chrome Page!!!!</body>  
  11. </html>  
       既然修改了nginx.conf文件,我們便需要重啓nginx

[html] view plain copy
  1. [root@nginx1 tester]# /usr/local/nginx/sbin/nginx -s reload  
      重啓之後,我們再訪問http://test.com:1234/tester111.html,這時訪問結果如下圖所示,可以看到請求被成功重定向到chrome.html頁面了。


      那麼爲何可以達到這個效果呢?我們來看看谷歌瀏覽器的請求日誌信息,如下圖所示,可以看到谷歌瀏覽器的請求信息中有"Chrome"信息,我們的if ($http_user_agent ~* chrome)條件判斷中使用了正則表達式,* chrome可以忽略大小寫,所以"Chrome"也可以匹配"chrome",既然瀏覽器匹配,就進入到了if條件當中,rewrite ^.*$ /chrome.html;這句話的意思是不論你要請求什麼資源,都給你定向到chrome.html文件,因此我們纔看到了chrome.html文件的內容。


      下面再舉個例子,我們在端口爲1234的server當中再加一個location,匹配goods,如下所示。

[html] view plain copy
  1. server {  
  2.         listen 1234;  
  3.         server_name test.com;  
  4.         location ~ tester {  
  5.             if ($http_user_agent ~* chrome) {  
  6.                rewrite ^.*$ /chrome.html;  
  7.                break;  
  8.             }  
  9.             root tester;  
  10.             index tester111.html;  
  11.         }  
  12.   
  13.         location /goods {  
  14.              rewrite "goods-(\d{1,5})\.html" /goods-ctrl.html;  
  15.              root tester;  
  16.              index tester111.html;  
  17.          }  
  18.         access_log  logs/test.com.log  main;  
  19.     }  
       我們還是得在/usr/local/nginx/tester目錄下創建一下goods-ctrl.html文件,如下所示。

[html] view plain copy
  1. [root@nginx1 conf]# cd /usr/local/nginx/tester/  
  2. [root@nginx1 tester]# ll  
  3. 總用量 8  
  4. -rw-r--r--. 1 root root 44 4月   8 05:52 chrome.html  
  5. -rw-r--r--. 1 root root 57 4月   7 06:45 tester111.html  
  6. [root@nginx1 tester]# vim goods-ctrl.html  
  7.   
  8. <html>  
  9.   <body>Goods Page!!!!</body>  
  10. </html>  
         由於修改了nginx.conf文件,因此需要重啓nginx

[html] view plain copy
  1. [root@nginx1 tester]# /usr/local/nginx/sbin/nginx -s reload  

         重啓之後,我們在瀏覽器輸入goods-後面跟0到5位的數字然後是.html,這樣才能匹配重定向條件,會被重定向到goods-ctrl.html頁面。這個例子在真實項目中是非常常見的,Rewrite最主要的作用就是對URL進行重寫,即重定向。舉個簡單的例子,我們用電腦打開淘寶顯示出的頁面與手機打開顯示出的頁面,或者是IE與Chrome瀏覽器打開的頁面,有着特別大的差別,這就是使用了Rewrite模塊,爲用戶提供最合適的頁面。


      nginx還可以對數據進行壓縮,對一些圖片、html、css、js等文件進行緩存、從而實現動靜分離等等優化功能,在網站做優化的時候非常的有用。

      所謂的動靜分離,通過上面的例子,我們完全可以將動態的請求都交給tomcat處理,靜態的請求都交給nginx來處理,這是非常容易做到的事情。

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