nginx url从www重定向到没有www for https

时间:2014-02-21 15:16:52

标签: ruby-on-rails url redirect nginx

我一直在寻找将我的网站从www.domain.com重定向到domain.com

我遵循了这个Nginx no-www to www and www to no-www

但是对于普通http://www.domain.com,它会重定向到domain.com

但是当我尝试https://www.domain.com时,它保持不变,而不是重定向到https://domain.com

我不知道我错过了什么

server {
    listen 80;
    server_name www.domain.com;
    // $scheme will get the http protocol
    // and 301 is best practice for tablet, phone, desktop and seo
    return 301 $scheme://domain.com$request_uri;
}

server {
    listen 80;
    server_name domain.com;
    // here goes the rest of your config file
    // example 
    location / {

        rewrite ^/cp/login?$ /cp/login.php last;
        // etc etc...

    }
}

1 个答案:

答案 0 :(得分:4)

您需要设置另一台服务器来收听https并重定向

server {
  listen 443 ssl;
  server_name www.example.com;
  # add ssl config to avoid certificate warning
  return 301 https://example.com;
}

您可以在一台服务器中对http和https重定向进行总结

server {
  listen 80 443;
  server_name www.example.com;
  ssl on;
  # ssl config
  return 301 $scheme://example.com;
}