Nginx 常用的URL重定向方法

本文将以一些实例简单对Nginx的一些常用的URL重写方法做个简要的介绍。废话这里也不多说了哈,下面就转入正题。

1. 在 Apache 中的写法

RewriteCond  %{HTTP_HOST}  nginx.org
RewriteRule  (.*)          http://www.nginx.org$1



在 Nginx 可以对应写成:

server {
    listen       80;
    server_name  www.nginx.org  nginx.org;
    if ($http_host = nginx.org) {
        rewrite  (.*)  http://www.nginx.org$1;
    }
    ...
}



但 Nginx 作者更建议的方法是:

server {
    listen       80;
    server_name  nginx.org;
    rewrite   ^  http://www.nginx.org$request_uri?;
}

server {
    listen       80;
    server_name  www.nginx.org;
    ...
}






location / {
    root       /var/www/myapp.com/current/public;

    try_files  /system/maintenance.html
               $uri  $uri/index.html $uri.html
               @mongrel;
}

location @mongrel {
    proxy_pass  http://mongrel;
}



 总体感觉这个就比Apache 的要简化多了。如果你需要了解更多关于Nginx  URL规则重写的知识,可以关注本博客相关分类或标签。

本文最后更新于 2011-06-11 00:07:53 并被添加「linux apache nginx rewrite」标签,已有 18089 位童鞋阅读过。
本文作者:未来往事
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处

相关文章

已有 4 条评论
  1. 林肆

    博主更新蛮勤快的嘛~  有木有ASP程序的重定向代码呢?301的

    林肆 IP 属地:湖北省 /
    1. Rinald

      @林肆

      你好!针对这个问题你可以参照网上的相关文章:
      http://www.lao8.org/html/8/2008-4-18/2008418100442.html
      http://www.chinaz.com/Webbiz/Exp/030510NK2010.html

      Rinald error: 375
  2. 木本无心

    每种程序语言都有自己的重定向方法,asp的也不复杂。

    木本无心 IP 属地:福建省 / 南平市
    1. Rinald

      @木本无心

      赞同

      Rinald error: 375

此处评论已关闭