繁体   English   中英

使用SSL将www重定向到Nginx上的非www会提供重定向循环

[英]Redirect www to non-www on Nginx with SSL gives redirect loop

如果这看起来像是似曾相识的话,我深表歉意。 有很多关于类似问题的文章,我都读了(并尝试了失败)。

我的设置: https:// www和https://的Rails 4,Puma,Nginx,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;
     }
    }
}

我所做的是有多个服务器块。 您提到希望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的文档,这将向您显示这是重写的正确方法

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

server_name domain.com www.domain.com 162.555.555.162;

并删除

listen 80 deferred;

并添加

server_name domain.com;

另外,只要确保您知道要执行此操作,就必须将域和www子域指向服务器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM