公司實戰nginx之rewrite配置域名跳轉
1.最近遇到了一個開發提的需求,訪問blog.cool360.org的時候跳轉到blog.cool360.org/blog的需求。
看著挺簡單的,但是還是研究了下才配置出來,主要還是不夠深入瞭解nginx的rewrite規則。
正確的配置如下:
rewrite ^/$ http://blog.cool360.org/blog permanent;
2.朋友詢問我一個關於nginx跳轉的問題
如下:
訪問 http://mapi2.dev.hoge.cn/mxu/xxxx.php 變成 http://mapi2.dev.hoge.cn/api/xxxx
配置如下:
location ~ /mxu/.*\.php {
rewrite "^/mxu/(.*)\.php" /api/$1 permanent;
}
3.公司有一個域名跳轉問題
192.168.0.1:8080/test/1234 想通過域名www.abc.com直接訪問。
http跳轉https:
rewrite ^(.*)$ https://$server_name$1 permanent;
https中配置:
proxy_pass 192.168.0.1:8080;
if ($document_uri !~ 'admin')
{
rewrite ^/(.*)$ https://192.168.0.1:8080/test/1234$1 permanent;
}
備註:
rewrite指令的最後一項引數為flag標記,flag標記有:
1.last 相當於apache裡面的[L]標記,表示rewrite。
2.break本條規則匹配完成後,終止匹配,不再匹配後面的規則。
3.redirect 返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址。
4.permanent 返回301永久重定向,瀏覽器地址會顯示跳轉後的URL地址。