yinznet
V2EX  ›  问与答

如何在 nginx 服务器上做 301 重定向?

  •  
  •   yinznet · Sep 3, 2014 · 4660 views
    This topic created in 4473 days ago, the information mentioned may be changed or developed.
    是在DNS服务商后台上设置CNAME比较好还是在nginx的配置文件上设置重定向好?
    8 replies  •  2014-09-03 14:59:07 +08:00
    roychan
    roychan
    Sep 3, 2014❤️1
    1
    nginx.conf
    Tink
    Tink
    Sep 3, 2014 via iPhone❤️1
    2
    rewrite
    arcas
    arcas
    Sep 3, 2014❤️1
    3
    先搞清楚301重定向和cname的含义。根据需求选择哪种方式合适。。

    301: 将一个地址永久的重定向到指定的地址。
    cname: 别名记录。这种记录允许您将多个名字映射到同一台计算机。 通常用于同时提供WWW和MAIL服务的计算机。例如,有一台计算机名为“host.mydomain.com”(A记录)。 它同时提供WWW和MAIL服务,为了便于用户访问服务。可以为该计算机设置两个别名(CNAME):WWW和MAIL。 这两个别名的全称就是www.mydomain.com和“mail.mydomain.com”。实际上他们都指向“host.mydomain.com”。(摘自 BD百科)
    bigtan
    bigtan
    Sep 3, 2014❤️1
    4
    http://www.changbizi.net/archives/542.html
    正好的我在博客上面mark了一下的
    yinznet
    yinznet
    OP
    Sep 3, 2014
    5
    nginx配置像这样写?

    server
    {
    isten 80;
    server_name old.com;
    rewrite ^/(.*)$ http://new.com/$1 permanent;
    access_log off;
    }
    2232588429
    2232588429
    Sep 3, 2014
    6
    @arcas 你好,能否帮我看看我这个conf文件配置的有没有问题?谢谢!
    server {
    listen 80;
    #ipv6
    #listen [::]:80 default_server;
    root /var/www/hyyy.tk;
    #.......
    index index.php index.html index.htm;
    #....
    server_name hyyy.tk;
    location / {
    try_files / /index.html;
    #
    include dz.conf;
    }
    #......
    #error_page 404 /404.html;
    location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    }
    }
    alsotang
    alsotang
    Sep 3, 2014
    7
    用 return
    不要用 rewrite。
    shiniv
    shiniv
    Sep 3, 2014
    8
    server{
    listen 80;
    server_name a.me;

    return 301 $scheme://www.a.me$request_uri;
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2436 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 11:46 · PVG 19:46 · LAX 03:46 · JFK 06:46
    ♥ Do have faith in what you're doing.