那种 Nginx 301 跳转规则写法是最稳妥的 ?

2015-04-08 15:46:22 +08:00
 boro
现在Nginx官方推荐的的301规则是:
server {
listen 80;
server_name example.org;
return 301 http://www.example.org$request_uri;
}

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

按照这种写法,如果需要将旧域名301到新域名上,怎么写才是最合适的?
3916 次点击
所在节点    NGINX
14 条回复
ryd994
2015-04-08 22:30:01 +08:00
这样就是
boro
2015-04-09 12:23:51 +08:00
@ryd994 这样会导致循环报错,是什么原因?
ryd994
2015-04-09 15:18:50 +08:00
@boro 先贴出你的配置,用gist
boro
2015-04-09 23:38:45 +08:00
@ryd994
配置如下:
server {
   listen 127.0.0.1:8080;
   server_name www.vvv.com youmane.com;
   root /var/www/www.vvv.com/public_html;
   index index.php index.html;
   location = /favicon.ico {
            log_not_found off;
            access_log off;
   }
   location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
   }
 
   location ~ /\. {
            deny all;
            access_log off;
            log_not_found off;
   }
   location / {
            try_files $uri $uri/ /index.php?$args;
   }
   rewrite /wp-admin$ $scheme://$host$uri/ permanent;
   location ~*  \.(jpg|jpeg|png|gif|css|js|ico)$ {
            expires max;
            log_not_found off;
   }
   location ~ \.php$ {
            try_files $uri =404;
            include /etc/nginx/fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }
}
boro
2015-04-09 23:40:17 +08:00
server_name www.vvv.com youmane.com;
这一段写错了,正确的事   server_name www.vvv.com vvv.com;
ryd994
2015-04-10 00:09:24 +08:00
@boro 你不是旧域名要301到新域名么?我只看见一个域名啊
FastMem
2015-04-10 10:00:17 +08:00
推荐使用官方的, 或者用rewrite

写法官方的这样:
server {
listen 80;
server_name example.org;
return 301 http://www.example.org$request_uri;
}
server {
listen 80;
server_name www.example.org;
#DO
}

如果rewrite这样
server {
listen 80;
server_name example.org;
rewrite ^(.*) http://www.dexample.org/$1 permanent;
}
server {
listen 80;
server_name www.example.org;
#DO
}
boro
2015-04-11 21:34:39 +08:00
@FastMem 我按照官方的写法,会出现 无限循环错误。
boro
2015-04-11 21:36:54 +08:00
@ryd994 我贴出的事原本的配置样列,按照nginx的建议写法,在top部位加入
server {
listen 80;
server_name example.org;
return 301 http://www.example.org$request_uri;
}

就出现无限循环了。
ryd994
2015-04-12 01:28:45 +08:00
@boro 也就是说旧域名是vvv新域名是example对么?
你这样写当然不对啊,跳来跳去都在旧的上面
server {
server_name 旧.com www.旧.com;
return 301 http://www.新.com$request_uri;
}
server {
server_name 新.com www.新.com;
//以下正常配置
}

另外看你用的是wordpress,检查过wp的配置么?wp是要指定域名的
boro
2015-04-12 13:41:32 +08:00
@ryd994 谢谢!如果我这样写你觉得会不会有问题,和你的比较那个会好些?

server {
server_name 旧.com www.旧.com 新.com ; //这里把所有需要301的域名都写上。
return 301 http://www.新.com$request_uri;
}
server {
server_name 旧.com www.旧.com 新.com www.新.com; //这里把所有需要绑定的域名写上
//以下正常配置
}
ryd994
2015-04-12 20:44:47 +08:00
如果你想要裸域名转过去那就把裸域名放到上面
你把旧域名也加到下面这算几个意思?
RTFM: http://nginx.org/en/docs/http/request_processing.html
FastMem
2015-04-13 13:28:40 +08:00
@boro 那是因为你在第二个server段里面的server_name 写错了 第一个写裸域 第二写带www 不会出现循环的
boro
2015-04-13 19:11:38 +08:00
@FastMem @ryd994 Thanks!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/182343

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX