Nginx補充

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

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

[root@localhost ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/nginx-1.6.2/

[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --withhttp_flv_module --with-http_ssl_module --with-pcre && make && make install

[root@localhost nginx-1.6.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/

[root@localhost ~]# nginx [root@localhost ~]# netstat -anpt | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4200/nginx [root@localhost ~]# elinks --dump http://192.168.200.101 Welcome to nginx!
複製代碼

 

 

1、配置Nginx隱藏版本號

在生產環境中,須要隱藏 Nginx 等服務的版本信息,以免安全風險 javascript

複製代碼
[root@localhost ~]# curl -I http://192.168.200.101

HTTP/1.1 200 OK

Server: nginx/1.6.2 Date: Thu, 17 Nov 2016 16:07:25 GMT

Content-Type: text/html

Content-Length: 612

Last-Modified: Thu, 17 Nov 2016 16:05:27 GMT

Connection: keep-alive

ETag: "582dd547-264"

Accept-Ranges: bytes
複製代碼

 

一、修改源碼包

複製代碼
[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

[root@localhost ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/nginx-1.6.2/

[root@localhost nginx-1.6.2]# vim src/core/nginx.h

13#define NGINX_VERSION "7.0.0 "

14 #define NGINX_VER "IIS/" NGINX_VERSION

[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install

[root@localhost ~]# /usr/local/nginx/sbin/nginx

[root@localhost ~]# netstat -anpt |grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4346/nginx

[root@localhost ~]# curl -I http://192.168.200.102

HTTP/1.1 200 OK

Server: IIS/7.0.0

Date: Thu, 17 Nov 2016 16:12:26 GMT

Content-Type: text/html

Content-Length: 612

Last-Modified: Thu, 17 Nov 2016 16:11:50 GMT

Connection: keep-alive

ETag: "582dd6c6-264"

Accept-Ranges: bytes
複製代碼

 


二、修改配置文件

複製代碼
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

28 server_tokens off;

[root@localhost ~]# /usr/local/nginx/sbin/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

[root@localhost ~]# killall -HUP nginx

[root@localhost ~]# curl -I http://192.168.200.101

HTTP/1.1 200 OK

Server: nginx

Date: Thu, 17 Nov 2016 16:14:19 GMT

Content-Type: text/html Content-Length: 612

Last-Modified: Thu, 17 Nov 2016 16:05:27 GMT

Connection: keep-alive

ETag: "582dd547-264"

Accept-Ranges: bytes 
複製代碼

 


若是 php 配置文件中配置了 fastcgi_param SERVER_SOFTWARE 選項,則編輯 php-fpm 配 置文件,將fastcgi_param SERVER_SOFTWARE對應值修改成fastcgi_param SERVER_SOFTWARE nginx;php


2、修改 Nginx 用戶與組

      Nginx 運行時進程須要有用戶與組身份的支持,以實現對網站文件讀取時進行訪問控制。 Nginx 默認使用 nobody 用戶帳號與組帳號,通常也要進行修改。css

 

一、編譯安裝時指定

 

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install 

 

二、修改配置文件

 

複製代碼
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 2 user nginx nginx;

[root@localhost ~]# killall -HUP nginx [root@localhost ~]# ps aux |grep nginx root 6651 0.0 0.2 20300 1248 ? Ss 13:38 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 6746 0.0 1.0 24472 5048 ? S 14:27 0:00 nginx: worker process
複製代碼

 

 

3、配置 Nginx 網頁緩存時間

        當 Nginx 將網頁數據返回給客戶端後,可設置資源在客戶端緩存的時間,以方便客戶端 在往後進行相同內容的請求時直接返回,以免重複請求,加快了訪問速度,通常針對靜態 網頁進行設置,對動態網頁不用設置緩存時間。可在 Windows 客戶端中使用 fiddler 查看網 頁緩存時間。 

設置方法:可修改配置文件,在 http 段、或 server 段、或者 location 段加入對特定內容的 過時參數。html

複製代碼
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

49              location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {

50                expires 1d;

51           }

52               location ~ .*\.(js|css)$ {

53                expires 1h;

54           } 

[root@localhost ~]# killall -HUP nginx

[root@localhost ~]# ll /usr/local/nginx/html/

total124

-rw-r--r-- 1 root root              537 Nov 18 00:05 50x.html

-rw-r--r-- 1 root root              643 Nov 18 00:21 index.html

-rw-r--r-- 1 root root         116309 Nov 14 11:54 linux.png

[root@localhost ~]# vim /usr/local/nginx/html/index.html

15 <hr />

16 <img src="linux.png" />
複製代碼

 

 


抓包查看:前端

4、實現Nginx的日誌切割

複製代碼
[root@localhost ~]# vim /opt/cut_nginx_log.sh

#!/bin/bash

# cut_nginx_log.sh 

datetime=$(date -d "-1 day" "+%Y%m%d")

log_path="/usr/local/nginx/logs"

pid_path="/usr/local/nginx/logs/nginx.pid"

[ -d $log_path/backup ] || mkdir -p $log_path/backup

if [ -f $pid_path ]

then

                   mv $log_path/access.log $log_path/backup/access.log-$datetime

                    kill -USR1 $(cat $pid_path)

                    find $log_path/backup -mtime +30 | xargs rm -f

else

                  echo "Error,Nginx is not working!" | tee -a /var/log/messages

fi

[root@localhost ~]# chmod +x /opt/cut_nginx_log.sh

[root@localhost ~]# crontab -e

0      0      *       *       *   /opt/cut_nginx_log.sh

[root@localhost ~]# /opt/cut_nginx_log.sh

[root@localhost ~]# ls /usr/local/nginx/logs/backup/

access.log-20161117

[root@localhost ~]# killall -9 nginx

[root@localhost ~]# /opt/cut_nginx_log.sh

Error,Nginx is not working!

[root@localhost ~]# tail -1 /var/log/messages

Error,Nginx is not working!
複製代碼

 


5、配置 Nginx 實現鏈接超時


         在企業網站中,爲了不同一個客戶長時間佔用鏈接,形成服務器資源浪費,能夠設置 相應的鏈接超時參數,實現控制鏈接訪問時間。 

keepalived_timeout:設置鏈接保持超時時間,通常可只設置該參數,默認爲 65 秒,可根據 網站的狀況設置,或者關閉,可在 http 段、server 段、或者 location 段設置。java

client_header_timeout:指定等待客戶端發送請求頭的超時時間。linux

client_body_timeout:設置請求體讀取超時時間。 

注意:若出現超時,會返回 408 報錯 nginx

複製代碼
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

34 keepalive_timeout 65;

35 client_header_timeout 60;

36 client_body_timeout 60;

[root@localhost ~]# killall -HUP nginx 
複製代碼

 

 

6、更改 Nginx 運行進程數


          在高併發場景,須要啓動更多的 nginx 進程以保證快速影響,以處理用戶的請求,避免 形成阻塞。c++

修改配置文件的 worker_processes 參數,通常設置爲 CPU 的個數或者核數的 2 倍 web

複製代碼
[root@localhost ~]# grep 'core id' /proc/cpuinfo | uniq | wc -l

1

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

3 worker_processes 2;

[root@localhost ~]# /usr/local/nginx/sbin/nginx

[root@localhost ~]# ps aux | grep nginx | grep -v grep

root      4431     0.0    0.2     45040     1160     ?     Ss     00:50       0:00 nginx: master process /usr/local/nginx/sbin/nginx

nginx     4432     0.0    0.3     45492     1844     ?     S      00:50       0:00 nginx: worker process

nginx     4433     0.0     0.3    45492      1756     ?    S       00:50       0:00 nginx: worker process
複製代碼

 

默認 Nginx 的多個進程可能更多的跑在一顆 CPU 上,能夠分配不一樣的進程給不一樣的 CPU 處 理,充分利用硬件多核多 CPU。在一臺 4 核物理服務器,能夠進行下面的配置,將進程進行 分配。

worker_cpu_affinity 0001 0010 0100 1000


7、配置 Nginx 實現網頁壓縮功能

         Nginx 的 ngx_http_gzip_module 壓縮模塊提供了對文件內容壓縮的功能,容許 nginx 服 務器將輸出內容發送到客戶端以前進行壓縮,以節約網站帶寬,提高用戶的訪問體驗,模塊 默認已經安裝。

複製代碼
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

38 gzip on;                                      //開啓 gzip 壓縮輸出

39 gzip_min_length 1k;                    //用於設置容許壓縮的頁面最小字節數

40 gzip_buffers 4 16k;                     //表示申請 4 個單位爲 16k 的內存做爲壓縮結果流緩 存,默認值是申請與原始數據大小相同的內存空間來儲存 gzip 壓縮結果

41 gzip_http_version 1.1;                //設置識別 http 協議版本,默認是 1.1

42 gzip_comp_level 2;                     //gzip 壓縮比,1-9 等級

43 gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;                       //壓縮類型,是就對哪些網頁文檔啓用壓縮功能

44 #gzip_vary on;                         //選項可讓前端的緩存服務器通過 gzip 壓縮的頁面 


[root@localhost ~]# killall -HUP nginx 
複製代碼

 

 


8、配置 Nginx 實現防盜鏈功能

      Nginx 防盜鏈功能也很是強大,在默認狀況下只須要進行簡單的配置,便可實現防盜處 理

 

實驗環境:

資源主機 www.source.com 192.168.200.101

盜鏈主機 www.steal.com    192.168.200.102


一、創建資源主機的測試頁

複製代碼
[root@localhost ~]# vim /usr/local/nginx/html/index.html

<html>

<head>

<title>source page</title>

</head>

<body>

<h1>www.source.com</h1>

<img src="linux.png" />

</body>

</html>

[root@localhost ~]# ls /usr/local/nginx/html/

50x.html      error.jpg        index.html        linux.png
複製代碼

 

客戶機測試:

 

 

二、創建盜鏈主機的測試頁

複製代碼
[root@localhost ~]# vim /usr/local/nginx/html/index.html

<html>

<head>

<title>steal page</title>

</head>

<body>

<h1>www.steal.com</h1>

<img src="http://www.source.com/linux.png" />

</body>

</html> 
複製代碼

 


客戶機測試: 

 

三、資源主機設置防盜鏈

配置說明:

valid_referers                設置信任網站

none                            瀏覽器中 referer(Referer 是 header 的一部分,當瀏覽器向 web 服務 器發送請求的時候,通常會帶上 Referer,告訴服務器我是從哪一個頁面連接過來的,服務 器基此能夠得到一些信息用於處理)爲空的狀況,就直接在瀏覽器訪問圖片

blocked referrer             不爲空的狀況,可是值被代理或防火牆刪除了,這些值不以http://或 https://開頭 

 

複製代碼
[root@localhost ~]# ls /var/www/

error.jpg   index.html     test.jpg

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

location ~* \.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv)$ {

valid_referers none blocked *.source.com source.com;

if ($invalid_referer) {

rewrite ^/ http://www.source.com/error.jpg;

#return 403;

                            }

            }


[root@localhost ~]# 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

[root@localhost ~]# killall -HUP nginx 
複製代碼

 


第一行:wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv 表示對這些後綴的文件實 行防盜鏈

第二行:none blocked *.source.com source.com;               //不區分大小寫  

表示 referers 信息中匹配 none blocked *.source.com source.com (*表明任何,任何的 二級域名)

if{}裏面內容的意思是,若是連接不是來自第二行指定的就強制跳轉到 403 錯誤頁面,當 然直接返回 404 也是能夠的,也能夠是圖片。 

注意:設置客戶機的 hosts 文件 

客戶測試: 

 

9、對 FPM 模塊進行參數優化


     Nginx 的 PHP 解析功能實現若是是交由 FPM(fastcgi 進程管理器)處理的,爲了提升 PHP 的處理速度,可對 FPM 模塊進行參數跳轉。 

FPM 優化參數:

pm                                    使用哪一種方式啓動 fpm 進程,能夠說 static 和 dynamic,前者將產生 固定數量的 fpm 進程,後者將以動態的方式產生 fpm 進程

pm.max_children                 static 方式下開啓的 fpm 進程數

pm.start_servers                 動態方式下初始的 fpm 進程數量

pm.min_spare_servers         動態方式下最小的 fpm 空閒進程數

pm.max_spare_servers        動態方式下最大的 fpm 空閒進程數 

注:以上調整要根據服務器的內存與服務器負載進行調整 

示例:

     服務器爲雲服務器,運行了我的論壇,內存爲 1.5G,fpm 進程數爲 20,內存消耗近 1G, 處理比較慢 

優化參數調整:

複製代碼
# vim /usr/local/php5/etc/php-fpm.conf

pm = dynamic pm=start_servers = 5

pm.min_spare_servers = 2

pm.max_spare_servers = 8
複製代碼

 


10、Nginx 爲目錄添加訪問控制

用戶訪問控制:使用 apache 的 htpasswd 來建立密碼文件

複製代碼
[root@localhost ~]# yum -y install httpd

[root@localhost ~]# htpasswd -c /usr/local/nginx/.htpasswd crushlinux

New password:

Re-type new password:

Adding password for user crushlinux 

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

location ~ /status {

stub_status on;

access_log off;

auth_basic "Nginx Status";

auth_basic_user_file /usr/local/nginx/.htpasswd;

}
複製代碼

 

 

客戶端地址訪問控制:

複製代碼
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

location ~ /status {

stub_status on;

access_log off;

auth_basic "Nginx Status";

auth_basic_user_file /usr/local/nginx/.htpasswd;

allow 192.168.200.2;

deny 192.168.200.0/24;

}
複製代碼

 


11、自定義錯誤頁面


  

複製代碼
                   error_page

                     403 404 /404.html;

                      location = /404.html {

                             root html;

                      } 

[root@localhost html]# echo "Sorry,Page Not Found" > /usr/local/nginx/html/404.html

[root@localhost html]# service nginx reload
複製代碼

 

瀏覽器訪問 http://192.168.200.101/abc

返回結果: Sorry,Page Not Found


12、自動索引

複製代碼
                    location /download {

                               autoindex on;

                           }

[root@localhost ~]# cd /usr/local/nginx/html/

[root@localhost html]# mkdir download/dir{1,2} -p

[root@localhost html]# touch download/1.txt

[root@localhost html]# touch download/2.txt

[root@localhost html]# service nginx reload
複製代碼

 

瀏覽器訪問 http://192.168.200.101/download 

 

十3、目錄別名功能:

複製代碼
[root@localhost html]# mkdir Centos RedHat

[root@localhost ~]# echo "hello,students" > /usr/local/nginx/html/RedHat/index.html

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf

              location ~ /Centos {

                     alias /usr/local/nginx/html/RedHat;

               }

[root@localhost ~]# service nginx restart 
複製代碼

 


在瀏覽器中 http://192.168.200.101/Centos 進行測試 

十4、經過 UA 實現手機端和電腦端的分離


      實現nginx區分pc和手機訪問不一樣的網站,是物理上徹底隔離的兩套網站(一套移動端、 一套 pc 端),這樣帶來的好處 pc 端和移動端的內容能夠不同,移動版網站不須要包含特別 多的內容,只要包含必要的文字和較小的圖片,這樣會更節省流量。有好處固然也就會增長 困難,難題就是你須要維護兩套環境,而且須要自動識別出來用戶的物理設備並跳轉到相應 的網站,當判斷錯誤時用戶能夠本身手動切換回正確的網站。 

    有兩套網站代碼,一套 PC 版放在/usr/local/nginx/html/web,一套移動版放在/usr/local/nginx/html/mobile。只須要修改 nginx 的配置文件,nginx 經過 UA 來判斷是否來自 移動端訪問,實現不一樣的客戶端訪問不一樣內容。 

複製代碼
location / {

#默認 PC 端訪問內容

root /usr/local/nginx/html/web; 
#若是是手機移動端訪問內容

if ( $http_user_agent ~

"(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(Wx dB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMCBrowser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcat el)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo) |(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird )|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM )|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SA GEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" )

              {

                    root /usr/local/nginx/html/mobile;

               } 


            index index.html index.htm;

 }
複製代碼

 

實驗模擬:不一樣瀏覽器訪問到不一樣的頁面:

複製代碼
[root@localhost html]# mkdir firefox msie

[root@localhost html]# echo "hello,firefox" > firefox/index.html

[root@localhost html]# echo "hello,msie" > msie/index.html

location / {

        if ($http_user_agent ~ Firefox) {

                           root /usr/local/nginx/html/firefox;

              }

         if ($http_user_agent ~ MSIE) {

                           root /usr/local/nginx/html/msie;

               }

                      index index.html index.htm;

               } 
複製代碼

 

十5、nginx 平滑升級版本

1、nginx 平滑升級概述

       隨着網站併發訪問量愈來愈高,nginx web 服務器頁愈來愈流行,nginx 版本換代愈來愈頻 繁,1.10.2 版本的 nginx 更新了許多新功能,生產環境中版本升級必然的,可是線上業務不 能停,此時 nginx 的升級就是運維的重要工做了。

2、nginx 平滑升級原理

多進程模式下的請求分配方式 

Nginx 默認工做在多進程模式下,即主進程(master process)啓動後完成配置加載和端口綁 定等動做,fork 出指定數量的工做進程(worker process),這些子進程會持有監聽端口的文 件描述符(fd),並經過在該描述符上添加監聽事件來接受鏈接(accept)。 

信號的接收和處理 

Nginx 主進程在啓動完成後會進入等待狀態,負責響應各種系統消息,如 SIGCHLD、SIGHUP、 SIGUSR2 等。 

Nginx 信號簡介 

主進程支持的信號

TERM, INT:               馬上退出

QUIT:                       等待工做進程結束後再退出

KILL:                        強制終止進程

HUP:                         從新加載配置文件,使用新的配置啓動工做進程,並逐步關閉舊進程。

USR1:                       從新打開日誌文件

USR2:                       啓動新的主進程,實現熱升級

WINCH:                     逐步關閉工做進程 


工做進程支持的信號

TERM, INT:                 馬上退出

QUIT:                         等待請求處理結束後再退出

USR1:                        從新打開日誌文件 


3、nginx 平滑升級實戰

 

複製代碼
[root@localhost ~]# rpm -q httpd

package httpd is not installed 

[root@localhost ~]# yum -y install pcre-devel zlib-devel 

[root@localhost ~]# ll nginx-*

-rw-r--r-- 1 root root 910812 Nov 15 15:00 nginx-1.10.2.tar.gz

-rw-r--r-- 1 root root 804164 Dec  11 2014  nginx-1.6.2.tar.gz 

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

[root@localhost ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/nginx-1.6.2/

[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --withhttp_gzip_static_module && make && make install 

[root@localhost ~]# /usr/local/nginx/sbin/nginx 

[root@localhost ~]# netstat -anpt |grep :80

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4178/nginx

[root@localhost ~]# elinks --dump http://localhost

                           Welcome to nginx!
複製代碼

 


1.查看舊版 nginx 的編譯參數

複製代碼
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.6.2

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --withhttp_stub_status_module --with-http_ssl_module --with-http_flv_module --withhttp_gzip_static_module 
複製代碼

 

2.編譯新版本 Nginx 源碼包,安裝路徑需與舊版一致,注意:不要執行 make install

複製代碼
[root@localhost ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/nginx-1.10.2/

[root@localhost nginx-1.10.2]# ./configure --prefix=/usr/local/nginx --user=nginx -group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module -with-http_gzip_static_module && make
複製代碼

 


3.備份二進制文件,用新版本的替換

複製代碼
[root@localhost nginx-1.10.2]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old [root@localhost nginx-1.10.2]# ls

auto    CHANGES.ru    configure    html   Makefile    objs       src      CHANGES      conf     contrib LICENSE       man          README

[root@localhost nginx-1.10.2]# cp objs/nginx /usr/local/nginx/sbin/
複製代碼

 


4.確保配置文件無報錯

[root@localhost nginx-1.10.2]# /usr/local/nginx/sbin/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

 

 

5.發送 USR2 信號

向主進程(master)發送 USR2 信號,Nginx 會啓動一個新版本的 master 進程和對應工做進 程,和舊版一塊兒處理請求

複製代碼
[root@localhost ~]# ps aux | grep nginx | grep -v grep

root    4108   0.0   0.2 45028 1152 ? Ss 16:58 0:00 nginx: master process /usr/local/nginx/sbin/nginx 
nginx 4109 0.0 0.4 45456 2012 ? S 16:58 0:00 nginx: worker process [root@localhost ~]# kill -USR2 4108 [root@localhost ~]# ps aux | grep nginx | grep -v grep root 4108 0.0 0.2 45028 1316 ? Ss 16:58 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 4109 0.0 0.4 45456 2012 ? S 16:58 0:00 nginx: worker process root 6605 0.5 0.6 45196 3364 ? S 17:02 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 6607 0.0 0.3 45624 1756 ? S 17:02 0:00 nginx: worker process
複製代碼

 


6.發送 WINCH 信號

向舊的 Nginx 主進程(master)發送 WINCH 信號,它會逐步關閉本身的工做進程(主進程不 退出),這時全部請求都會由新版 Nginx 處理

[root@localhost ~]# kill -WINCH 4108 [root@localhost ~]# ps aux | grep nginx | grep -v grep 
root 4108 0.0 0.2 45028 1320 ? Ss 16:58 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 6605 0.0 0.6 45196 3364 ? S 17:02 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 6607 0.0 0.3 45624 1756 ? S 17:02 0:00 nginx: worker process

 

 

Nginx rewrite
Rewrite 主要的功能就是實現URL的重寫,Nginx的Rewrite規則採用PCRE(Perl Compatible Regular Expressions)
perl兼容正則表達式的語法進行規則的功能,在編譯Nginx以前,須要編譯安裝PCRE庫

URL是Uniform Resource Location的縮寫,譯爲「統一資源定位符」。如:http://www.linkwan.com/111/welcome.htm

URI由一個經過通用資源標誌符(Universal Resource identifier,簡稱「URI」)進行定位。

if指令
規則語法

1
2
3
4
5
6
if ($http_user_agent ~MSIE){
rewrite ^(.*)$  /msie/ $1  break ;
}
if (!-f $request_filename){
rewrite ^ /img/ (.*)$ /site/ $host /images/ $1 last;
}

  

rewrite語法規則
變量名
變量名可使用"="或"!="運算符
"~" 符號表示區分大小寫字母的匹配
"~*" 符號表示不區分大小寫字母的匹配
"!~" 和"!~*"與"~" "!~"相反
"-f"和"!-f" 用來判斷文件是否存在
"-d"和"!-d" 用來判斷目錄是否存在
"-e"和"!-e" 用來判斷文件或目錄是否存在
"-x"和"!-x" 用來判斷文件是否能夠執行
也支持$!到$9位置化參數

return指令
示例,若是訪問的URL以"*.sh" "*.bash"結尾,則返回狀態碼403

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
location ~ .*\.(sh| bash )?$
{
return  403;
}
 
set 、rewrite指令
if  ($host ~* ^(.*?)\.aaa\.com$)
{
set  $var_tz  '1' ;
if  ($host ~* ^192\.168\.1\.(.*?)$)
{
set  $var_tz  '1' ;
}
if  ($host ~*^localhost)
{
set  $var_tz  '1'
}
if  ($var_tz !~ '1' )
{
rewrite ^/(.*)$ http: //www .aaa.com/ redlirect;

  

rewrite指令
rewrite指令的最後一項參數爲flag標記,支持的flag標記主要有如下幾種
last:至關於apache裏的[L]標記,表示完成rewrite;
break:本條規則匹配完成後,終止匹配,再也不匹配後面的規則
redirect:返回302臨時重定向,瀏覽器地址欄會顯示跳轉後的URL地址
permanent:返回301永久重定向,瀏覽器地址欄會顯示跳轉後的URL地址

last和break用來實現URI重寫,瀏覽器地址欄URL地址不變
redirect和permanent用來實現URL跳轉,瀏覽器地址欄會顯示跳轉後的URL地址

rewrite fiag案例
lacation /cms/ {
proxy_pass http://test.yourdomain.com;
rewrite "^/cms/(.*)\.html$" /cms/index.html break;
}
通常在跟location中(即location /{……})或直接在Server標籤中編寫rewrite規則,
推薦使用last標記,在非跟location中(location /cms/{……}),則使用break標記

rewrite規則編寫實例
1.將原來要訪問/data目錄重寫爲/bbs
rewrite ^/data/?$ /bbs/ permanent;
2.根據不一樣的瀏覽器將獲得不一樣的結果

1
2
3
4
5
6
7
8
9
10
11
if ($http_user_agent~MSIE){
rewrite ^(.*)$ /msie/ $1  break ;
}
 
3.防止盜鏈
localtion ~*\.(gif|jpg|png|swf|flv)${
valid_referers none blocked www. test .com *. test .com;
if ($incalid_referer){
rewrite ^/(.*)http: //www . test .com /block .html;
}
}
相關文章
相關標籤/搜索