5. Nginx规则Rewrite讲解及优化配置
expires 设置缓存多少天过期。
一般设置为30天!
Rewrite规则详解:
Rewrite规则含义就是某个URL重写成特定的URL,从某种意义上说为了美观或者对搜索引擎友好,提高收录量及排名等。
Rewrite规则的最后一项参数为flag标记,支持的flag标记主要有以下几种:
last :相当于Apache里的(L)标记,表示完成rewrite;
break;本条规则匹配完成后,终止匹配,不再匹配后面的规则
redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
last和break用来实现URL重写,浏览器地址栏URL地址不变。
例如用户访问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;
}
}
}