通常設置爲30天!php
Rewrite規則含義就是某個URL重寫成特定的URL,從某種意義上說爲了美觀或者對搜索引擎友好,提升收錄量及排名等。html
Rewrite規則的最後一項參數爲flag標記,支持的flag標記主要有如下幾種: nginx
last :至關於Apache裏的(L)標記,表示完成rewrite;瀏覽器
break;本條規則匹配完成後,終止匹配,再也不匹配後面的規則 緩存
redirect:返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址 app
permanent:返回301永久重定向,瀏覽器地址欄會顯示跳轉後的URL地址 tcp
last和break用來實現URL重寫,瀏覽器地址欄URL地址不變。 ide
例如用戶訪問www.test.com,想直接跳轉到網站下面的某個頁面,www.test.com/new.index.html如何來實現呢?網站
咱們可使用Nginx Rewrite 來實現這個需求,具體以下:搜索引擎
在server中加入以下語句便可:
rewrite ^/$ http://www.test.com/index01.html permanent;
*表明前面0或更多個字符
+表明前面1或更多個字符
?表明前面0或1個字符
^表明字符串的開始位置
$表明字符串結束的位置
。爲通配符,表明任何字符
例如多個域名跳轉到同一個域名,nginx rewrite規則寫法以下:
server
{
listen 80;
server_name www.wugk.com wugk.com;
if ($host != ‘www.wugk.com’ ) {
rewrite ^/(.*)$ http://www.wugk.com/$1 permanent;
}
例如日常輸入baidu.com會直接跳轉到www.baidu.com
server {
listen 80;
server_name doudou0826a.com ;
location / {
root html/a;
index index.html index.htm;
expires 3d;
}
rewrite ^/$ http://www.doudou0826a.com/index.html permanent;
}
當訪問doudou0826.com的時候會永久的本身跳轉到www.doudou0826.com/index.html 能夠用在網站替換的時候,或者用在網站癱了以後指定一個頁面的狀況。
server {
listen 80;
server_name www.doudou0826a.com;
location / {
root html/a;
index index.html index.htm;
expires 3d;
}
rewrite ^/$ /newindex.html last;
}
#rewrite 後不會在主頁不會顯示newindex.html文件名
#rewrite 後面不寫全路徑的時候也是能夠的。
server {
listen 80;
server_name doudou0826a.com www.doudou0826a.com dd0826a.com ;
location / {
root html/a;
index index.html index.htm;
expires 3d;
}
if ($host != 'www.doudou0826a.com') #客戶訪問的主機名
rewrite ^/(.*) http://www.doudou0826a.com/$1 permanent;
}
#rewrite ^/$ http://www.doudou0826a.com/index.html permanent;
}
這樣的話,無論訪問哪一個都會跳轉到http://www.doudou0826a.com/index.html
必需要加$1,不然無論訪問什麼,對與錯都直接會跳到主頁。
當咱們訪問一個不存在的一個目錄的時候,重定向到一個PHP文件
if ( !-e $request_filename )#請求的文件
{
rewrite ^/(.*)$ /index.php last;
}
能夠用到訪問不存在的目錄時候跳轉到新頁面,或者跳轉到主頁,或者給出提示。
目錄對換 /123456/xxxx ====> /xxxx?id=123456
Rewrite `/(\d+)/(.+)/ /$2?id=$1 last;
(僞靜態)
瀏覽器請求頭跳轉
(好比電腦和手機訪問的時候頁面顯示不同)
(好比網站防止迅雷下載,防止別人爬數據)
server {
listen 80;
server_name www.doudou0826b.com;
location / {
root html/b;
index index.html index.htm;
}
if ($http_user_agent ~* "wget" ) { return 404; } #忽略大小寫
location /NginxStatus {
stub_status on;
}
}
禁止訪問以.jpg .fly .mp3 .swf爲文件後綴名的文件
#模擬C網站盜用B網站下的圖片
C網站首頁配置
# cat html/c/index.html
<html>
<h1>C PAGE </h1>
<img src= "http://www.doudou0826b.com/10.jpg" >
</html>
#B
server {
listen 80;
server_name www.doudou0826b.com;
location / {
root html/b;
index index.html index.htm;
}
if ($http_user_agent ~* "wget" ) { return 404; }
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked *.doudou0826b.com;
root /html/b ;
if ($invalid_referer) {
return 403;
}
}
location /NginxStatus {
stub_status on;
}
}
#C
server {
listen 80;
server_name www.doudou0826c.com;
location / {
root html/c;
index index.html index.htm;
}
}
直接訪問http://www.doudou0826b.com/10.jpg返回404
訪問http://www.doudou0826c.com/ 頁面上的10.JPG圖片將不會再顯示
查看訪問日誌
92.168.119.1 - - [29/Nov/2017:12:41:25 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; Trident/7.0; rv:11.0) like Gecko"
192.168.119.1 - - [29/Nov/2017:12:41:25 +0800] "GET /10.jpg HTTP/1.1" 403 169 "http://www.doudou0826c.com/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; Trident/7.0; rv:11.0) like Gecko"
瀏覽器查看
最終nginx.conf配置文件
# cat nginx.conf
user nginx nginx;
worker_processes 1;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
#A
server {
listen 80;
server_name doudou0826a.com www.doudou0826a.com dd0826a.com ;
location / {
root html/a;
index index.html index.htm;
expires 3d;
}
if ($host != 'www.doudou0826a.com') {
rewrite ^/(.*) http://www.doudou0826a.com/$1 permanent;
}
#rewrite ^/$ http://www.doudou0826a.com/index.html permanent;
if ( !-e $request_filename )
{
rewrite ^/(.*)$ /index.php last;
}
}
#B
server {
listen 80;
server_name www.doudou0826b.com;
location / {
root html/b;
index index.html index.htm;
}
if ($http_user_agent ~* "wget" ) { return 404; }
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked *.doudou0826b.com;
root /html/b ;
if ($invalid_referer) {
return 403;
}
}
location /NginxStatus {
stub_status on;
}
}
#C
server {
listen 80;
server_name www.doudou0826c.com;
location / {
root html/c;
index index.html index.htm;
}
}
}