Nginx rewrite 301 永久重定向跳转

(1)实现http://blog.klaus.org跳转到http://www.klaus.org/blog/klaus.html

   跳转前,http://blog.klaus.org对应的站点配置(前提是在www.klaus.org站点www目录下得有blog目录,并且blog目录下有klaus.html文件输出)

[root@mobana conf]#cat -n extra/blog.conf

     1      server {
      2          listen       80;
      3          server_name  blog.klaus.org;
      4          location / {
      5              root   html/blog;
      6              index  index.html index.htm  klaus.html;
    7          }
      8        if ( $http_host ~* "^(.*)\.klaus\.org$" ){
      9          set $domain $1;
    10          rewrite ^(.*) http://www.klaus.org/$domain/klaus.html break;
    11        }
         <== 配置无论用户输入任意.klau.org都会跳转到www.klaus.org/blog/klaus.html页面下;
    12        access_log logs/access_blog.log main gzip buffer=32k flush=5s;
    13     }


   跳转后,http://www.klaus.org/blog/klaus.html地址对应的站点配置如下:

[root@mobana conf]# cat -n extra/www.conf 
      1  server {
      2          listen       80;
      3          server_name  www.klaus.org klaus.org;
      4          location / {
      5              root   html/www;
      6              index  index.html index.htm  klaus.html;
      7          }
      8       access_log logs/access_www.log main gzip buffer=32k flush=5s;

     9     }


(2)实现访问http://klaus.org/bbs/跳转到http://bbs.klaus.org
[root@mobana conf]# cat extra/www.conf -n
      1  server {
      2          listen       80;
      3          server_name  www.klaus.org klaus.org;
      4          location / {
      5              root   html/www;
      6              index  index.html index.htm  klaus.html;
      7          }
      8       rewrite ^(.*)/bbs/ http://bbs.klaus.org break;<==配置无论用户输入klau.org/bbs都会跳转到bbs.klaus.org页面下;
      9       access_log logs/access_www.log main gzip buffer=32k flush=5s;
    10     }
[root@mobana conf]# cat extra/bbs.conf -n
      1      server {
      2          listen       80;
      3          server_name  bbs.klaus.org;
      4          location / {
      5              root   html/bbs;
      6              index  index.html index.htm klaus.html;
      7          }
      8        access_log logs/access_bbs.log main gzip buffer=32k flush=5s;
      9     }


注:问题1:在(1)中需要提前在www目录下创建blog目录,并且在blog目录下有blog.klaus.org站点的主文件;

        问题2:在(2)中需要注意的是需要提前在hosts文件中将klaus.org域名做本地解析;

        问题3:reload的时候会出现错误

[root@mobana conf]# ../sbin/nginx -t

nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful

[root@mobana conf]# ../sbin/nginx -s reload

nginx: [error] invalid PID number "" in "/application/nginx-1.6.3//logs/nginx.pid

[root@mobana conf]# ../sbin/nginx -c /application/nginx/conf/nginx.conf

[root@mobana conf]# ../sbin/nginx -s reload

[root@mobana conf]# netstat -lntup|grep 80

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1957/nginx          

         建议:在根location(即location、{......})中或者server{......}标签中编写rewrite规则,使用last标记,而在普通的location(location/klaus/{......}或者if{})中编写rewrite规

则,使用break标记。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值