【问题标题】:Nginx HTTPS issue to redirect from www to non-wwwNginx HTTPS 问题从 www 重定向到非 www
【发布时间】:2015-06-09 09:28:22
【问题描述】:

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

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

【问题讨论】:

  • serverfault.com 上的 nginx 的流量(和更高的专业知识)比 stackoverflow 上的流量多,我建议您在那里询问。
  • 在收听 80 时你的 $scheme = http 所以你正在循环返回,如果你想要 80 -> 443 那么不要使用方案,强制 https。与 443 相同,看起来你创建了一个循环。

标签: ruby-on-rails ssl nginx


【解决方案1】:

不确定这是否有帮助。

我也遇到了同样的问题。我挣扎了很长时间,直到我从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

【讨论】:

  • 或者把 H 先生的答案放在 nginx config terms server { listen 80; server_name www.example.com;重写 ^/(.*)$ example.com/$1 永久; }`(在需要的地方添加回车,我无法
    这个sn-p)
【解决方案2】:

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

  • 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;

    ......
}

【讨论】:

    猜你喜欢
    • 2018-04-22
    • 1970-01-01
    • 2018-06-23
    • 2015-10-23
    • 1970-01-01
    • 2017-03-10
    相关资源
    最近更新 更多