【问题标题】:Redirect www to non-www on Nginx with SSL gives redirect loop使用 SSL 在 Nginx 上将 www 重定向到非 www 会产生重定向循环
【发布时间】:2015-10-09 23:16:30
【问题描述】:

如果这看起来像似曾相识,我深表歉意。有很多关于类似问题的帖子,我都阅读了它们(并尝试了它们没有成功)。

我的设置:Rails 4、Puma、Nginx、https://www 和 https:// 的 SSL 证书

我正在使用组合块,因此我获得了对 SSL 的重定向。但是,我想将https://www.domain.com 重定向到https://domain.com 在我添加重定向规则(返回 301 https://$host$request_uri;)之前,您将在下面看到的设置一切正常,然后我得到一个重定向循环。

我添加了“proxy_set_header X-Forwarded-Proto $scheme;”到我的 force_ssl 的@app 位置(在 Rails 配置文件中设置为 true),但这并没有解决问题。

非常感谢专家的建议,如果您发现我的设置有任何改进之处,除了修复重定向循环之外,请告诉我。

nginx.conf:

user root;
worker_processes 4;
pid /var/run/nginx.pid;

#setup where nginx will log errors to 
# and where the nginx process id resides
error_log  /var/log/nginx/error.log error;
#pid        /var/run/nginx.pid;

events {
  worker_connections  1024;
  accept_mutex off;
  use epoll;
}


http {
  include /etc/nginx/mime.types;
  types_hash_max_size 2048;
  default_type application/octet-stream;
  #access_log /tmp/nginx.access.log combined;

  # use the kernel sendfile
  sendfile      on;
  # prepend http headers before sendfile() 
  tcp_nopush    on;

  keepalive_timeout  25;
  tcp_nodelay        on;

  gzip on;
  gzip_http_version 1.0;
  gzip_proxied any;
  gzip_min_length 500;
  gzip_disable "MSIE [1-6]\.";
  gzip_types text/plain text/html text/xml text/css
             text/comma-separated-values
             text/javascript application/x-javascript
             application/atom+xml;

  #Hide server info
  server_tokens off;

    upstream app_server {
      server unix:/root/sites/mina_deploy/shared/tmp/sockets/puma.sock
        fail_timeout=0;
    }

  # configure the virtual host
  server {

    server_name domain.com www.domain.com 162.555.555.162;

    root /root/sites/mina_deploy/current/public;
    # port to listen for requests on
    listen 80 default deferred;
    listen 443 ssl;

    ####### THIS REDIRECT CAUSES A LOOP ########
    #return       301 https://$host$request_uri;

    ssl_certificate    /etc/ssl/ssl-bundle.crt;
    ssl_certificate_key     /etc/ssl/myserver.key;
    #enables all versions of TLS, but not SSLv2 or 3 which are weak and now deprecated.
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #Disables all weak ciphers
    ssl_ciphers 'AES128+EECDH:AES128+EDH';
    ssl_session_cache shared:SSL:10m;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/dhparam.pem;
    # maximum accepted body size of client request 
    client_max_body_size 4G;
    # the server will close connections after this time 
    keepalive_timeout 5;
    add_header Strict-Transport-Security max-age=63072000;
    #add_header X-Frame-Options DENY;
    add_header Access-Control-Allow-Origin '*';
    add_header X-Content-Type-Options nosniff;
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;

    location ~ ^/(system|assets)/  {
      gzip_static on;
      error_page 405 = $uri;
      expires max;
      add_header Cache-Control public;
      break;
    } 

   try_files $uri/index.html $uri @app;

     location @app {
     # pass to the upstream unicorn server mentioned above 
     proxy_pass http://app_server;
     proxy_redirect off;

     proxy_set_header   Host              $host;
     proxy_set_header   X-Real-IP         $remote_addr;
     proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
     proxy_set_header   X-Forwarded-Proto $scheme;

     proxy_read_timeout 300;
     }
    }
}

【问题讨论】:

    标签: ruby-on-rails ssl nginx devise puma


    【解决方案1】:

    我所做的是有多个服务器块。您提到您希望 www.domain.com 重定向到 domain.com。在这种情况下,我会做

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

    然后从原始块中的 server_name 中删除您的 www.domain.com。此外,我还会将您的重定向从 80 拆分到 443 的单独块中。因此,如果用户尝试访问 https://www.domain.com,您将重复此过程,您将拥有一台显示类似内容的服务器。

    server {
        listen 443;
        server_name www.domain.com;
        return 301 https://domain.com$request_uri;
    }
    

    还有一个用于侦听您想要的域上的 http 流量,但重定向到 https 流量。

    server {
        listen 80;
        server_name domain.com;
        return 301 https://domain.com$request_uri;
    }
    

    然后,您可以只在您希望每个人都去的服务器块中侦听端口 443,并且该块中没有重定向。

    你可以查看 nginx here 的文档,它会告诉你这是重写的正确方法

    回复你的评论,使用我写的三个块,在你原来的服务器块中,你需要删除

    server_name domain.com www.domain.com 162.555.555.162;
    

    也删除

    listen 80 deferred;
    

    并添加

    server_name domain.com;
    

    此外,只要确保您知道要使其正常工作,您必须让您的域和 www 子域指向您的服务器

    【讨论】:

    • Aaron,如果你是这个意思:server { listen 80; server_name www.domain.com;返回 301domain.com$request_uri; } 服务器 { 听 80; server_name domain.com;返回 301doamin.com$request_uri; } 服务器 { server_name www.domain.com;根/root/sites/mina_deploy/current/public;听 443 ssl;返回 301domain.com$request_uri; .......然后那对我不起作用。也对“从原始块中删除 www”感到困惑,然后你在下面添加它。您能否粘贴一个示例来说明您的意思?
    • 请在底部查看我编辑的答案。希望对您有所帮助。
    • 亚伦,成功了!我不得不将常规的 443 重定向块移到原始服务器块下方,但后来它起作用了。非常感谢!
    • 现在是背部的第二个痛点 :) - 虽然我在 curl 站点时有这个响应标头,但我仍然看不到 font-awesome 字体:HTTP/1.1 301 Moved Permanently 服务器: nginx 日期:2015 年 7 月 20 日星期一 22:40:32 GMT 内容类型:text/html 内容长度:178 连接:保持活动 位置:domain.com Strict-Transport-Security:max-age=63072000 访问控制-Allow-Origin: * X-Content-Type-Options: nosniff ...知道为什么吗?
    • 很高兴这对您有用 :) 这听起来像是另一个问题的主题,但这取决于您是托管资产还是从其他来源提取资产。如果您正在托管资产,并且您的应用程序负责提供它,我猜您需要将其添加到您的 assets.rb 中才能进行预编译。我建议您抛出另一个问题或寻找答案,了解为什么您在生产模式下看不到 Rails 应用程序的资产。
    猜你喜欢
    • 1970-01-01
    • 2016-04-02
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 2018-06-23
    • 2020-12-31
    相关资源
    最近更新 更多