Nginx 301重定向设置二种方法

  进行了301重定向,把www.sunrisenan.com和sunrisenan.com合并,并把之前的域名也一并合并.

有两种实现方法,第一种方法是判断nginx核心变量host(老版本是http_host):

第一种方法:

server {

      server_name www.sunrisenan.com sunrisenan.com;

      if ($host != 'www.sunrisenan.com' ) {

         rewrite ^/(.*)$ http://www.sunrisenan.com/$1 permanent;

      }

      ...

}


第二种方法:

 server {

if ($host = 'sunrisenan.com'){

           rewrite ^/(.*)$ http://www.sunrisenan.com/$1 permanent;

        }

}

这两种方法中, permanent是关键,详细说明见nginx重定向规则说明。

  • last – 基本上都用这个Flag。

  • break – 中止Rewirte,不在继续匹配

  • redirect – 返回临时重定向的HTTP状态302

  • permanent – 返回永久重定向的HTTP状态301


测试是否定向成功

/usr/local/nginx/sbin/nginx -t

提示:

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

configuration file /usr/local/nginx/conf/nginx.conf test is successful


测试成功, 重启nginx,输入命令:

/usr/local/nginx/sbin/nginx -s reload


重启之后测试一下~是否成功设定完成!

输入命令:

curl  –I   sunrisenan.com

返回如下

HTTP/1.1 301 Moved Permanently

Server: nginx/0.7.65

Date: Tue, 03 Aug 2010 01:12:37 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http://www.sunrisenan.com/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章