通常设置为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;
}
}
}