使用CloudFlare,Nginx和Thin,附带80端口的httpsurl

我有一个运行Ruby on Rails项目的瘦服务器的Nginx设置。 Nginx目前是我的SSL的端点。

我最近在web服务器上遇到了一些攻击问题,所以我决定添加CloudFlare。 它目前使用cloudflare中的“严格SSL”(客户端-CF链接和CF-web服务器链接中的ssl)。 现在,浏览我的网站时,但是当我使用Oauth的Facebook,G +等。redirect链接显示为: https://example.com:80/destination/url : https://example.com:80/destination/url :80/ destination/url(注意:80 )。 当我不使用CloudFlare时不会发生这种情况。

我能做些什么来解决这个问题? 这是CloudFlare或Nginx的问题吗? 我发现这个post ,但我不能逆向工程它nginx

我目前的nginx-setup:

 server { listen 80 default; server_name .example.com; ## redirect http to https ## return 301 https://example.com$request_uri; } server { listen 443 ssl; server_name .example.com; client_max_body_size 20M; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA384:EECDH+aRSA+SHA256:EECDH+aRSA+RC4:EECDH:EDH+aRSA:RC4:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS; ssl_certificate /home/my_user/.ssl/example.crt; ssl_certificate_key /home/my_user/.ssl/example.key; access_log /var/www/example_server/log/access.log; error_log /var/www/example_server/log/error.log; root /var/www/example_server; index index.html; if ($host != 'example.com' ) { return 301 https://example.com$request_uri; } location / { proxy_set_header X-Real-IP $remote_addr; 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; try_files /system/maintenance.html $uri $uri/index.html $uri.html @ruby; } location @ruby { proxy_pass http://example.com; } } 

在我的nginx.conf中:

 #default stuff http{ set_real_ip_from *IP address for cloudflare*; #.... port_in_redirect off; #more default stuff 

作为参考,当前请求path:客户端请求-SSL-> CloudFlare -SSL-> Nginx -non-SSL-> Thin -non-ssl-> Nginx -SSL-> CloudFlare -SSL->客户端响应

这似乎是导致这个问题的Omniauthgem中的一个问题。 为什么只有当我使用CloudFlare时才会出现问题,而不是在closures时出现问题,因此我无法胜任。

无论如何,这是github上的问题:

https://github.com/intridea/omniauth/issues/101

我的解决scheme

在rails应用程序中,使用以下内容创build一个新的初始化程序( config/initializers/omniauth_fix.rb ):

 if Rails.env.production? module OmniAuth module Strategy def full_host uri = URI.parse(request.url) uri.path = '' uri.query = nil uri.port = (uri.scheme == 'https' ? 443 : 80) uri.to_s end end end end 

这本质上是一个monkeypatch在这个问题上。 希望它将在未来的omniauth版本中得到修复。

这似乎很奇怪,因为我们不添加任何redirect,除非你特意要求我们用PageRule来做 。 我会build议打开支持CloudFlare支持票,所以我们可以检查出来。