Nginx高級

Nginx高級模塊

ngx_http_rewrite_module

什麼是rewrite

rewrite的主要功能是實現URL地址重寫,需要PCRE軟件的支持,通過PCRE兼容正則表達式語法進行匹配。

作用場景:

  • URL訪問跳轉,支持開發設計,如頁面跳轉,兼容性支持,展示效果等
  • SEO優化
  • 維護:後臺維護、流量轉發等
  • 安全

rewrite的語法

關於rewrite的語法可以查看官方文檔,http://nginx.org/en/docs/http/ngx_http_rewrite_module.html,同時官網也提供了關於rewrite規則的一些用法,http://nginx.org/en/docs/http/converting_rewrite_rules.html。

實例

  1. 首先書寫相關的配置文件
server {
    listen 80 default_server;
    server_name localhost;

    root /usr/share/nginx/html;
    location ~ ^/break {
        rewrite ^/break /rewrite_test/ break;
    }

    location ~ ^/last {
         rewrite ^/last /rewrite_test/ last;
    }

    location /rewrite_test/ {
       default_type application/json;
       return 200 '{"status":"success"}';
    }
}

接下里我們在/usr/share/nginx/html/目錄下創建目錄rewrite_test,並添加一個index.html文件,當我們訪問http://193.112.69.164/break的時候,我們將會訪問/usr/share/nginx/html/rewrite_test/index.html,此時頁面將返回json內容。

{
    "status": "success"
}

當然訪問last也是可以的。

官網有一些小例子,這裏貼出來。

if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break;
}

if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
    set $id $1;
}

if ($request_method = POST) {
    return 405;
}

if ($slow) {
    limit_rate 10k;
}

if ($invalid_referer) {
    return 403;
}
#例如我們請求/category-水果-蘋果-紅富士.html時,我們將訪問/category/水果/蘋果/category_紅富士.html
rewrite ^/category-(\d+)-(\d+)-(\d+)\.html$ /category/$1/$2/category_$3.html break;
# 如果當前的agent是google瀏覽器,將重定向/nginx到百度
if ($http_user_agent ~* Chrome) {
	rewrite ^/nginx http://www.baidu.com redirect;
} 
#如果請求的文件不存在。將重定向到百度去請求這個文件
if (!-f $request_filename) {
rewrite ^/(.*)$ http://www.baidu.com/$1 redirect;
}

4種flag的介紹

  • last 停止rewrite檢測【如果沒有匹配到,會繼續向下匹配】

  • break 停止rewrite檢測【如果沒有匹配到,則不再向下匹配,直接返回結果404】

  • redirect 返回302臨時重定向,地址欄會顯示跳轉後的地址

  • permanent 返回301永久重定向,地址欄會顯示跳轉後的地址

rewrite規則優先級

  • 執行server塊中的rewrite指令
  • 執行location匹配
  • 執行選定的location中的rewrite指令

ngx_http_secure_link_module

官方文檔地址:http://nginx.org/en/docs/http/ngx_http_secure_link_module.html

  • 指定並允許檢查請求的鏈接的真實性以及保護資源免遭未經授權的訪問
  • 限制鏈接生效週期

測試


    location / {
        secure_link $arg_md5,$arg_expires;
        secure_link_md5 "$secure_link_expires$uri admin";

        if ($secure_link = "") {
            return 403;
        }

        if ($secure_link = "0") {
            return 410;
        }
    }
#該sh用於生成相應的請求文件
servername="193.112.69.164" #請求的服務器
download_file="/download/User.java" #下載的文件路徑
time_num=$(date -d "2020-1-1 00:00:00" +%s) #過期時間
secret_num="admin" #密鑰,與配置文件相對應

res=$(echo -n "${time_num}${download_file} ${secret_num}"|openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =)

echo "http://${servername}${download_file}?md5=${res}&expires=${time_num}"

當我們用瀏覽器訪問上面腳本生成的鏈接時,我們就可以下載這個文件了,但是當文件過期,或者是參數路徑不正確都時無法下載該文件的。

ngx_http_geoip_module

官方文檔:http://nginx.org/en/docs/http/ngx_http_geoip_module.html

基於IP地址匹配MaxMind GeoIP二進制文件,讀取IP所在地域信息

案例:公司多臺服務器,國內訪問國內的服務器,國外用戶訪問國外的服務器。

  • 區別國內外做HTTP訪問規則
  • 區別國內城市地域做HTTP訪問規則

需要注意的是,nginx默認是不安裝這個模塊的,需要手動安裝這個模塊。

測試使用的配置文件,如下。

geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
server {
    listen       80;
    server_name  localhost;
    #如果訪問的ip不是中國的ip,那將返回403
    location / {
        if ($geoip_country_code != CN) {
            return 403;
        }
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    #獲取自己的ip
   location /myip {
        default_type text/plain;
        return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
   }
    error_page   500 502 503 504 404  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

Nginx的安全控制

關於安全控制的部分可以查看nginx的官方文檔:https://docs.nginx.com/nginx/admin-guide/security-controls/

關於HTTPS服務優化

  • 激活keepalive長連接
  • 設置SSL session緩存
 ##示例配置
 keepalive_timeout 100;
   ssl on;
   ssl_session_cache   shared:SSL:10m;
   ssl_session_timeout 10m;

Nginx結合Lua

Integrate Lua co‑routines into the NGINX event‑processing model.

Installation Instructions

  1. Install the Lua module.

    For Amazon Linux, CentOS, Oracle Linux, and RHEL:

    $ yum install nginx-plus-module-lua
    

    For Debian and Ubuntu:

    $ apt-get install nginx-plus-module-lua
    

    For SLES:

    $ zypper install nginx-plus-module-lua
    
  2. Put both of the following directives in the top-level (“main”) context of the main NGINX Plus configuration file, /etc/nginx/nginx.conf:

    load_module modules/ndk_http_module.so;
    load_module modules/ngx_http_lua_module.so;
    

    Note: The directives must be in this order.

  3. Perform additional configuration as required by the module.

  4. Reload NGINX Plus to enable the module:

    $ nginx -t && nginx -s reload
    

總結

  • rewrite
  • secure_link
  • geoip
  • 安全控制
  • Lua的使用

關於文檔的來源,當前文檔的大部分模塊都是參考的nginx admin doc ,文檔地址:https://docs.nginx.com/nginx/admin-guide/,

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