1. 程式人生 > 實用技巧 >nginx 基本功能

nginx 基本功能

1.nginx簡介

官方文件

Nginx是一個高效能WEB伺服器,除它之外Apache、Tomcat、Jetty、IIS,它們都是Web伺服器,或者叫做WWW(World Wide Web)伺服器,相應地也都具備Web伺服器的基本功能,它處理高併發能力也是十分強大的,能經受高負載的考驗,有報告表明能支援高達 50,000 個併發連線數。

Tomcat、Jetty 面向java語言,先天就是重量級的WEB伺服器,其效能與Nginx沒有可比性。
nginx 和可以做什麼事情

  • 正向代理
  • 反向代理
  • 動靜分離

2. 安裝啟動

安裝環境準備:
最好是linux核心在2.6 及以上.只有2.6之後才支援epool ,在此之前使用select或pool多路複用的IO模型,無法解決高併發壓力的問題。通過命令uname -a 即可檢視。

#檢視 linux 核心
uname -a

(1)GCC編譯器
GCC(GNU Compiler Collection)可用來編譯C語言程式。Nginx不會直接提供二進位制可執行程式,只能下載原始碼進行編譯。
(2)PCRE庫
PCRE(Perl Compatible Regular Expressions,Perl相容正則表示式)是由Philip Hazel開發的函式庫,目前為很多軟體所使用,該庫支援正則表示式。
(3)zlib庫
zlib庫用於對HTTP包的內容做gzip格式的壓縮,如果我們在nginx.conf裡配置了gzip on,並指定對於某些型別(content-type)的HTTP響應使用gzip來進行壓縮以減少網路傳輸量。
(4)OpenSSL開發庫


如果我們的伺服器不只是要支援HTTP,還需要在更安全的SSL協議上傳輸HTTP,那麼就需要擁有OpenSSL了。另外,如果我們想使用MD5、SHA1等雜湊函式,那麼也需要安裝它。
上面幾個庫都是Nginx 基礎功能所必需的,為簡單起見我們可以通過yum 命令統一安裝。

#yum 安裝nginx 環境
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel

原始碼獲取:
nginx 下載頁:http://nginx.org/en/download.html

  1. 下載到本地後 解壓 並進入到解壓之後的目錄
# 解壓
tar -zxvf nginx-1.14.0.tar.gz
  1. 執行configure檔案
./configure

會在objs目錄中生成安裝所需的相關檔案 比如makefile

  1. 再在主目錄執行 make 編譯命令 make

會在objs中生成編譯好的nginx二進位制可執行檔案

  1. 執行make install 將 nginx 可執行檔案copy到 預設的目錄,並生成執行相關的資料夾及檔案

    預設目錄為 /usr/local/nginx

  2. 進入到/usr/local/nginx/sbin 目錄 並執行命令 ./nginx 啟動nginx

    瀏覽器訪問 127.0.0.1 埠預設80 看到如下這個頁面 即為啟動成功

更新模組

nginx 的功能 都是以module的模式進行安裝 ,再我們安裝nginx時 已經預設安裝了許多module 大多數情況下都可以滿足,也可以自己新增模組

ngx_http_stub_status_module為例

官方的解釋:

The ngx_http_stub_status_module module provides access to basic status information.

This module is not built by default, it should be enabled with the --with-http_stub_status_module configuration parameter.

提供對基本狀態資訊的訪問 , 預設情況下未構建此模組

回到我們下載解壓的安裝包目錄, 重新執行configure 並指定模組

# 新增狀態查檢視模組
./configure --with-http_stub_status_module 

再重新構建nginx執行檔案

# 重新建立檔案
make

可以手動將 objs 下生成的nginx 檔案 拷貝到 你的nginx安裝目錄 sbin下 並覆蓋 ,也可以直接執行make install 會自動拷貝到預設的資料夾 並將老的nginx檔案備份

# 複製過去
cp objs/nginx /usr/local/nginx/sbin/

執行命令 檢視是否更新成功

# 檢視詳細資訊 V 大寫
/usr/local/nginx/sbin/nginx -V

編輯配置檔案 /usr/local/nginx/conf/nginx.conf

在server 模組中新增如下配置

 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

		# http_stub_status_module 相關配置
        location = /basic_status {
                stub_status;
        }

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

瀏覽器訪問http://127.0.0.1/basic_status

可以在瀏覽器中看到當前nginx 健康狀態

Active connections: 2 
server accepts handled requests
 5 5 4 
Reading: 0 Writing: 1 Waiting: 1 

3. 常用命令

  1. 檢視命令幫助
    ./sbin/nginx -?
  2. 預設方式啟動:
    ./sbin/nginx
  3. 指定配置檔案啟動
    ./sbing/nginx -c /tmp/nginx.conf
  4. 指定nginx程式目錄啟動
    ./sbin/nginx -p /usr/local/nginx/
  5. 快速停止(立即關閉nginx服務)
    ./sbin/nginx -s stop
  6. 正常停止(拒絕新的請求,但會講已經接受的請求處理完畢)
    ./sbin/nginx -s quit
  7. 不重啟服務 重新載入配置檔案
    ./sbin/nginx -s reload
  8. 重新開啟日誌檔案
    ./sbin/nginx -s reopen
  9. 檢視版本
    ./sbin/nginx -v

4. 配置檔案

預設的配置檔案

# worker執行緒的個數
worker_processes  1;

# events塊
events {
	# 一個worker執行緒最多建立多少個 連線
    worker_connections  1024;
}

#http塊
http {
	# 包含本目錄下的 mime.types 下的內容
    include       mime.types;
    # 
    default_type  application/octet-stream;
    sendfile        on;
    
    #長連線最大的超時閒置時間
    keepalive_timeout  65;
    
    # 配置一個站點 可以配置多個
    server {
    	#站點監聽的埠
        listen       80;
        #站點的名稱 匹配以什麼方式訪問該站點,如果以 localhost:80 的方式 就訪問這個站點
        #就訪問這個站點     
        server_name  localhost;
        
        #攔截的路徑
        location / {
        	# 指定該站點的跟目錄
            root   html;
            #指定index頁面(當沒有指定檔案時 訪問這個檔案)
            index  index.html index.htm;
        }
    }
    
    server {
    	
        listen       80;
        #本機ip為192.168.100.80 如果以 127.0.0.1:80 的方式 即訪問這個站點
        # 也可以有多個 已www.test.com 訪問也是這個站點 以空格隔開
        # 萬用字元:www.*.com
        # 如果有多個站點都相符 則以最大匹配度最大原則
        # 左邊匹配 大於右邊匹配 即 *.test.com 優於 www.test.*
        # 如果優先順序一樣則 在前面的優於後面的
        # 如果所有的都不匹配 都匹配 標註default的 站點 如果沒有就匹配監聽80埠的第一個
        server_name  192.168.100.80 www.test.com default;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
    
}

主要由 配置塊 例如:events、http、server、location 等 和 配置塊中的屬性 例如:worker_connections、include、listen 等 構成 ,/nginx_status 屬於配置塊的特定引數。其中server塊巢狀於http塊,其可以直接繼承訪問Http塊當中的引數

  1. 使用 root 攔截時 會將訪問路徑加到更站點的後面

    		#通過 127.0.0.1/user/conf 訪問時
            location / {
            	# 會基於 html跟目錄訪問
            	# 相當於 訪問html/user/conf
                root   html;
                #指定index頁面
                index  index.html index.htm;
            }
    
  2. 使用alias 去除 匹配到的路徑

    		#通過 127.0.0.1/user/conf 訪問時
            location /user {
            	# 相當於 訪問html/conf
                alias   html;
                #指定index頁面
                index  index.html index.htm;
            }
    

root 和 alias的區別

root: 指定 站點的跟目錄 會拼接攔截的路徑 http 模組下和 loaction模組下都可以配置 location 優先

alias: 指定 站點的別名 去除匹配的攔截路徑 只能配置在 location下

location的匹配方法(優先順序從上到下)

  1. =表示把URI作為字串,以便與引數中的uri做完全匹配。

    # 訪問路徑 127.0.0.1/user 需要完全匹配
    location = user {
            	
            }
    
  2. ^~表示 匹配URI時只需要其前半部分與uri引數匹配即可。

    # 匹配任何已 /test/ 開頭的任何查詢並且停止搜尋。任何正則表示式將不會被測試。
    # 例如: 127.0.0.1/test/a
    location ^~ /test/ {
    
    }
    
  3. ~ 表示uri裡面包含正則,並且區分大小寫

    # 正則匹配 以靜態檔案結尾 例如 127.0.0.1/user/test.jpg
    location ~ \.(png|jpg|mp4)$ {
            	# 相當於 訪問html/conf
                alias   html;
                #指定index頁面
                index  index.html index.htm;
            }
    
  4. ~* 表示uri裡面包含正則,不區分大小寫。

  5. / 基於uri目錄匹配

對連線進行限速

location /download {
    limit_rate 1m; // 限制每S下載速度為1M
    limit_rate_after 30m; // 超過30M 之 後再限速
}

防盜鏈配置:

# 設定訪問png jpg等靜態資源 放盜鏈
location ~ \.(png|jpg|mp4)$ {
        valid_referers none blocked www.baidu.com;
        if ($invalid_referer) {
            return 403;
       }
}

該功能由 ngx_http_referer_module 模組實現,判斷請求頭中的 referer 欄位 判斷訪問的原地址, 並新增白名單,例如本案例 為百度可以訪問, 如果不符合條件則返回403錯誤

但是此方法完全依賴 referer 屬性, 只能防止常規瀏覽器常規操作 如有更復雜的需要 可以使用 http_accesskey_module 模組判斷url的自定義 key判斷

建立IP黑名單,白名單

http_accesskey_module 模組功能

# 封禁規則從上至下依次執行
location / {
    #封禁指定IP
    deny 192.168.0.1;
    #開放指定IP
    allow 192.168.0.1;
    #開放指定IP段  
    #斜槓後面的24代表 :匹配最後一位最大值 即 匹配 192.168.0.1 -> 192.168.0.254
    # 16:匹配後兩位最大值 192.168.0.1 -> 192.168.255.254
    # 8:匹配後三位最大值的 192.0.0.1 -> 192.255.255.254
    allow 192.168.0.0/24;
    #封禁所有
    deny    all;
    #開放所有
    allow    all;
}