Nginx基礎原理知識

1. Nginx基礎原理知識學習(上)

1. nginx軟件介紹說明

nginx軟件常見的使用方式或架構爲:LNMP(linux nginx mysql php)
    利用nginx官方文檔進行研究學習(多利用官方文檔進行學習)
    http://oldboy.blog.51cto.com/2561410/775056
    https://www.unixhot.com/page/ops
    https://www.unixhot.com/page/cache 

2. nginx軟件的三大主要功能:

  1. web網站服務
  2. 反向代理負載均衡(nginx lvs haproxy)
  3. nginx緩存服務

3. nginx軟件的特點或優勢

  • 支持高併發:能支持幾萬併發連接(特別是靜態小文件業務環境)

  • 資源消耗少:在3萬併發連接下,開啓10個Nginx線程消耗的內存不到200MB

  • 可以做HTTP反向代理及加速緩存、即負載均衡功能,內置對RS節點服務器健康檢查功能,這相當於專業的Haproxy軟件或LVS的功能。

  • 具備Squid等專業緩存軟件等的緩存功能。

  • 支持異步網絡I/O事件模型epoll(Linux 2.6+)(繪圖說明同步和異步)

    1. 基礎課程知識: 大併發寫操作;先寫內存,再將內存數據存儲到硬盤中—保證訪問效率
      大併發讀操作;先寫磁盤,再將磁盤數據存儲到內存中—保證數據安全
    2. 總結:高併發–>資源消耗少–>功能多樣(web服務/負載均衡/支持緩存)–>通訊模型先進(epoll)

4. 思想篇:

​ 把公司當成是自己開的,不是打工者,而是創造財富的

  1. 領導沒有時間,但是經驗豐富
  2. 小白擁有時間,但是經驗缺乏
  3. 與領導多交流,獲取經驗,花費時間
  4. 找尋優質工作機會 -ne 找尋豐厚福利待遇;能力越強,經驗越多 -eq 職位越高,薪資越多

2. nginx軟件的企業功能應用

  • 作爲web服務軟件

  • 反向代理或負載均衡服務

  • 繪圖講解反向代理與負載均衡

  • 前端業務數據緩存服務

  • nginx軟件的動態訪問瓶頸
    ①. 網站數據請求處理流程:

    • web靜態服務軟件:主要負責處理靜態頁面請求
    • php動態程序解釋器:主要負責處理動態頁面請求
    • db數據庫:存取數據信息(重要的瓶頸點)
  • 說明:瞭解了架構瓶頸問題,就需要響應優化的技術進行解決,後期會在優化課程中進行詳細說明講解

    ②. 網站數據請求模型說明:
    epoll模型與select模型的區別說明(比喻說明)


3. nginx軟件的編譯安裝步驟

  • 步驟

    
    ①. 檢查軟件安裝的系統環境
    	    cat /etc/redhat-release
    		uname -r
    
    ②. 安裝nginx的依賴包(pcre-devel openssl-devel)---假設不進行安裝
        yum install -y pcre-devel openssl-devel
    
    ③. 下載nginx軟件---1.10.2 複製鏈接地址(統一位置進行下載)        
        mkdir -p /home/oldboy/tools
        cd /home/oldboy/tools
        wget -q http://nginx.org/download/nginx-1.10.3.tar.gz
    	說明:軟件很小,用心查看一下
       
    ④. 編譯安裝軟件步驟
        a. 解壓要編譯安裝的軟件(解壓軟件---配置(./configure)---做菜(編譯 make)---上菜(安裝 make install))
           tar xf nginx-1.10.2.tar.gz
           cd nginx-1.10.2
           ls (裏面的內容就是源代碼(config readme安裝說明)---默認編譯會安裝到/usr/local目錄)
           useradd -s /sbin/nologin -M www									<--- 創建web服務程序www用戶
           ./configure --prefix=/application/nginx-1.10.2 --user=www --group=www --with-http_stub_status_module  --with-http_ssl_module
    	   (編譯參數說明後續補充說明)
           make 
           make install 
           ln -s /application/nginx-1.10.2 /application/nginx				<--- 安裝完成一個軟件要做一個軟鏈接
        b. 啓動nginx軟件程序進行測試
           /applicaton/nginx/sbin/nginx 
           lsof -i:80
           瀏覽器訪問 10.0.0.8:80
           至此軟件安裝完畢:
    
  • 軟件編譯安裝步驟:

    1. 配置軟件
    2. 編譯軟件
    3. 編譯安裝軟件

4. nginx軟件的編譯安裝常見錯誤說明

1. nginx軟件安裝過程中遇到的問題

  • 軟件依賴包未正確安裝問題—PCRE依賴包沒有安裝

    ./configure: error: the HTTP rewrite module requires the PCRE library.
    You can either disable the module by using --without-http_rewrite_module
    option, or install the PCRE library into the system, or build the PCRE library
    statically from the source with nginx by using --with-pcre=<path> option.
    
    • 解決方法:yum install pcre pcre-devel -y
  • 軟件依賴包未正確安裝問題—OPENSSL依賴包沒有安裝

    ./configure: error: SSL modules require the OpenSSL library.
    You can either do not enable the modules, or install the OpenSSL library
    into the system, or build the OpenSSL library statically from the source
    with nginx by using --with-openssl=<path> option.
    
    • 解決方法:yum install openssl openssl-devel -y

2. nginx軟件啓動過程中遇到的問題

  1. nginx軟件重複啓動產生的錯誤信息

    [root@web01 nginx-1.10.2]# /application/nginx/sbin/nginx 
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] still could not bind()
    
    • 解決方法:nginx軟件已經啓動無需反覆啓動,如果需要重新啓動需要停止nginx進程或者用reload方式進行重啓
  2. nginx軟件使用過程中疑難雜症(參考教案說明)

  • Nginx啓動的疑難雜症彙總
  1. nginx軟件排查問題三部曲說明

    a 在客戶端上ping服務器端IP,檢查鏈路是否通暢
    b 在客戶端上telnet服務器端IP、端口,檢查鏈路訪問是否通暢
    c 在客戶端上wget檢測模擬頁面訪問是否正常    
    curl -v/wget --debug
    

5. nginx目錄結構說明

  • nginx目錄結構

    [root@web01 nginx]# ll
    total 36
    drwxr-xr-x 2 root root 4096 2017-08-10 11:02 conf   --- 保存nginx服務配置文件信息
    drwxr-xr-x 2 root root 4096 2017-08-10 11:02 html   --- web服務的站點目錄
    drwxr-xr-x 2 root root 4096 2017-09-09 15:51 logs   --- nginx服務相關日誌文件保存的目錄
    drwxr-xr-x 2 root root 4096 2017-09-09 15:45 sbin   --- nginx服務相關命令保存目錄
    
  • 403錯誤出現情況:

    1. 客戶端訪問受限
    2. 默認的首頁文件不存在時
  • vim批量編輯方法:
    01; ctrl+v 進入批量編輯模式
    02: 上下箭頭選中批量操作的行
    03: shift+i 編輯選中的第一行
    04: 按ESC結束編輯,多行同時編輯完成


6. nginx軟件使用過程中深入說明

  1. nginx軟件語法檢查方法:

    nginx -t 
    
    1. nginx軟件訪問測試過程:

      curl -v www.baidu.com
      
  2. 擴展說明:

    a. 304狀態碼的意義說明
           304狀態碼是瀏覽器緩存造成的,可以利用裝包工具進行查看獲取(抓包工具進行理解http訪問過程)
    	   取消緩存的方式爲:在瀏覽器設置中進行清除緩存;或者採用瀏覽器強制刷新功能進行瀏覽器緩存的刷新
    b. wireshark抓包軟件使用說明
           啓動軟件---選擇需要進行轉包的網卡---開始進行抓包		
    
  3. nginx軟件編譯參數查看

    nginx -V                 <--- 查看原有的編譯參數信息
    

7. nginx軟件靜態頁面編寫過程:

  1. 熟悉軟件文件目錄結構信息

    • 需要對軟件的目錄結構、軟件相關文件作用、軟件主要文件配置方法進行了解熟悉
    • 日誌文件信息內容查看,相關日誌文件信息內容格式後續課程會繼續說明
  2. 編寫靜態訪問頁面文件信息

<html>
	<meta charset="utf-8">
	<head>
	<title>老男孩教育35期最牛</title>
	</head>
	<body>
	老男孩教育34期最牛
	<table border=1>
	<tr> <td>01</td> <td> </td> </tr>
	<tr> <td>02</td> <td> </td> </tr>
    <tr> <td>03</td> <td> </td> </tr>
    </table>
	<a href="http://blog.oldboyedu.com">
	<img src="stu.png" />
	</a>
	</body>
	</html>

說明:利用虛擬機端口轉發,進行zip壓縮後的站點目錄文件壓縮包下載
wget http://192.168.11.xx:9000/index.html.zip    <- 進行站點目錄下壓縮包文件下載
  1. 擴展知識說明:頁面訪問亂碼文件排查方法

    a. 檢查linux系統的字符集信息
    	   echo $LANG
    	   locale     <- 查看字符集
    	   vi /etc/virc          
           set fileencodings=utf-8,gbk,ucs-bom,cp936    <--- 任意地方增加一行
    b. 檢查xshell軟件字符集設置
    
  2. 靜態頁面編寫企業場景應用實例:

     shell編程例子:http://oldboy.blog.51cto.com/2561410/1867160
    在熟悉靜態頁面編寫知識之後,可以進行編寫web集羣監控頁面,一旦web集羣中有服務器出現故障,就可以在頁面上進行即使發現
    

8. nginx配置文件內容信息說明

1. nginx配置文件框架結果說明

  1. 配置文件內容中的框架結構說明

     egrep -v "#|^$" nginx.conf.default >nginx.conf       <- 精簡化/最小化默認nginx.conf配置文件信息
    
  2. nginx配置文件實踐配置說明

    檢驗nginx服務是否正常運行:
    [root@web02 ~]# ps -ef|grep nginx
          root       5018      1  0 Apr27 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
          nginx     12998   5018  0 Apr27 ?        00:00:00 nginx: worker process        
          root      37115  37039  0 15:50 pts/0    00:00:00 grep --color=auto nginx
          [root@web02 ~]# lsof -i:80
          COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
          nginx    5018  root    7u  IPv4  18199      0t0  TCP *:http (LISTEN)
          nginx   12998 nginx    7u  IPv4  18199      0t0  TCP *:http (LISTEN)
    

1. Nginx進階知識學習(下)

1. 開始nginx配置文件實踐配置:

  • 【虛擬主機知識說明】

    1. 理解虛擬主機知識概念;即一個server就是一個虛擬主機

        [root@web02 ~]# cat /application/nginx/conf/nginx.conf
          worker_processes  1;
          events {
              worker_connections  1024;
          }
          http {
              include       mime.types;
              default_type  application/octet-stream;
              sendfile        on;
              keepalive_timeout  65;
              server {
                  listen       80;
                  server_name  www.etiantian.org;         <- 修改虛擬主機域名名稱
                  location / {
                      root   html/www;                    <- 修改虛擬主機站點目錄
                      index  index.html index.htm;
                  }
              }
          }
      	curl 10.0.0.8  == curl 10.0.0.8/index.html   	
      
      爲什麼
      [root@web02 ~]# mkdir /application/nginx/html/www/ -p     <- 創建虛擬主機的站點目錄
      [root@web02 ~]# /application/nginx/sbin/nginx -t          <- 檢查配置文件語法信息是否正確
      [root@web02 ~]# /application/nginx/sbin/nginx -s reload	<- 檢查語法配置正確後,進行優雅重啓nginx服務
      
      [root@web02 ~]# cat /etc/hosts                            <- 編輯域名解析信息,便於linux服務器通過域名訪問主機站點目錄下資源信息
          127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
          ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
          10.0.0.7    www.etiantian.org bbs.etiantian.org
          
      [root@web02 ~]# curl www.etiantian.org      <- 利用curl命令本地檢測nginx配置是否成功
      進行curl時,報403錯誤,因爲沒有首頁文件信息
      [root@web02 ~]# echo 'web01 www' > /application/nginx/html/www/index.html
      	                                                       <- 在虛擬主機指定的站點目錄中創建首頁文件			  
      [root@web02 ~]# curl www.etiantian.org                    <- 利用curl命令本地檢測nginx配置是否成功;已經存在首頁文件,測試成功
      

2. 理解虛擬主機知識概念;

  • 進行搭建多個網站(搭建多個虛擬主機,一個網站就是一個虛擬主機(server))

  • 擴展知識:nginx配置文件編輯技巧

    • 可以利用%在server一對花括號之間進行切換
    [root@web02 ~]# cat /application/nginx/conf/nginx.conf
    worker_processes  1;
    events {
           worker_connections  1024;
    }
    http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
             listen       80;
             server_name  www.etiantian.org;         <- 修改虛擬主機域名名稱
             location / {
                 root   html/www;                    <- 修改虛擬主機站點目錄
                 index  index.html index.htm;
           }
    }
    server {
             listen       80;
             server_name  bbs.etiantian.org;         <- 修改虛擬主機域名名稱
             location / {
                 root   html/bbs;                    <- 修改虛擬主機站點目錄
                 index  index.html index.htm;
             }
    }
       server {
             listen       80;
             server_name  blog.etiantian.org;        <- 修改虛擬主機域名名稱
             location / {
                 root   html/blog;                   <- 修改虛擬主機站點目錄
                 index  index.html index.htm;
             }
       }
       }
       說明:利用vim複製命令,複製創建多個server主機標籤;即x,ycopyz---10,17copy17	  
    
    [root@web02 ~]# mkdir /application/nginx/html/{www,bbs,blog} -p  <- 創建不同網站域名站點目錄
    [root@web02 ~]# for name in www bbs blog ;do echo web01 ${name}.etiantian.org >/application/nginx/html/$name/index.html ;done
    
    [root@web02 ~]# for name in www bbs blog ;do cat /application/nginx/html/$name/index.html ;done 
    說明:創建不同站點目錄,並利用for循環創建不同站點目錄下的首頁文件,以及利用for循環進行驗證不同站點目錄下首頁文件內容是否不同
    
    [root@web02 ~]# /application/nginx/sbin/nginx -t          <- 檢查配置文件語法信息是否正確
    [root@web02 ~]# /application/nginx/sbin/nginx -s reload	 <- 檢查語法配置正確後,進行優雅重啓nginx服務			  
    說明:編輯完多個虛擬主機信息,進行重啓nginx服務(檢查語法,平滑重啓)   
             以上配置虛擬主機方式爲---基於域名的方式配置虛擬主機
           
    
    [root@web02 ~]# curl www.etiantian.org          <- 利用curl命令本地檢測nginx配置是否成功;
    [root@web02 ~]# curl bbs.etiantian.org
    [root@web02 ~]# curl blog.etiantian.org
    說明:利用curl命令進行測試三個虛擬主機是否已經可以正確訪問;需要注意hosts文件中已經對三個域名進行了統一解析
    
  • 以上配置虛擬主機方式爲—基於域名的方式配置虛擬主機


3. nginx配置文件實踐配置說明—基於端口配置虛擬主機

  • 開始nginx配置文件實踐配置:

  • 【虛擬主機配置說明—基於端口配置】

    1. 理解基於端口虛擬主機概念;主要用於內部網絡人員訪問虛擬主機使用,不想提供直接對外訪問的虛擬主機

      [root@www conf]# cat nginx.conf
      worker_processes  1;
      events {
          worker_connections  1024;
      }
      http {
          include       mime.types;
          default_type  application/octet-stream;
          sendfile        on;
          keepalive_timeout  65;
          server {
              listen       80;
              server_name  www.etiantian.org;
              location / {
                  root   html/www;
                  index  index.html index.htm;
              }
          }
          server {
              listen       81;						#←由基於域名的80改爲基於端口的81
              server_name  bbs.etiantian.org;
              location / {
                  root   html/bbs;
                  index  index.html index.htm;
              }
          }
          server {
              listen       80;						
              server_name  blog.etiantian.org;
              location / {
                  root   html/blog;
                  index  index.html index.htm;
              }
          }
      }
      說明:通過修改不同server標籤中的監聽端口信息,實現基於端口的方式訪問不同虛擬主機;精簡化配置:只改動bbs虛擬主機端口,其它主機端口不變
      
      [root@web02 ~]# /application/nginx/sbin/nginx -t        <- 檢查配置文件語法信息是否正確
      [root@web02 ~]# /application/nginx/sbin/nginx -s reload	<- 檢查語法配置正確後,進行優雅[root@web02 ~]# ss -lntup|grep nginx           <- 端口信息相應增加,不再只是一個80端口
      說明:編輯完多個虛擬主機信息,進行重啓nginx服務(檢查語法,平滑重啓)
      
      [root@web02 ~]# curl www.etiantian.org       <- 利用curl命令本地檢測nginx配置是否成功;
      [root@web02 ~]# curl bbs.etiantian.org              
      說明:顯示結果均爲www.etiantian.org域名站點目錄下的首頁文件內容
      
      [root@web02 ~]# curl -v bbs.etiantian.org         <- 利用curl命令-v參數獲取訪問網站流程
      	  a. 訪問網站時首先需要根據域名解析獲取到網站的ip地址,找尋網站的ip地址對應的服務器
      	  b. 訪問網站時其次需要根據請求建立連接的目標端口信息,找尋網站的相應服務端口是否存在
      	  c. 訪問網站時再次需要根據請求域名信息獲悉相應的站點,找尋網站的相應站點目錄下的資源信息
      	  d. 訪問網站時最後如果ip地址加端口信息都已找到,但沒有找到對應的域名信息,會按照默認原則使用第一個虛擬主機作爲默認訪問的虛擬站點目錄
      	  
      [root@web02 ~]# curl  http://bbs.etiantian.org:81        <- 利用curl命令本地檢測nginx配置是否成功;
      [root@web02 ~]# curl  http://www.etiantian.org:81        <- 利用curl命令本地檢測nginx配置是否成功;
      
      說明:以上信息爲在linux系統中進行驗證檢測基於端口的虛擬主機配置;建議可以利用windos環境再進行相應測試;並且利用抓包工具進行抓包查看
      
  • 以上配置虛擬主機方式爲—基於端口的方式配置虛擬主機(抓包查看基於端口訪問過程)


4. nginx配置文件實踐配置說明—基於ip配置虛擬主機

  • 開始nginx配置文件實踐配置:

  • 【虛擬主機配置說明—基於ip配置】

    1. 理解基於ip虛擬主機概念;回顧地址監聽概念(繪圖理解)

      [root@www conf]# cat nginx.conf
           worker_processes  1;
           events {
                   worker_connections  1024;
           }
           http {
                 include       mime.types;
                 default_type  application/octet-stream;
                 sendfile        on;
                 keepalive_timeout  65;
                 server {
                         listen       10.0.0.8:80;
                         server_name  www.etiantian.org;			<- 此處也可以改成對應IP 10.0.0.8
                         location / {
                             root   html/www;
                             index  index.html index.htm;
                 }
           }  
      
      # server {
      
      # listen       10.0.0.9:81;
      
      # server_name  bbs.etiantian.org;			<- 此處也可以改成對應IP 10.0.0.9
      
      # location / {
      
      # root   html/bbs;
      
      # index  index.html index.htm;
      
      # }
      
      # }
      
      # server {
      
      # listen       10.0.0.10:82;
      
      # server_name  blog.etiantian.org;			<- 此處也可以改成對應IP 10.0.0.10
      
      # location / {
      
      # root   html/blog;
      
      # index  index.html index.htm;
      
      # }
      
      # }
      
      # }
      
      # 說明:通過修改不同server標籤中的監聽ip信息,實現基於ip的方式訪問不同虛擬主機;精簡化配置:只配置一個虛擬主機,其它註釋
      
      [root@web02 ~]# /application/nginx/sbin/nginx -t        <- 檢查配置文件語法信息是否正確
      [root@web02 ~]# /application/nginx/sbin/nginx -s reload	<- 檢查語法配置正確後,進行優雅重啓nginx服務
      [root@web02 ~]# ss -lntup|grep nginx                      <- 監聽地址信息發生變化
      [root@web02 ~]# /application/nginx/sbin/nginx -s stop	    <- 檢查語法配置正確後,進行優雅關閉nginx服務
      [root@web02 ~]# /application/nginx/sbin/nginx     	    <- 進行啓動nginx服務
      [root@web02 ~]# ss -lntup|grep nginx                      <- 監聽地址信息發生變化
      
      說明:編輯完多個虛擬主機信息,進行重啓nginx服務(檢查語法,平滑重啓);和IP地址相關的配置變化,nginx服務需要重新啓動生效,不能採用平滑重啓方式
      
      提示:更多nginx命令行參數說明:http://nginx.org/en/docs/switches.html
      
      [root@web02 ~]# curl 10.0.0.8                <- 利用curl命令本地檢測nginx配置是否成功;
      [root@web02 ~]# curl 172.16.1.8              
      說明:顯示結果均爲相應站點目錄資源信息;而訪問非監聽地址,沒有返回信息
      
  • 以上配置虛擬主機方式爲—基於IP的方式配置虛擬主機

  • 擴展知識說明:vim編輯器使用

    ctrl+v          進入可視化模塊模式,實現批量編輯操作
    r               直接替換字符信息
    x               刪除當前光標所在位置字符信息
    

5. Nginx配置虛擬主機的步驟(簡介)

  • Nginx配置虛擬主機的步驟如下(適合各類虛擬主機類型):

    1. 增加一個完整的server標籤段到結尾處。注意,要放在http的結束大括號前,也就是將server標籤段放入http標籤。
    2. 更改server_name及對應網頁的root根目錄,如果需要其他參數,可以增加或修改。
    3. 創建server_name域名對應網頁的根目錄,並且建立測試文件,如果沒有index首頁,訪問會出現403錯誤。
      • 如果是apache軟件,沒有首頁文件,默認會把站點目錄下面的信息顯示出來
      • nginx出403錯誤解決方式:http://oldboy.blog.51cto.com/2561410/1633952
      • autoindex on; #<==當找不到首頁文件時,會展示目錄結構,這個功能一般不要用除非有需求
      • PS:顯示的目錄結構中,有些信息點擊就是下載,有的點擊就是顯示,因爲擴展名稱不一樣
        • 根本在於nginx軟件是否能夠進行解析
        • nginx是否解析:
          1. html jpg 認識 顯示出內容
          2. 認識 不解析 便直接下載
    4. 檢查Nginx配置文件語法,平滑重啓Nginx服務,快速檢查啓動結果。
    5. 在客戶端對server_name處配置的域名做host解析或DNS配置,並檢查(ping域名看返回的IP是否正確)。
    6. 在Win32瀏覽器中輸入地址訪問,或者在Linux客戶端做hosts解析,用wget或curl接地址訪問。
    • Nginx虛擬主機的官方幫助網址爲:http://Nginx.org/en/docs/http/request_processing.html 。
  • 擴展知識:瀏覽器隱身模式設置

    • 谷歌隱身模式:不會留下緩存
    • 緩存信息抓包後顯示的狀態碼爲304
    • nginx裏面看緩存304的訪問記錄,訪問nginx日誌信息,訪問尺寸爲0
    • tail -f /application/nginx/logs/access_www.log

5. nginx排錯過程說明

* 下面介紹客戶端排查的思路:(如果curl www.baidu.com結果不是預期的)
    
      * 第一步,在客戶端上ping服務器端IP,命令如下。

        ```
  ping 10.0.0.8					    <- 排除物理線路問題影響
        ```
      
* 第二步,在客戶端上telnet服務器端IP、端口,命令如下:

  ```
   telnet 10.0.0.8 80				    <- 排除防火牆等得影響
  ```

* 第三步,在服務端使用wget命令檢測,如下:

  ```
    wget 10.0.0.8(curl -I 10.0.0.8)	<- 模擬用戶訪問,排除http服務自身問題,根據輸出在排錯    
    curl -v/wget -debug
  ```

* 提示信息:以上三步是客戶端訪問網站異常排查的重要三部曲。

* nginx軟件如何進行處理請求:參見官方網站資料:http://Nginx.org/en/docs/http/request_processing.html

6. nginx常用功能說明

  1. 多個主機不同配置文件進行管理(規範化配置文件)

    • 實現一個網站對應一個配置文件,而不是將多個網站都放在一個nginx.conf配置文件中

      [root@web02 ~]# cd /application/nginx/conf/
      [root@web02 conf]# mkdir extra
      將不同站點server標籤信息追加到www.conf bbs.conf blog.conf文件中去
      include extra/*.conf
      說明:按照以上參數規範nginx配置文件配置會出現問題,並不符合要求
      
      curl www.etiantian.org 沒有出現問題,正常顯示信息
      curl 10.0.0.7          就會出現問題,顯示bbs.etiantian.org域名信息
      因爲ls extra/*.conf -l信息查看到的第一個配置文件就是bbs.conf
      
      建議nginx配置文件規範配置內容信息如下格式:
      include extra/www.conf;
      include extra/bbs.conf;
      include extra/blog.conf;
      
  2. 域名別名功能說明(sudo裏面的別名-用戶別名 命令別名)

    • 短域名變爲長域名—配置域名別名功能

      server_name  www.etiantian.org www1.etiantian.org www2.etiantian.org;
      主要功能:便於定位識別相應的nginx服務器集羣節點
      
  3. 狀態模塊功能說明

    • nginx -V —查看編譯參數中,是否加載了狀態模塊信息(–with-http_stub_status_module )
    配置nginx服務的狀態模塊信息
    	cat >>/application/nginx/conf/extra/status.conf<<EOF  
        ##status
        server{
            listen  80;
            server_name  status.etiantian.org;
            location / {
              stub_status on;
              access_log   off;
            }
          }
        EOF
        sed -i '13 i include extra/status.conf;'  nginx.conf	
    
  4. 擴展知識:虛擬機端口映射配置

    可以對狀態模塊訪問設置限制,只允許部分網段主機可以訪問狀態模塊頁面信息
    	location /nginx_status {
        stub_status on;
        access_log off;
        allow 10.0.0.0/24;			#<==設置允許和禁止的IP段訪問
        deny all;					#<==設置允許和禁止的IP段訪問
        }
    

7. nginx日誌功能說明

  1. 錯誤日誌信息說明

    error_log的默認值爲:
            #default:error_log logs/error.log error;
            可以放置的標籤段爲:
            #context:main,http,server,location
            參考資料:http://nginx.org/en/docs/ngx_core_module.html#error_log 。
    		說明:nginx官方文檔查詢信息如何使用,如何根據配置信息獲取所在模塊目錄
    
  2. 訪問日誌信息說明

    Nginx日誌格式中默認的參數配置如下:
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
    Nginx記錄日誌的默認參數配置如下:
    access_log  logs/access.log  main;
    說明:以下信息類似於inotify命令的使用過程
    a. 日誌格式信息說明
    b. 日誌輪詢切割說明
    /bin/mv /application/nginx/logs/access.log /application/nginx/logs/access_$(date +%F).log
    /application/nginx/sbin/nginx -s reload
    編寫腳本,利用定時任務進行執行,實現日誌切割的目的
    
  • 擴展自學:logrotate工具使用(系統自帶日誌切割工具)

8. nginx的location作用說明

  • location表示位置的概念,類似於if,即滿足什麼條件,就做什麼
    官方location配置信息說明:
location = / {
        [ configuration A ]
    }
    location / {
        [ configuration B ]
    }
    location /documents/ {
        [ configuration C ]
    }
    location ^~ /images/ {
        [ configuration D ]
    }
    location ~* \.(gif|jpg|jpeg)$ {
        [ configuration E ]
    }
  • 測試說明:

    server {
        listen       80; 
        server_name  www.etiantian.org etiantian.org;
        root   html/www;
        location / {
           return 401; 
        }
        location = / {
            return 402;
        }
        location /documents/ {
            return 403;
        }
        location ^~ /images/ {
            return 404;
        }
        location ~* \.(gif|jpg|jpeg)$ {
    	    return 500;
        }
        access_log logs/access_www.log main
    }
    
    curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org
    curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/index.html
    

9. nginx的rewirte作用說明

  • nginx 的rwite重寫模塊說明:

    http://www.oldboyedu.com/day1&oldboy
    http://www.oldboyedu.com/day1-oldboy
    
    rewrite語法格式:rewrite regex replacement [flag] == s#regex#replacement#g
    rewrite應用標籤:server、location、if
    
  • 練習一

    etiantian.org  >>  www.etiantian.org
    	        server {
                    listen       80;
                    server_name  www.etiantian.org;
                    rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
                    location / {
                        root   html/www;
                        index  index.html index.htm;
                    }
                    access_log  logs/access_www.log  main;
                }
    說明:以上配置會存在問題
    
    [root@web02 conf]# curl etiantian.org
                <html>
    
                <head><title>301 Moved Permanently</title></head><body bgcolor="white">
    
    ​```
            <center><h1>301 Moved Permanently</h1></center>
            <hr><center>nginx/1.10.2</center>
            </body>
    ​```
    
    </html>
    
    [root@web02 conf]# curl -L etiantian.org
    curl: (47) Maximum (50) redirects followed
    
  • 命令知識擴充:curl命令參數說明

    curl -Lv   
    -L表示:-L/--location      Follow Location: hints (H);表示追蹤訪問的過程,跟蹤Location信息;示意信息(H)
     -v表示:                   顯示追蹤的信息
    
  • a. 避免無線跳轉的第一種方法:

       if ($host ~* "^etiantian.org$") {
                rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
                }
    
  • b. 避免無線跳轉的第二種方法:

    server {
                        server_name etiantian.org;
                        rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
                }
    
  • 標記參數說明

    301和302狀態區別:
    Http狀態碼301和302概念簡單區別及企業應用案例
    http://oldboy.blog.51cto.com/2561410/1774260 
    

10 . 工作中應用場景說明

  • nginx 的rwite重寫企業應用場景:
    • 可以調整用戶瀏覽的URL,使其看起來更規範,合乎開發及產品人員的需求。
    • 爲了讓搜索引擎收錄網站內容,並讓用戶體驗更好,企業會將動態URL地址僞裝成靜態地址提供服務。
    • 網站換新域名後,讓舊域名的訪問跳轉到新的域名上,例如:讓京東的360buy換成了jd.com。
    • 根據特殊變量、目錄、客戶端的信息進行URL跳轉等。
  • 說明:開源軟件類似wordpress的,官方都會對僞靜態配置進行說明

11. nginx 的訪問認證

  1. 修改nginx的相關配置文件

    vim extra/www.conf
    	location / {
                root   html/www;
                index  index.html index.htm;
                auth_basic              "oldboy training";
                auth_basic_user_file    /application/nginx/conf/htpasswd;
         }
    
  2. 創建密碼認證文件並進行授權

    yum install httpd-tools -y
    
    htpasswd -bc /application/nginx/conf/htpasswd oldboy 123456
    -c  Create a new file.
         創建一個新的密碼文件
    -b  Use the password from the command line rather than prompting for it.
    	採用免交互的方式輸入用戶的密碼信息
    	
    chmod 400 /application/nginx/conf/htpasswd     
    chown www /application/nginx/conf/htpasswd   
    
    ll /application/nginx/conf/htpasswd
    -r-------- 1 nginx root 21 2017-05-14 19:45 /application/nginx/conf/htpasswd
    
  3. 配置結果測試

    windows瀏覽器進行測試
    linux系統下進行測試	
    curl -u 
    -u/--user <user[:password]> Set server user and password
    [root@web01 conf]# curl -u lidao888:123456 www.etiantian.org
    web01 www
    

12. nginx 知識重點回顧

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