使用 Nginx 和 Puma 在 Rails 应用的 Ruby 上启用 SSL

Enable SSL on Ruby on Rails app with Nginx and Puma

这是我的 Nginx 配置文件:

upstream app {
  server unix:/home/deploy/example_app/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {

  listen 80;
  listen 443 ssl;
  # ssl on;
  server_name localhost example.com www.example.com;

  root /home/deploy/example_app/current/public;

  ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;

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

  location / {
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection '';
    proxy_pass http://app;
  }

  location /.well-known { allow all; }

  location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

证书的路径是正确的,但是当我访问 https://example.com 时,它会永远加载。

我的 SSL 设置有问题吗?

尝试SSL checker检查SSL是否有问题。

它将验证您的服务器证书并告诉您问题出在哪里。

就我而言,我确保域名正确解析为IP。我的 Ubuntu 上的防火墙允许端口 443,并确保 Let's Encrypt 的证书正确安装在我的服务器上。

我使用 AWS EC2,我花了几个小时才发现我忘记将 HTTPS 添加到 EC2 实例的 入站规则 安全组.

在AWS EC2上添加规则后,我又可以访问https://mydomain了