V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
boro
V2EX  ›  NGINX

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

  •  
  •   boro · 2015-04-08 15:46:22 +08:00 · 3905 次点击
    这是一个创建于 3308 天前的主题,其中的信息可能已经有所发展或是发生改变。
    现在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到新域名上,怎么写才是最合适的?
    14 条回复    2015-04-13 19:11:38 +08:00
    ryd994
        1
    ryd994  
       2015-04-08 22:30:01 +08:00 via Android
    这样就是
    boro
        2
    boro  
    OP
       2015-04-09 12:23:51 +08:00
    @ryd994 这样会导致循环报错,是什么原因?
    ryd994
        3
    ryd994  
       2015-04-09 15:18:50 +08:00
    @boro 先贴出你的配置,用gist
    boro
        4
    boro  
    OP
       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
        5
    boro  
    OP
       2015-04-09 23:40:17 +08:00
    server_name www.vvv.com youmane.com;
    这一段写错了,正确的事   server_name www.vvv.com vvv.com;
    ryd994
        6
    ryd994  
       2015-04-10 00:09:24 +08:00 via Android
    @boro 你不是旧域名要301到新域名么?我只看见一个域名啊
    FastMem
        7
    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
        8
    boro  
    OP
       2015-04-11 21:34:39 +08:00
    @FastMem 我按照官方的写法,会出现 无限循环错误。
    boro
        9
    boro  
    OP
       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
        10
    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
        11
    boro  
    OP
       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
        12
    ryd994  
       2015-04-12 20:44:47 +08:00 via Android
    如果你想要裸域名转过去那就把裸域名放到上面
    你把旧域名也加到下面这算几个意思?
    RTFM: http://nginx.org/en/docs/http/request_processing.html
    FastMem
        13
    FastMem  
       2015-04-13 13:28:40 +08:00
    @boro 那是因为你在第二个server段里面的server_name 写错了 第一个写裸域 第二写带www 不会出现循环的
    boro
        14
    boro  
    OP
       2015-04-13 19:11:38 +08:00
    @FastMem @ryd994 Thanks!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5430 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:43 · PVG 16:43 · LAX 01:43 · JFK 04:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.