Tech Steps

Nignx 重定向规则

2018-11-20  本文已影响15人  AnnaJIAN
priority prefix example
1 = (exactly) location = /path
2 ^~ (forward match) location = /image
3 ~ (regular expression & case-sensitive) location ~ /image/
4 ~* (regular expression & case-insensitive) location ~* .(jpg|png)
5 NONE (forward match) location /image
last

stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;

break

stops processing the current set of ngx_http_rewrite_module directives as with the break directive;

redirect

returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://”, “https://”, or “$scheme”;

permanent

returns a permanent redirect with the 301 code.

examples

#完全匹配某个链接到本服务器首页
location = "/blog/movie-news/1575/new-up-poster.html" {
   return 301 /; 
}

#以/tag/dana-ward/开头的链接都跳转到首页,301
rewrite "^/tag/dana-ward/" / permanent;

#此类静态文件,加浏览器缓存,设置两周的过期时间
location ~ \.(html|htm|rtf|rtx|svg|txt|xsd|xsl|xml)$ {  
    expires 216000s;    
    add_header Pragma "public";          
    add_header Cache-Control "max-age=216000, public”;
}

#某个坏文件,匹配到logo png
location ~ "^/wp-content/uploads/2013/05/selena-gomez-justin-bieber-kiss-billboard-music-awards-taylor-swift.gif$" {   
    set $args '';   
    rewrite ^ [http://cdn1.clevver.com/wp-content/uploads/2017/08/LOGO-Clevver.png](http://cdn1.clevver.com/wp-content/uploads/2017/08/LOGO-Clevver.png) permanent;
}

#此服务器的首页,跳转到另外一台服务器,加速处理
location / {  
    rewrite ^  http://www.abc.com$request_uri? permanent;
}

#图片后缀的链接,不区分大小写,都跳转到首页, 301
location ~*\.(gif|png|jpg)$ {
    rewrite ^ http://www.abc.come permanent;
}

#静态文件直接请求目录
location ^~ /static/ {
    root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}

上一篇 下一篇

猜你喜欢

热点阅读