rails 项目搭建 https

一、申请 https 证书

使用免费的 Let's encrypt, 参考:https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

二、rails 项目配置 config/environments/production.rb

config.action_cable.url ="wss://www.example.com/cable”

config.action_cable.allowed_request_origins = [ "http://www.example.com", "https://www.example.com" ]

三、服务器上nginx配置 nginx/example.com.conf

# 环境:nginx + puma + rails + action cable

upstream example {

  server unix:///var/www/example/shared/tmp/sockets/puma.sock fail_timeout=0;

}

server {

  listen 80;

  listen 443 ssl;

  server_name example.com;

  root /var/www/example/current/public;

  access_log /var/www/example/shared/log/nginx_access.log;

  error_log /var/www/example/shared/log/nginx_error.log;

  ssl on;

  #listen 443 ssl; # managed by Certbot

  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot

  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot

  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

  location ^~ /assets/ {

    gzip_static on;

    expires max;

    add_header Cache-Control public;

  }

  location /cable {

    proxy_pass http://example/cable;

    proxy_http_version 1.1;

    proxy_set_header Upgrade $http_upgrade;

    proxy_set_header Connection "Upgrade";

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_set_header Host $http_host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-Proto https;

    proxy_redirect off;

}

  location ~ ^/(uploads)/  {

    expires max;

    break;

  }

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

location @example {

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_set_header Host $http_host;

    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_redirect off;

  proxy_pass http://example;

  }

  error_page 500 502 503 504 /500.html;

  client_max_body_size 20M;

  keepalive_timeout 10;

}


参考:

http://railscasts.com/episodes/357-adding-ssl?view=asciicast

https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

https://www.pluralsight.com/guides/ruby-ruby-on-rails/using-https-with-ruby-on-rails

https://certbot.eff.org/docs/using.html#getting-certificates-and-choosing-plugins

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,534评论 19 139
  • Page 1:nginx 服务器安装及配置文件详解 CentOS 6.2 x86_64 安装 nginx 1.1 ...
    xiaojianxu阅读 12,716评论 1 41
  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 6,313评论 0 9
  • 我的初中班主任叫李传喜,95年那会,解放了小升初考试,开始“一锅端”,考试也是流程,考好、考不好都要读初中。...
    唐四姑阅读 2,948评论 0 0
  • 2017年9月6号,星期三,天气:雨 昨天和我们班的优秀家长:薛智一妈妈,宋浩言妈妈一起去了学校,一到教室里看到了...
    于浩晨阅读 2,444评论 0 7