Nginx rewrite配置

2022-12-02 11:50 更新

Nginx以其良好的并發(fā)性能,目前正在逐漸取代Apache成為大家的Web server首選,但是Nginx目前的中文資料很少,需要大家努力貢獻。

下面我介紹一下Nginx的Rewrite模塊設(shè)置及Wordpress和Discuz的示例。Nginx的Rewrite規(guī)則比Apache的簡單靈活多了,從下面介紹可見一斑。

首先,Nginx可以用if進行條件匹配,語法規(guī)則類似C,舉例如下:

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

Nginx正則匹配符說明

1、正則表達式匹配,其中:

* ~  為區(qū)分大小寫匹配
* ~* 為不區(qū)分大小寫匹配
* !~和!~*分別為區(qū)分大小寫不匹配及不區(qū)分大小寫不匹配

2、文件及目錄匹配,其中:

* -f和!-f用來判斷是否存在文件
* -d和!-d用來判斷是否存在目錄
* -e和!-e用來判斷是否存在文件或目錄
* -x和!-x用來判斷文件是否可執(zhí)行

如:

if (!-f $request_filename) {
proxy_pass  http://127.0.0.1;
}

Nginx全局變量

一些可用的全局變量有,可以用做條件判斷(待補全)

1.$args
2.$content_length
3.$content_type
4.$document_root
5.$document_uri
6.$host
7.$http_user_agent
8.$http_cookie
9.$limit_rate
10.$request_body_file
11.$request_method
12.$remote_addr
13.$remote_port
14.$remote_user
15.$request_filename
16.$request_uri
17.$query_string
18.$scheme
19.$server_protocol
20.$server_addr
21.$server_name
22.$server_port
23.$uri


其次,Nginx的Rewrite規(guī)則與Apache幾乎完全一致,所不同的是最后的flag標(biāo)記,舉例如下:

rewrite ^/feed/$ http://feed.shunz.net last;

flag標(biāo)記有:

* last 相當(dāng)于Apache里的[L]標(biāo)記,表示完成rewrite,不再匹配后面的規(guī)則
* break 與last類似
* redirect 返回302臨時重定向
* permanent 返回301永久重定向


常用幾個Nginx rewrite配置實例:

Wordpress的重定向規(guī)則:

if (!-e $request_filename) {
rewrite ^/(index|atom|rsd)\.xml$ http://feed.shunz.net last;
rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last;
rewrite ^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last;
rewrite ^ /index.php last;
}

Discuz!的重定向規(guī)則:

if (!-f $request_filename) {
rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$   /archiver/index.php?$1 last;
rewrite ^/forum-([0-9]+)-([0-9]+)\.html$   /forumdisplay.php?fid=$1&page=$2 last;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$  /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
rewrite ^/space-(username|uid)-(.+)\.html$   /space.php?$1=$2 last;
rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
}


多目錄轉(zhuǎn)成參數(shù) 

要求:abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2 

規(guī)則配置:

if ($host ~* (.*)\.domain\.com) { 

    set $sub_name $1;

    rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last; 

目錄對換 

要求:/123456/xxxx -> /xxxx?id=123456 

規(guī)則配置:

rewrite ^/(\d+)/(.+)/ /$2?id=$1 last; 

 

再來一個針對瀏覽器優(yōu)化的自動rewrite,這里rewrite后的目錄可以是存在的;

例如設(shè)定nginx在用戶使用ie的使用重定向到/nginx-ie目錄

規(guī)則如下:

 if ($http_user_agent ~ MSIE) {

     rewrite ^(.*)$ /nginx-ie/$1 break; 

目錄自動加“/” ,這個功能一般瀏覽器自動完成

if (-d $request_filename){ 

rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; 

以下這些可能就跟廣義的rewrite重寫無關(guān)了

禁止htaccess 

location ~/\.ht { 

    deny all; 

禁止多個目錄 

location ~ ^/(cron|templates)/ { 

    deny all; break; 

禁止以/data開頭的文件,可以禁止/data/下多級目錄下.log.txt等請求

location ~ ^/data { 

    deny all; 

禁止單個文件 

location ~ /data/sql/data.sql { 

    deny all; 

給favicon.ico和robots.txt設(shè)置過期時間; 這里為favicon.ico為99天,robots.txt為7天并不記錄404錯誤日志 

location ~(favicon.ico) { 

    log_not_found off; 

    expires 99d; 

    break; 

location ~(robots.txt) { 

    log_not_found off; 

    expires 7d; 

    break; 

設(shè)定某個文件的瀏覽器緩存過期時間;這里為600秒,并不記錄訪問日志 

location ^~ /html/scripts/loadhead_1.js { 

    access_log off; 

    expires 600; 

    break; 

 

Nginx還可以自定義某一類型的文件的保質(zhì)期時間,具體寫法看下文的代碼:

location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
   expires    1h;
   break;
  }
  }

//上段代碼就將js|css|jpg|jpeg|gif|png|swf這類文件的保質(zhì)期設(shè)置為一小時。

 

防盜鏈的設(shè)置:

防盜鏈:如果你的網(wǎng)站是個下載網(wǎng)站,下載步驟應(yīng)該是先經(jīng)過你的主頁找到下載地址,才能下載,為了防止某些網(wǎng)友直接訪問下載地址完全不通過主頁下載,我們就可以使用防盜鏈的方式,具體代碼如下:

location ~* \.(gif|jpg|swf)$ {
  valid_referers none blocked start.igrow.cn sta.igrow.cn;
  if ($invalid_referer) {
  rewrite ^/ http://$host/logo.png;
  }
}

 

文件反盜鏈并設(shè)置過期時間--<盜鏈多次請求也會打開你的站點的圖片啊,所以設(shè)置下緩存時間,不會每次盜鏈都請求并下載這張圖片>

location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ { 

    valid_referers none blocked *.jjonline.cn *.jjonline.com.cn *.lanwei.org *.jjonline.org localhost  42.121.107.189; 

    if ($invalid_referer) { 

        rewrite ^/ http://img.jjonline.cn/forbid.gif; 

        return 417; 

        break; 

    } 

    access_log off; 

    break; 

說明:

 

這里的return 417 為自定義的http狀態(tài)碼,默認為403,方便通過nginx的log文件找出正確的盜鏈的請求地址 

“rewrite ^/ http://img.jjonline.cn/forbid.gif;”顯示一張防盜鏈圖片

 “access_log off;”不記錄訪問日志,減輕壓力 

“expires 3d”所有文件3天的瀏覽器緩存 

 

只允許固定ip訪問網(wǎng)站,并加上密碼;這個對有權(quán)限認證的應(yīng)用比較在行

location \ { 

    allow 22.27.164.25; #允許的ipd

    deny all; 

    auth_basic “KEY”; #認證的一些設(shè)置

    auth_basic_user_file htpasswd; 

}

說明:location的應(yīng)用也有各種變化,這里的寫法就針對了根目錄了。

文件和目錄不存在的時重定向

if (!-e $request_filename) { 

    #proxy_pass http://127.0.0.1; #這里是跳轉(zhuǎn)到代理ip,這個代理ip上有一個監(jiān)聽的web服務(wù)器

    rewrite ^/ http://www.jjonline.cn/none.html;  #跳轉(zhuǎn)到這個網(wǎng)頁去

    #return 404; #直接返回404碼,然后會尋找root指定的404.html文件

域名跳轉(zhuǎn) 

server { 

    listen 80; 

    server_name jump.jjonline.cn ;#需要跳轉(zhuǎn)的多級域名

    index index.html index.htm index.php; #入口索引文件的名字

    root /var/www/public_html/; #這個站點的根目錄

    rewrite ^/ http://www.jjonline.cn/; 

    #rewrite到這個地址,功能表現(xiàn):在瀏覽器上輸入jump.jjonline.cn并回車,不會有任何提示直接變成www.jjonline.cn

    access_log off; 

多域名轉(zhuǎn)向 

server { 

    listen 80; 

 

    server_name www.jjonline.cn www.jjonline.org;

    index index.html index.htm index.php; 

    root /var/www/public_html/; 

    if ($host ~ “jjonline\.org”) { 

        rewrite ^(.*) http://www.jjonline.cn$1 permanent; 

    } 

}

三級域名跳轉(zhuǎn) 

if ($http_host ~* “^(.*)\.i\.jjonline\.cn$”) { 

    rewrite ^(.*) http://demo.jjonline.cn$1; 

    break; 

域名鏡向 

server { 

    listen 80; 

    server_name mirror.jjonline.cn; 

    index index.html index.htm index.php; 

    root /var/www/public_html; 

    rewrite ^/(.*) http://www.jjonline.cn/$1 last; 

    access_log off; 

某個子目錄作鏡向,這里的示例是demo子目錄

location ^~ /demo { 

    rewrite ^.+ http://demo.jjonline.cn/ last; 

    break; 

}

以下在附帶本博客的rewrite寫法,emlog系統(tǒng)的rewrite

 

location ~ {

    if (!-e $request_filename) {

           rewrite ^/(.+)$ /index.php last;

    }

}

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號