从 www 重定向到非 www 的 Nginx HTTPS 问题

Nginx HTTPS issue to redirect from www to non-www

我需要为我的 rails 应用程序之一配置 nginx 以通过 SSL 路由某些页面,但遇到配置问题。

我有一个 SSL 证书,其中通用名称是 example.com,我的站点从 www.example.com

路由到 example.com

这是我的 nginx.conf:

  upstream unicorn {
    server unix:/tmp/unicorn.sock fail_timeout=0;
  }

  server {
    listen 80;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
  }

  server {
    listen 443 ssl;
    server_name example.com;
    return 301 $scheme://example.com$request_uri;

    ssl on;
    ssl_certificate      /certificate path;
    ssl_certificate_key  /key path;
  }

  server {
    listen 80 default deferred;
    root /public path;
    try_files $uri/index.html $uri @unicorn;
    location @unicorn {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://unicorn;
    }
    client_max_body_size 50M;
  }

我也尝试过不同的配置,但没有任何效果。任何建议将不胜感激。提前致谢。

不确定这是否有帮助。

我遇到了同样的问题。我挣扎了很长时间,直到我从 ApplicationController

重定向

在应用程序控制器中:

 before_filter :redirect_subdomain

  def redirect_subdomain
    if request.host == 'www.example.com.au'
      redirect_to 'https://example.com.au' + request.fullpath
    end
  end

我的问题已通过以下修改得到解决,回答这个问题可能会对其他人有所帮助:

  • default_server 块中删除了 ssl_certificatessl_certificate_key
  • 从 SSL 服务器块中删除了 URL 覆盖。
  • 已将 ssl_protocolsssl_ciphers 添加到 SSL 服务器块

修改后的配置如下:

upstream unicorn {
    server unix:/tmp/unicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    listen 80 default_server;

    root /example.com/current/public;
    try_files $uri/index.html $uri @unicorn;

    ......
}

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl on;
    ssl_certificate      /example.com.crt;
    ssl_certificate_key  /example.com.key;
    ssl_protocols    TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!MD5;

    ......
}