ruby-on-rails - nginx url 从 www 重定向到 https 的无 www

标签 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...

    }
}

最佳答案

您需要设置另一台服务器来监听 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;
}

关于ruby-on-rails - nginx url 从 www 重定向到 https 的无 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21938178/

相关文章:

ruby-on-rails - 未定义的方法每个 rails 错误

php - Apache:在此服务器上找不到请求的 URL。 Apache

java - 检查具有 URL 的远程服务器上是否存在文件

python - nginx/bottle python - 将请求路由到另一个端口

Wordpress 站点从 https 恢复...链接断开

c++ - 系统调用 open() 创建可执行文件

javascript - 2个不同平台之间的移动检测

ruby-on-rails - 神社的新手。缓存有什么用?

ruby-on-rails - 用于反向拍卖类型应用程序的 Ruby on Rails

url - 如何向 YouTube API search.list 查询的参数添加多个值?