nginx 的 rewrite 語法
語法: rewrite regex replacement flag
默認: none
做用域: server, location, if
此指令根據表達式來更改URI,或修改字符串。
指令根據配置文件中的順序來執行。php
注意:
重寫表達式只對相對路徑有效。若是想配對主機名,應該使用if語句。
rewrite只是會改寫路徑部分的東東,不會改動用戶的輸入參數,所以這裏的if規則裏面,你無需關心用戶在瀏覽器裏輸入的參數,rewrite後會自動添加的,所以,只是加上了一個?號和後面咱們想要的一個小小的參數 ***https=1就能夠了。css
nginx的rewrite規則參考:
html
結合QeePHP的例子: nginx
多目錄轉成參數web
目錄對換 sql
例以下面設定nginx在用戶使用ie的使用重定向到/nginx-ie目錄下:瀏覽器
目錄自動加「/」緩存
禁止htaccesscookie
禁止多個目錄dom
禁止以/data開頭的文件
能夠禁止/data/下多級目錄下.log.txt等請求;
禁止單個目錄
不能禁止.log.txt能請求
禁止單個文件
給favicon.ico和robots.txt設置過時時間;
這裏爲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;
}
設定某個文件的過時時間;這裏爲600秒,並不記錄訪問日誌
文件反盜鏈並設置過時時間
這裏的return 412 爲自定義的http狀態碼,默認爲403,方便找出正確的盜鏈的請求
「rewrite ^/ http://www.jbxue.com/leech.gif;」顯示一張防盜鏈圖片
「access_log off;」不記錄訪問日誌,減輕壓力
「expires 3d」全部文件3天的瀏覽器緩存
只充許固定ip訪問網站,並加上密碼
將多級目錄下的文件轉成一個文件,加強seo效果
將根目錄下某個文件夾指向2級目錄
如/shanghaijob/ 指向 /area/shanghai/
若是你將last改爲permanent,那麼瀏覽器地址欄顯是/location/shanghai/
上面例子有個問題是訪問/shanghai 時將不會匹配
這樣/shanghai 也能夠訪問了,但頁面中的相對連接沒法使用,
如./list_1.html真實地址是/area/shanghia/list_1.html會變成/list_1.html,導至沒法訪問。
那我加上自動跳轉也是不行咯
(-d $request_filename)它有個條件是必需爲真實目錄,而個人rewrite不是的,因此沒有效果
知道緣由後就好辦了,手動跳轉:
文件和目錄不存在的時候重定向:
域名跳轉
多域名轉向
三級域名跳轉
域名鏡向
某個子目錄做鏡向
給discuz某版塊單獨配置域名
server_name bbs.c1gstudio.com news.c1gstudio.com;
location = / {
if ($http_host ~ news\.jbxue.com$) {
rewrite ^.+ http://news.jbxue.com/forum-831-1.html last;
break;
}
}
discuz ucenter 頭像 rewrite 優化
location ^~ /ucenter {
location ~ .*\.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location /ucenter/data/avatar {
log_not_found off;
access_log off;
location ~ /(.*)_big\.jpg$ {
error_page 404 /ucenter/images/noavatar_big.gif;
}
location ~ /(.*)_middle\.jpg$ {
error_page 404 /ucenter/images/noavatar_middle.gif;
}
location ~ /(.*)_small\.jpg$ {
error_page 404 /ucenter/images/noavatar_small.gif;
}
expires 300;
break;
}
}
jspace rewrite
location ~ .*\.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~* ^/index.php/{rewrite ^/index.php/(.*) /index.php?$1 break;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;include fcgi.conf;}