nginx rewrite常用示例
相信大家在日常运维工作中如果你用到nginx作为前端反向代理服务器的话,你会对nginx的rewrite又爱又恨,爱它是因为你搞定了它,完成了开发人员的跳转需求后你会觉得很爽,觉得真的很强大,恨它是因为当一些稀奇古怪跳转的需求时候你会没有头绪、百般调试、上网求神拜佛都搞不定的时候真是想死的心都有了,当然网上也有很多nginx rewrite的经典示例,但是我感觉对我的工作都没有太大的帮助。下面是我工作中遇到的一些rewrite示例。提供给大家分享。一、访问http://www.abc.com正常,现在需要访问abc.com,直接跳转到http://www.abc.com。
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>server {</li><li>listen 80;</li><li>server_name abc.com;</li><li>access_log off;</li><li>rewrite (.+) $scheme://www.$host$1 permanent;</li><li>}</li></ol>二、如果你们域名是http://www.abc.com,现在想换一个域名http://www.def.com了,又不想失去之前域名的访问量,需要配置跳转。
首先需要在你的server_name里面把www.abc.com和www.def.cn都写上。
然后做如下配置:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>location / {</li><li>if ($host = 'www.abc.com')</li><li>{</li><li>rewrite ^/(.*)$ http://www.def.com/$1 permanent;</li><li>}</li><li>}</li></ol>三、跳转维护页面,比如今天维护,想挂一个维护页面,希望除某些特定的IP地址能正常访问,因为需要测试。其余所以的IP地址都是访问维护页面,这个需求我之前好像写过,但是今天写的有点不一样。在server里面添加如下内容:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>set $rewrite true;</li><li>if ($remote_addr = "xxx.xxx.xxx.xxx") {</li><li>set $rewrite false;</li><li>}</li><li>if ($rewrite = true) {</li><li>rewrite (.+) http://www.abc.com/maintenance.html;</li><li>}</li><li>location = /maintenance.html {</li><li> root /var/vhost;</li><li>}<br /></li></ol>四、比如现在你的域名是http://bbs.abc.com,现在将这个域名下面的帖子访问都跳转到http://www.abc.com/bbs,注意跳转后的参数也要保持一致。
比如:http://bbs.abc.com/post/addpost/913.shtml跳转到http://www.abc.com/bbs/post/addpost/913.shtml
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>location /post {</li><li>rewrite (.+) http://www.abc.com/bbs$1 permanent;</li><li>}</li></ol>五、基于uri的跳转
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>if ($request_uri ~* ^/note\.php\?product_code=(.*)$) {</li><li>rewrite (.*) http://www.abc.com permanent;</li><li>}</li></ol>
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>if ($request_uri ~ ^/forum-(140|141|142|143|144|145|150|151|152|153|154|155|156|157|158|159|200|222|223|224|225|226|227|228)-(\d+).html$) {</li><li>rewrite (.*) http://www.abc.com/list permanent;</li><li>}</li></ol>六、基于目录下面文件的跳转
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>location ~* /upload/.*\.php$ {</li><li>return 404;</li><li>}</li></ol>七、最普通的url到url跳转
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>location ~* ^/ab/maintain/main.html {</li><li>rewrite (.+) http://www.abc.com/maintain/main.html permanent;</li><li>}</li></ol>虽然例子举不全,可能还有一些其它更好的跳转示例,以后再补充。有不对的地方欢迎大家拍砖。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
