Rails - Puma - Nginx:如何将所有内容重定向到https://www.example.com?

时间:2017-06-23 07:27:28

标签: ruby-on-rails nginx puma

我有一个Rails应用程序,一切正常。

我的nginx配置文件:

upstream puma-enziin {
  server unix:///home/kevin/websites/enziin/shared/tmp/sockets/enziin-puma.sock;
}

server {
  listen 80;
  server_name enziin.com;

  root /home/kevin/websites/enziin/current/public;

  access_log /home/kevin/websites/enziin/current/log/nginx.access.log;
  error_log /home/kevin/websites/enziin/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;

    # Some browsers still send conditional GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma-enziin;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/enziin.com-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/enziin.com-0001/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot

}

我的问题:如何将所有内容重定向到https://www.example.com?

使用以上配置,它可以重定向到https://example.com,而不是https://www.example.com

感谢您的回答。

1 个答案:

答案 0 :(得分:0)

改变这个:

server_name enziin.com;

到此:

server_name enziin.com www.example.com;

以上使nginx服务于两台主机。目前,它仅为enzinn.com映射。在此之后,您的现有配置也适用于www.example.com,并将开始重定向到其https等效项。

(确保www.example.com的DNS记录指向您的nginx)