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

时间:2015-06-09 09:28:23

标签: ruby-on-rails ssl nginx

我需要为我的一个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;
  }

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

2 个答案:

答案 0 :(得分:0)

不确定这是否有帮助。

我遇到了同样的问题。在我从ApplicationController

重定向之前,我一直很努力

在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

答案 1 :(得分:0)

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

  • ssl_certificate阻止中删除了ssl_certificate_keydefault_server
  • 从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;

    ......
}
相关问题