首页 文章

如何使用Rails,AWS Elastic Beanstalk和Cloudflare进行SSL

提问于
浏览
9

我有一个托管在使用Ruby on Rails构建的Elastic Beanstalk上的站点 . 我设置Cloudflare来配置DNS并提供CDN . Cloudflare还提供SSL .

我无法使用我的应用程序使用SSL .

随着Cloudflare的SSL设置为“灵活”,我可以加载我的主页,但是当我尝试登录时,我收到这些错误(为简洁起见编辑):

INFO -- :  Started POST "/users/sign_in" for xxx.xxx.146.132 at 2018-03-19 16:45:24 +0000
INFO -- :  Processing by Users::SessionsController#create as HTML
INFO -- :    Parameters: {"utf8"=>"✓", "authenticity_token"=>"f92CTIe5qlp7C624DZzZM2oWdFMcq6PhyfOJI16saV32yugMmJlenL/F3gTeBBsAjaAw92P1vncWBzI+JnK8wA==", "user"=>{"email"=>"test@test.com", "password"=>"[FILTERED]"}, "commit"=>"Log in"}
WARN -- :  HTTP Origin header (https://[MY_URL].com) didn't match request.base_url (http://[MY_URL].com)
INFO -- :  Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)  
FATAL -- :  ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):

如果我将Cloudflare的SSL设置为“Full”,我会在Cloudflare生成的页面中收到502错误(参见图片) .

enter image description here

我遇到了这个网站(http://til.obiefernandez.com/posts/875a2a69af-cloudflare-flexible-ssl-mode-breaks-rails-5-csrf)似乎有完全相同的问题,但设置为"full"并没有帮助我 .

我已经尝试在/config/environments/production.rb中设置 config.force_ssl = true . 该设置不允许任何访问该网站 . 只显示来自Cloudflare的相同502错误页面,而我的 生产环境 或nginx日志中没有任何内容 .

我已经尝试过使用自定义nginx配置,但还没有到达任何地方 . 这是我最新的nginx confix尝试:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    index   index.html index.htm;

    server {
        listen       80 ;
        listen       [::]:80 ;
        server_name  localhost;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;


        location / {
          proxy_pass        http://localhost;
          proxy_set_header  Host $host;
          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header  X-Forwarded-Proto $scheme;
          proxy_set_header  X-Forwarded-Ssl on; # Optional
          proxy_set_header  X-Forwarded-Port $server_port;
          proxy_set_header  X-Forwarded-Host $host;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

有人可以帮忙吗?我确定我在这里遗漏了一些明显的东西 .

1 回答

  • -2

    502 Error :此错误表明Cloudfare服务器无法读取您的rails服务器发送的响应,基本上当您选择 Full SSL 时,Cloudflare希望响应在SSL中,但此处rails应用程序发送NON-SSL(HTTP)响应Cloudflare无法阅读 .

    我阅读了您分享的文章,看起来rails出于安全原因不允许使用灵活的SSL .

    Flexible SSLflexible SSL 中,您无需使用SSL证书(HTTPS)保护您的rails应用,但您的访问者仍然会将该网站视为启用了HTTPS .

    enter image description here

    灵活的SSL:访问者与Cloudflare之间的安全连接,但Cloudflare与您的Web服务器之间没有安全连接 . 您不需要在Web服务器上拥有SSL证书,但访问者仍然将该站点视为启用了HTTPS .

    Full SSL
    article中所述,如果启用 Full SSL ,则需要配置rails config.force_ssl = true 以使用自签名SSL证书,或者您可以免费获取letsencrypt的证书,一旦您这样做,您需要更改您的Nginx以在HTTPS端口上运行这应该修复502错误 .

    这是一个关于Using HTTPs with Ruby on Rails的教程

    enter image description here

    完整SSL:访问者与Cloudflare之间的安全连接,以及Cloudflare与您的Web服务器之间的安全连接(但未经过身份验证) . 您需要将服务器配置为应答HTTPS连接,至少使用自签名证书 .

    Image Source

相关问题