一、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。
- yum install wget
- yum install vim-enhanced
- yum install make cmake gcc gcc-c++
1、下載nginx安裝包
- [root@nginx1 ~]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
- --2017-04-07 01:44:55-- http://nginx.org/download/nginx-1.6.2.tar.gz
- 正在解析主機 nginx.org... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ...
- 正在連接 nginx.org|206.251.255.63|:80... 已連接。
- 已發出 HTTP 請求,正在等待迴應... 200 OK
- 長度:804164 (785K) [application/octet-stream]
- 正在保存至: “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庫。
- yum install -y pcre pcre-devel
- yum install -y zlib zlib-devel
- yum install -y openssl openssl-devel
- [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、編譯安裝
- [root@nginx1 nginx-1.6.2]# make && make install
- [root@nginx1 nginx-1.6.2]# /usr/local/nginx/sbin/nginx
- [root@nginx1 nginx-1.6.2]# ps -ef | grep nginx
- root 3640 1 0 04:40 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
- nobody 3641 3640 0 04:40 ? 00:00:00 nginx: worker process
- root 3643 1368 0 04:40 pts/0 00:00:00 grep nginx
- [root@nginx1 nginx-1.6.2]#
- [root@nginx1 nginx-1.6.2]# /usr/local/nginx/sbin/nginx -s stop
- [root@nginx1 nginx-1.6.2]# /usr/local/nginx/sbin/nginx -s reload
- [root@nginx1 nginx-1.6.2]# vim /etc/sysconfig/iptables
- # Firewall configuration written by system-config-firewall
- # Manual customization of this file is not recommended.
- *filter
- :INPUT ACCEPT [0:0]
- :FORWARD ACCEPT [0:0]
- :OUTPUT ACCEPT [0:0]
- -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- -A INPUT -p icmp -j ACCEPT
- -A INPUT -i lo -j ACCEPT
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
- -A INPUT -j REJECT --reject-with icmp-host-prohibited
- -A FORWARD -j REJECT --reject-with icmp-host-prohibited
- COMMIT
- [root@nginx1 nginx-1.6.2]# service iptables restart
- iptables:將鏈設置爲政策 ACCEPT:filter [確定]
- iptables:清除防火牆規則: [確定]
- iptables:正在卸載模塊: [確定]
- 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就是修改該文件,如下所示。
- [root@nginx1 nginx-1.6.2]# cd /usr/local/nginx
- [root@nginx1 nginx]# ll
- 總用量 36
- drwx------. 2 nobody root 4096 4月 7 05:16 client_body_temp
- drwxr-xr-x. 2 root root 4096 4月 7 04:37 conf
- drwx------. 2 nobody root 4096 4月 7 05:16 fastcgi_temp
- drwxr-xr-x. 2 root root 4096 4月 7 04:37 html
- drwxr-xr-x. 2 root root 4096 4月 7 05:16 logs
- drwx------. 2 nobody root 4096 4月 7 05:16 proxy_temp
- drwxr-xr-x. 2 root root 4096 4月 7 04:37 sbin
- drwx------. 2 nobody root 4096 4月 7 05:16 scgi_temp
- drwx------. 2 nobody root 4096 4月 7 05:16 uwsgi_temp
- [root@nginx1 nginx]# cd conf
- [root@nginx1 conf]# ll
- 總用量 60
- -rw-r--r--. 1 root root 1034 4月 7 04:37 fastcgi.conf
- -rw-r--r--. 1 root root 1034 4月 7 04:37 fastcgi.conf.default
- -rw-r--r--. 1 root root 964 4月 7 04:37 fastcgi_params
- -rw-r--r--. 1 root root 964 4月 7 04:37 fastcgi_params.default
- -rw-r--r--. 1 root root 2837 4月 7 04:37 koi-utf
- -rw-r--r--. 1 root root 2223 4月 7 04:37 koi-win
- -rw-r--r--. 1 root root 3957 4月 7 04:37 mime.types
- -rw-r--r--. 1 root root 3957 4月 7 04:37 mime.types.default
- -rw-r--r--. 1 root root 2656 4月 7 04:37 nginx.conf
- -rw-r--r--. 1 root root 2656 4月 7 04:37 nginx.conf.default
- -rw-r--r--. 1 root root 596 4月 7 04:37 scgi_params
- -rw-r--r--. 1 root root 596 4月 7 04:37 scgi_params.default
- -rw-r--r--. 1 root root 623 4月 7 04:37 uwsgi_params
- -rw-r--r--. 1 root root 623 4月 7 04:37 uwsgi_params.default
- -rw-r--r--. 1 root root 3610 4月 7 04:37 win-utf
- [root@nginx1 conf]#
- #user nobody;
- #開啓進程數 <=CPU數
- worker_processes 1;
- #錯誤日誌保存位置
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #進程號保存文件
- #pid logs/nginx.pid;
- #每個進程最大連接數(最大連接=連接數x進程數)每個worker允許同時產生多少個鏈接,默認1024
- events {
- worker_connections 1024;
- }
- http {
- #文件擴展名與文件類型映射表
- include mime.types;
- #默認文件類型
- default_type application/octet-stream;
- #日誌文件輸出格式 這個位置相於全局設置
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- #請求日誌保存位置
- #access_log logs/access.log main;
- #打開發送文件
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- #連接超時時間
- keepalive_timeout 65;
- #打開gzip壓縮
- #gzip on;
- server {
- #監聽端口,默認是80端口
- listen 80;
- #監聽域名
- server_name localhost;
- #charset koi8-r;
- #nginx訪問日誌放在logs/host.access.log下,並且使用main格式(還可以自定義格式)
- #access_log logs/host.access.log main;
- #如果沒有location更明確的匹配訪問路徑的話,訪問請求都會被該location處理。
- location / {
- #root指定nginx的根目錄爲/usr/local/nginx/html
- root html;
- #默認訪問文件,歡迎頁先去html目錄下找index.html,如果找不到再去找index.htm
- index index.html index.htm;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- #錯誤頁面及其返回地址,錯誤碼爲500、502、503、504都會返回50.html錯誤頁面。
- error_page 500 502 503 504 /50x.html;
- #location後面是"="的話,說明是精確匹配
- location = /50x.html {
- root html;
- }
- # proxy the PHP scripts to Apache listening on 127.0.0.1:80
- #
- #location ~ \.php$ {
- # proxy_pass http://127.0.0.1;
- #}
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- #location ~ \.php$ {
- # root html;
- # fastcgi_pass 127.0.0.1:9000;
- # fastcgi_index index.php;
- # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- # include fastcgi_params;
- #}
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- #location ~ /\.ht {
- # deny all;
- #}
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server {
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- # HTTPS server
- #
- #server {
- # listen 443 ssl;
- # server_name localhost;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_cache shared:SSL:1m;
- # ssl_session_timeout 5m;
- # ssl_ciphers HIGH:!aNULL:!MD5;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
- #user nobody;
- #開啓進程數 <=CPU數
- worker_processes 1;
- #錯誤日誌保存位置
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #進程號保存文件
- #pid logs/nginx.pid;
- #每個進程最大連接數(最大連接=連接數x進程數)每個worker允許同時產生多少個鏈接,默認1024
- events {
- worker_connections 1024;
- }
- http {
- #文件擴展名與文件類型映射表
- include mime.types;
- #默認文件類型
- default_type application/octet-stream;
- #日誌文件輸出格式 這個位置相於全局設置
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- #請求日誌保存位置
- #access_log logs/access.log main;
- #打開發送文件
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- #連接超時時間
- keepalive_timeout 65;
- #打開gzip壓縮
- #gzip on;
- server {
- #監聽端口
- listen 80;
- #監聽域名
- server_name localhost;
- #charset koi8-r;
- #nginx訪問日誌放在logs/host.access.log下,並且使用main格式(還可以自定義格式)
- #access_log logs/host.access.log main;
- #如果沒有location更明確的匹配訪問路徑的話,訪問請求都會被該location處理。
- location / {
- #root指定nginx的根目錄爲/usr/local/nginx/html
- root html;
- #默認訪問文件,歡迎頁先去html目錄下找index.html,如果找不到再去找index.htm
- index index.html index.htm;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- #錯誤頁面及其返回地址,錯誤碼爲500、502、503、504都會返回50.html錯誤頁面。
- error_page 500 502 503 504 /50x.html;
- #location後面是"="的話,說明是精確匹配
- location = /50x.html {
- root html;
- }
- server {
- listen 1234;
- server_name test.com;
- location / {
- #正則表達式匹配uri方式:在/usr/local/nginx/tester下 建立一個tester111.html 然後使用正則匹配
- root tester;
- index tester111.html;
- }
- }
- }
- }
- [root@nginx1 nginx]# mkdir tester
- [root@nginx1 nginx]# ll
- 總用量 40
- drwx------. 2 nobody root 4096 4月 7 05:16 client_body_temp
- drwxr-xr-x. 2 root root 4096 4月 7 06:32 conf
- drwx------. 2 nobody root 4096 4月 7 05:16 fastcgi_temp
- drwxr-xr-x. 2 root root 4096 4月 7 04:37 html
- drwxr-xr-x. 2 root root 4096 4月 7 05:16 logs
- drwx------. 2 nobody root 4096 4月 7 05:16 proxy_temp
- drwxr-xr-x. 2 root root 4096 4月 7 04:37 sbin
- drwx------. 2 nobody root 4096 4月 7 05:16 scgi_temp
- drwxr-xr-x. 2 root root 4096 4月 7 06:37 tester
- drwx------. 2 nobody root 4096 4月 7 05:16 uwsgi_temp
- [root@nginx1 nginx]# cd tester/
- [root@nginx1 tester]# vim tester111.html
- <html>
- <body>Hello This is Nginx Test!</body>
- </html>
在防火牆配置中添加完1234端口後,我們需要重啓nginx,如下所示。
- [root@nginx1 nginx]# /usr/local/nginx/sbin/nginx -s reload
- [root@nginx1 nginx]# ps -ef|grep nginx
- root 3654 1 0 05:16 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
- nobody 3870 3654 0 06:47 ? 00:00:00 nginx: worker process
- root 3872 1368 0 06:47 pts/0 00:00:00 grep nginx
- [root@nginx1 nginx]#
下面我們在地址欄輸入: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則是記錄當前的進程號,如下所示。
- [root@nginx1 conf]# cd /usr/local/nginx/
- [root@nginx1 nginx]# ll
- 總用量 40
- drwx------. 2 nobody root 4096 4月 7 05:16 client_body_temp
- drwxr-xr-x. 2 root root 4096 4月 7 07:00 conf
- drwx------. 2 nobody root 4096 4月 7 05:16 fastcgi_temp
- drwxr-xr-x. 2 root root 4096 4月 7 04:37 html
- drwxr-xr-x. 2 root root 4096 4月 7 05:16 logs
- drwx------. 2 nobody root 4096 4月 7 05:16 proxy_temp
- drwxr-xr-x. 2 root root 4096 4月 7 04:37 sbin
- drwx------. 2 nobody root 4096 4月 7 05:16 scgi_temp
- drwxr-xr-x. 2 root root 4096 4月 7 06:45 tester
- drwx------. 2 nobody root 4096 4月 7 05:16 uwsgi_temp
- [root@nginx1 nginx]# cd logs/
- [root@nginx1 logs]# ll
- 總用量 16
- -rw-r--r--. 1 root root 4121 4月 7 07:01 access.log
- -rw-r--r--. 1 root root 3435 4月 7 07:01 error.log
- -rw-r--r--. 1 root root 5 4月 7 05:16 nginx.pid
- [root@nginx1 logs]#
- [root@nginx1 logs]# cat nginx.pid
- 3654
- [root@nginx1 logs]# ps -ef | grep nginx
- root 3654 1 0 05:16 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
- nobody 4085 3654 0 07:01 ? 00:00:00 nginx: worker process
- root 4090 1368 0 07:23 pts/0 00:00:00 grep nginx
- [root@nginx1 logs]#
- [root@nginx1 logs]# ls
- nginx.pid
- [root@nginx1 logs]# ls
- nginx.pid
- [root@nginx1 logs]# /usr/local/nginx/sbin/nginx -s reload
- [root@nginx1 logs]# ll
- 總用量 8
- -rw-r--r--. 1 root root 0 4月 7 07:30 access.log
- -rw-r--r--. 1 root root 60 4月 7 07:30 error.log
- -rw-r--r--. 1 root root 5 4月 7 05:16 nginx.pid
- [root@nginx1 logs]#
爲了不讓原來log文件中的內容影響我們的結果,我們先把access.log和error.log文件刪除,由於修改了配置文件並且要生成log文件,因此我們需要重啓nginx,重啓後,我們到/usr/local/nginx/logs/目錄下查看生成的日誌文件,如下所示。可以看到已經生成我說的幾個日誌文件了,這時候日誌文件都是空的。
- [root@nginx1 conf]# /usr/local/nginx/sbin/nginx -s reload
- [root@nginx1 conf]# ps -ef | grep nginx
- root 3654 1 0 05:16 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
- nobody 4141 3654 0 08:01 ? 00:00:00 nginx: worker process
- root 4143 1368 0 08:02 pts/0 00:00:00 grep nginx
- [root@nginx1 conf]# cd /usr/local/nginx/logs/
- [root@nginx1 logs]# ll
- 總用量 8
- -rw-r--r--. 1 root root 0 4月 7 08:01 access.log
- -rw-r--r--. 1 root root 60 4月 7 08:01 error.log
- -rw-r--r--. 1 root root 5 4月 7 05:16 nginx.pid
- -rw-r--r--. 1 root root 0 4月 7 08:01 test.com.log
- [root@nginx1 logs]#
- [root@nginx1 logs]# cat test.com.log
- 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)" "-"
- [root@nginx1 logs]#
下面來說下日誌切割,我們在日常生活中,對nginx日誌的分析非常的重要,通常需要運維去對nginx的日誌進行切割和分析處理。比如實現一個定時任務,去處理nginx日誌等。爲什麼要進行日誌拆分?這是因爲對於一個訪問量大的網站來說,日誌的增長是非常快的,如果都放在一個文件當中,查看將非常不方便,不利於問題的解決,我們根據公司的實際情況定時對日誌進行拆分,像天貓、京東這樣的大型互聯網站,每隔多少分鐘可能就需要進行一次拆分,對於一般的公司來說,可以每小時或者每天拆分一次。
分割日誌,我們肯定不能靠人工去分割,我們採用的是shell腳本來進行分割,做法就是先把日誌文件移動到另一個地方並根據時間命名,然後再重新生成一個新的日誌文件。由於需要定時做分割,因此我們在contab裏面定時去調用shell腳本
首先我們在/usr/local/nginx/sbin/目錄下創建一個log.sh的腳本,如下所示。
- [root@nginx1 sbin]# pwd
- /usr/local/nginx/sbin
- [root@nginx1 sbin]# vim log.sh
log.sh腳本內容如下:
- #基礎路徑:/usr/local/nginx
- BASE_DIR=/usr/local/nginx
- #原文件名稱:test.com.access.log
- BASE_FILE_NAME=test.com.access.log
- #當前日誌文件路徑:/usr/local/nginx/logs
- CURRENT_PATH=$BASE_DIR/logs
- #備份日誌文件地址:/usr/local/nginx/datalogs
- BAK_PATH=$BASE_DIR/datalogs
- #當前日誌文件:/usr/local/nginx/logs/test.com.access.log
- CURRENT_FILE=$CURRENT_PATH/$BASE_FILE_NAME
- #備份今天的日誌,爲了方便測試我們的定時器功能,這裏把時間精確到分鐘,名稱如:201704071720
- BAK_TIME=`/bin/date -d today +%Y%m%d%H%M`
- #備份文件:/usr/local/nginx/datalogs/201704071720-test.com.access.log
- BAK_FILE=$BAK_PATH/$BAK_TIME-$BASE_FILE_NAME
- #輸出備份文件信息
- echo $BAK_FILE
- #備份之前先關閉nginx,關閉命令:/usr/local/nginx/sbin/nginx -s stop
- $BASE_DIR/sbin/nginx -s stop
- #將/usr/local/nginx/logs/test.com.access.log移動到/usr/local/nginx/datalogs/201704071720-test.com.access.log
- mv $CURRENT_FILE $BAK_FILE
- #啓動nginx,命令:/usr/local/nginx/sbin/nginx,重啓之後會自動再生成一個test.com.access.log文件
- $BASE_DIR/sbin/nginx
- [root@nginx1 nginx]# mkdir datalogs
- [root@nginx1 nginx]# ll
- 總用量 40
- drwx------. 2 nobody root 4096 4月 7 02:59 client_body_temp
- drwxr-xr-x. 2 root root 4096 4月 7 02:58 conf
- drwxr-xr-x. 2 root root 4096 4月 7 08:41 datalogs
- drwx------. 2 nobody root 4096 4月 7 02:59 fastcgi_temp
- drwxr-xr-x. 2 root root 4096 4月 7 02:58 html
- drwxr-xr-x. 2 root root 4096 4月 7 02:59 logs
- drwx------. 2 nobody root 4096 4月 7 02:59 proxy_temp
- drwxr-xr-x. 2 root root 4096 4月 7 02:58 sbin
- drwx------. 2 nobody root 4096 4月 7 02:59 scgi_temp
- drwx------. 2 nobody root 4096 4月 7 02:59 uwsgi_temp
- [root@nginx1 nginx]#
- [root@nginx1 sbin]# ll
- 總用量 3048
- -rw-r--r--. 1 root root 1016 4月 7 09:11 log.sh
- -rwxr-xr-x. 1 root root 3115344 4月 7 02:58 nginx
- [root@nginx1 sbin]#
我們給log.sh文件賦予所有權限,如下所示。
- [root@nginx1 sbin]# chmod 777 log.sh
- [root@nginx1 sbin]# ll
- 總用量 3048
- -rwxrwxrwx. 1 root root 1016 4月 7 10:12 log.sh
- -rwxr-xr-x. 1 root root 3115344 4月 7 02:58 nginx
- [root@nginx1 sbin]#
下面我們便使用crontab -e命令去設置定時任務,如下所示。
- #每天凌晨兩點備份前一天的日誌
- #* 2 * * * sh /usr/local/nginx/sbin/log.sh
- #爲了測試方便,我們暫且設置成每分鐘執行一次備份
- */1 * * * * sh /usr/local/nginx/sbin/log.sh
- [root@nginx1 sbin]# cd /usr/local/nginx/datalogs/
- You have new mail in /var/spool/mail/root
- [root@nginx1 datalogs]# ll
- 總用量 0
- -rw-r--r--. 1 root root 0 4月 7 10:12 201704071013-test.com.access.log
- -rw-r--r--. 1 root root 0 4月 7 10:13 201704071014-test.com.access.log
- -rw-r--r--. 1 root root 0 4月 7 10:14 201704071015-test.com.access.log
- -rw-r--r--. 1 root root 0 4月 7 10:15 201704071016-test.com.access.log
- -rw-r--r--. 1 root root 0 4月 7 10:16 201704071017-test.com.access.log
- -rw-r--r--. 1 root root 0 4月 7 10:17 201704071018-test.com.access.log
- -rw-r--r--. 1 root root 0 4月 7 10:18 201704071019-test.com.access.log
- -rw-r--r--. 1 root root 0 4月 7 10:19 201704071020-test.com.access.log
- -rw-r--r--. 1 root root 0 4月 7 10:20 201704071021-test.com.access.log
- -rw-r--r--. 1 root root 0 4月 7 10:21 201704071022-test.com.access.log
- -rw-r--r--. 1 root root 0 4月 7 10:22 201704071023-test.com.access.log
- -rw-r--r--. 1 root root 0 4月 7 10:23 201704071024-test.com.access.log
- [root@nginx1 datalogs]#
- [root@nginx1 sbin]# cd /usr/local/nginx/datalogs/
- 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開頭的文件。
- server {
- listen 1234;
- server_name test.com;
- location ~ tester {
- root tester;
- index tester111.html;
- }
- access_log logs/test.com.log main;
- }
- [root@nginx1 nginx]# /usr/local/nginx/sbin/nginx -s reload
當我們在地址欄輸入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錯誤。
- server {
- listen 1234;
- server_name test.com;
- location ~ tester {
- if ($remote_addr = 192.168.156.100) {
- return 401;
- }
- root tester;
- index tester111.html;
- }
- access_log logs/test.com.log main;
- }
- [root@nginx1 conf]# /usr/local/nginx/sbin/nginx -s reload
下面再舉個if條件的例子,我們把if條件改成如下所示的樣子,它的意思是訪問的瀏覽器如果是chrome瀏覽器的話,不管你是什麼請求都定位到chrome.html頁面,然後break跳出(如果不加break的話,每次請求過來都會重新讓瀏覽器發起重定向請求,而且永遠不會結束,從而無法真正訪問到chrome.html頁面,導致提示404錯誤)。
- server {
- listen 1234;
- server_name test.com;
- location ~ tester {
- if ($http_user_agent ~* chrome) {
- rewrite ^.*$ /chrome.html;
- break;
- }
- root tester;
- index tester111.html;
- }
- access_log logs/test.com.log main;
- }
- [root@nginx1 nginx]# cd tester/
- [root@nginx1 tester]# pwd
- /usr/local/nginx/tester
- [root@nginx1 tester]# ll
- 總用量 4
- -rw-r--r--. 1 root root 57 4月 7 06:45 tester111.html
- [root@nginx1 tester]# vim chrome.html
- <html>
- <body>Chrome Page!!!!</body>
- </html>
- [root@nginx1 tester]# /usr/local/nginx/sbin/nginx -s reload
那麼爲何可以達到這個效果呢?我們來看看谷歌瀏覽器的請求日誌信息,如下圖所示,可以看到谷歌瀏覽器的請求信息中有"Chrome"信息,我們的if ($http_user_agent ~* chrome)條件判斷中使用了正則表達式,* chrome可以忽略大小寫,所以"Chrome"也可以匹配"chrome",既然瀏覽器匹配,就進入到了if條件當中,rewrite ^.*$ /chrome.html;這句話的意思是不論你要請求什麼資源,都給你定向到chrome.html文件,因此我們纔看到了chrome.html文件的內容。
下面再舉個例子,我們在端口爲1234的server當中再加一個location,匹配goods,如下所示。
- server {
- listen 1234;
- server_name test.com;
- location ~ tester {
- if ($http_user_agent ~* chrome) {
- rewrite ^.*$ /chrome.html;
- break;
- }
- root tester;
- index tester111.html;
- }
- location /goods {
- rewrite "goods-(\d{1,5})\.html" /goods-ctrl.html;
- root tester;
- index tester111.html;
- }
- access_log logs/test.com.log main;
- }
- [root@nginx1 conf]# cd /usr/local/nginx/tester/
- [root@nginx1 tester]# ll
- 總用量 8
- -rw-r--r--. 1 root root 44 4月 8 05:52 chrome.html
- -rw-r--r--. 1 root root 57 4月 7 06:45 tester111.html
- [root@nginx1 tester]# vim goods-ctrl.html
- <html>
- <body>Goods Page!!!!</body>
- </html>
- [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來處理,這是非常容易做到的事情。