Nginx的Rewrite配置
本文最后更新于 5753 天前,其中的信息可能已经有所发展或是发生改变。

Nginx的Rewrite
经过网上查阅和测试,发现Nginx的Rewrite规则和Apache的Rewite规则差别不是很大,几乎可以直接使用。比如在Apache中这样写规则
rewrite ^/([0-9]{5}).html$ /viewthread.php?tid=$1 last;
而在Nginx中写成这样写是无法启动的,解决的办法是加上两个双引号:
rewrite "^/([0-9]{5}).html$" /viewthread.php?tid=$1 last;
同时将RewriteRule为Rewrite,基本就实现了Nginx的Rewrite规则到Apache的Rewite规则的转换。

Rewrite的Flags
last - 基本上都用这个Flag。
break - 中止Rewirte,不在继续匹配
redirect - 返回临时重定向的HTTP状态302
permanent - 返回永久重定向的HTTP状态301

官方文档请点击这里,另外如果对于302,301这些状态有疑问的,可以参考《301 Redirect 永久重定向的实现》:https://kimi.pub/85.html
如果需要对Nginx配置防盗链的话,可以参考《Nginx的防盗链配置》:https://kimi.pub/312.html

Discuz!在Nginx下的Rewrite
需要说明的是,下网上以前一直流传的Rewrite都是有误的。
下面的Rewrite中百分号前面多了个转移字符“\”,这在Apache中是需要的,而在Nginx中则是不需要的。
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last;
正确的应该是
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
这个错误在基本上目前所有使用Nginx作为服务器,并且开启了Rewrite的网站上存在。包括Discuz!官方,目前已经给cnteacher反馈了。

完整正确的Discuz!在Nginx下的Rewrite如下:
rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last;
rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last;
rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
break;

相关文档
Nginx下Discuz!的Rewrite》:https://kimi.pub/348.html
Nginx下WordPress的Rewrite》:https://kimi.pub/336.html
Nginx的Rewrite配置》:https://kimi.pub/319.html
Nginx的防盗链配置》:https://kimi.pub/312.html

评论

  1. 16年前
    2008-6-18 17:26:44

    第一楼。。。Oh yeah

  2. 16年前
    2008-6-18 17:36:55

    那wordpress呢?

  3. 16年前
    2008-6-18 21:44:22

    记下来备用

  4. 独自流浪
    16年前
    2008-6-19 15:31:38

    标记,说不定什么时候都用上了!

  5. 博主
    16年前
    2008-6-20 15:55:00

    @Black-Xstar wordpress的URL Rewrite就比较麻烦了,得看你原来是使用的哪种配置方式。

    比如你用www.ccvita.com/%post_id%/这种形式的就可以使用下面的rewrite规则
    if (!-f $request_filename) {
    rewrite ^(.*)$ /index.php?q=$1 last;
    break;
    }
    然后reload下Nginx就可以了。

    以上示例没有经过真实环境的测试。

  6. baalchina
    16年前
    2008-7-21 16:06:49

    看了半天才发现博主是原来dz的水水啊…

    请教个问题,if那里,能否指定port呢。比如,我想指定,通过80端口访问logging.php的,一律给转到443端口去..

    也就是https访问logging.php啦。

    apache里面

    RewriteCond %{SERVER_PORT} ^443$
    RewriteCond %{REQUEST_URI} !^/logging.php.*
    RewriteRule ^/(.*)$ http://%{SERVER_NAME}/$1 [R,L]

    就行了…nginx找了半天文档没找到SERVER_PORT的属性…

    谢谢哈~

  7. 16年前
    2008-8-26 14:03:45

    有些东西是不需要照着apache配置硬转的,先理解apache配置的意思,然后可以舍弃的就舍弃,只考虑必须要的部分

  8. 博主
    16年前
    2008-8-27 22:56:24

    @nginx_fans 是啊,文章第一段就是提个通用的解决方案。

    这样方便新手嘛~~

  9. 笨拉灯
    16年前
    2008-9-06 1:22:00

    博主好啊,网上搜索 nginx 下关于 discuz 的 rewrite 一大堆方案,都不行。试用你的这个也不行。我把我的环境说一下:

    我的网站是 http://www.test.com
    discuz 运行在 http://www.test.com/bbs/

    当前 server 配置为:

    server {
    listen 80;
    server_name http://www.test.com test.com *.test.com;

    location / {
    root /data/www/test;
    index index.htm index.html index.php;
    }

    location ~ .*\.php?$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /data/www/test$fastcgi_script_name;
    include fastcgi_params;
    }
    }

    根据你的方案,修改后为:

    server {
    listen 80;
    server_name http://www.test.com test.com *.test.com;

    location / {
    root /data/www/test;
    index index.htm index.html index.php;
    }

    location /bbs {
    rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last;
    rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last;
    rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
    rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last;
    rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
    break;
    }

    location ~ .*\.php?$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /data/www/test$fastcgi_script_name;
    include fastcgi_params;
    }
    }

    结果发现不行,不知道哪里错了,请博主指教!

  10. 博主
    16年前
    2008-9-06 2:12:49

    楼上的朋友,请查看http://www.ccvita.com/348.html这篇文文章,我贴了完整的Nginx配置代码。

  11. 笨拉灯
    16年前
    2008-9-07 13:11:59

    已经搞定!谢谢楼主的帮助!

  12. 16年前
    2008-9-10 21:04:37

    博主你好,我有个很恼人的问题,百度、google千百次后都解决不了,可否帮忙看看是啥问题:

    这样一个rewrite
    rewrite ^/search/([^_/]*)\.htm$ /index.php?mod=search&w=$1 last;

    原来在Apache下是正常工作的,但是将它移植到Nginx(v0.5.37)下后,对中文的url编码形式,如%E4%BC%91%E9%97%B2(休闲),会解析成%E4%BC%91%E9%A5%B9,即第一个字(休)都是正确的,但第一个字后面的就不正常了。

    可否看看这是啥问题,已经有啥解决办法?谢过了。

  13. 张长弓
    15年前
    2009-1-16 20:52:38

    您好:个人freebsd主机该如何配置?能否贴出完整文件?实验多次,均不成功。

    • 博主
      张长弓
      15年前
      2009-1-17 15:43:08

      @张长弓, freebsd中的Nginx的Rewrite配置应该也是一样的,我没有具体的实践过,没法回复你。

  14. 14年前
    2010-3-27 23:10:06

    我有个问题啊 博主你一定要帮助一下我啊 我这里有一个规则不知道在nginx里面可以用不 你给我看一下吧
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule index\.php /index\.php
    RewriteRule category/([0-9]+)/$ /plus/list.php?tid=$1
    RewriteRule games/([0-9]+)/([0-9]+)/$ /plus/view.php?arcID=$1&pageno=$3
    RewriteRule category/([0-9]+)/([0-9]+)/([0-9]+)/$ /plus/list.php?tid=$1&totalresult=$2&PageNo=$3

    也可以说是apache下面的规则 能不能帮我转换一下 万分感谢啊

  15. 13年前
    2011-3-11 10:19:45

    nginx 如何将请求处理为 url编码格式啊 比如这样http://www.xx27.net/tags/彩妆教程 自动处理为http://www.xx27.net/tags/%B2%CA%D7%B1%BD%CC%B3%CC

  16. 13年前
    2011-5-15 2:01:31

    这是我在lighttpd上使用的一条规则,因为我的网站是全静态,当有GET参数传入的时候静态页面就不能处理了。。。需要转交给wordpress处理
    我的固定连接:/archives/%year%-%monthnum%/%postname%.html
    lighttpd的rewrite规则是:url.rewrite = (“^/archives/(.*)/(.*)\?cid=([0-9]+)” => “/index.php/archives/$1/$2?cid=$3”)
    需要匹配的网址是类似这样的/archives/2011-05/nat-check-c.html?cid=1717
    这条规则在lighttpd里是有效果的,但我直接套用失败。。。不知何处出错
    rewrite ^/archives/(.*)/(.*)\?cid=([0-9]+) /index.php/archives/$1/$2?cid=$3;

  17. 10年前
    2013-11-11 10:13:54
  18. yerkle
    10年前
    2014-4-22 10:04:59

    在线将Apache Rewrite伪静态规则自动转换为Nginx Rewrite
    http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇