waterlink/rack-reverse-proxy

Receiving (binary?) symbols instead of HTML

Sexycrets opened this issue · 12 comments

Hi, I followed the instructions on the GitHub page to set up my config.ru

# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment',  __FILE__)
use Rack::Deflater
use Rack::ReverseProxy do  
   reverse_proxy /^\/blog(\/.*)$/, 'http://blog.sexycrets.com$1', opts={:preserve_host => true}
end
run Sxs::Application

when I hit .../blog/ the server fetches something that is returned on the browser as:
���5:B���V[o�6������ۛ.���M" ȭ]��-�v����-Q���THʊ����%�0���s�|�P���Og_��|�*]�t��?�0/��p������(���(��TD'^�����H��n�r��M���|= �D�MW�x(�\��J�� �K�g��5I�O�����P�5B��JGs]%9�Ќ���#ʩ���_�왕F��H�M�{�8X�Z�1��p�A���J�G�����[$ K,�(�� u�oh����w-��h���y��k�õ�!YK��� �UT�=�'i��дh��qJ}�������z��^M>ظN8f[0�>�֐��:���J^'�����?�� �rx�������Yش��bY�f"���o�,���I������1N }3o���;t_f�5Y��$�'�����/��������Xmy����T�<���K��(r�R���=%0�djR�6�Ϧ�Xt>\�6.���2�'�3�-������xZ+�,�dv<��K<���'>����������`i������D�ṡ7����Rog�)��ܽ�[����<�gZ�

I tried pointing to multiple blogs other than ours and I tried to run the RoR app from multiple servers in and outside of Heroku but the result does not change.

Any ideas? Thank you very much!

Just tried to run it with this code

require "rack/reverse_proxy"
require "rack/deflater"

use Rack::Deflater
use Rack::ReverseProxy do
   reverse_proxy /^\/blog(\/.*)$/, 'http://blog.sexycrets.com$1', opts={:preserve_host => true}
end

app = proc do |env|
  [ 200, {'Content-Type' => 'text/plain'}, ["b"] ]
end
run app

And everything works just fine.

  • What is happening in your config/environment ?
  • What OS are you running on? What is your default encoding?

Thank you for your quick reply. Glad it works for you, so it has to be something on my end.

As far as my config/production:

  # Code is not reloaded between requests
  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = true

  # Generate digests for assets URLs
  config.assets.digest = true

 # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation can not be found)
  config.i18n.fallbacks = true

I am running on Mac locally and on Heroku I get the same result.

My default encoding in application.rb is:

# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"

Anything specific you think is worth looking into? Thanks!

Doing more research I am starting to think that it might have something to do with SSL. When you said it works just fine did you do it from your local server or Heroku? Do you use SSL in your server?

On my end I do not. But it gives me the same results (returning those symbols) on both my local server (no SSL) as well as Heroku.

Can you take a look which middlewares you are using, for Rails rake middleware should be enough, for generic rack application see: http://stackoverflow.com/questions/4008028/get-ordered-list-of-middleware-in-a-generic-rack-application

Can you post output (as a gist) of curl -vvv http://localhost:YOUR_PORT/blog/ ?

Hi Waterlink! Thanks for your suggestions.

I tried the basic setup as you suggested in application.rb:

class Application < Rails::Application
    # Reverse proxy for blog
    config.middleware.insert_before(Rack::Lock, Rack::ReverseProxy) do
      reverse_proxy_options preserve_host: true
      reverse_proxy '/wiki', 'http://blog.sexycrets.com/'
    end

The config.ru is the same apart from the $1 that I removed from the blog url):

require ::File.expand_path('../config/environment',  __FILE__)
use Rack::Deflater

use Rack::ReverseProxy do  
       reverse_proxy /^\/blog(\/.*)$/, 'http://blog.sexycrets.com', opts={:preserve_host => true}
end
app = proc do |env|
  [ 200, {'Content-Type' => 'text/plain'}, "b" ]
end

run Sxs::Application

Interesting enough I get the right html from curl:
https://gist.github.com/Sexycrets/2282aab906160c14f43d
(but in safari, firefox and chrome still the weird symbols)

This is the output of 'rake middleware':
https://gist.github.com/Sexycrets/1c5dcced7ac2566022e2

I still can't figure this out.

I tried again on the browser with:
view-source:http://dev.sexycrets.com:3000/blog/
to try to bypass any javascript but I still get the weird symbols.
Same symbols using Postman.

While with
$ curl dev.sexycrets.com:3000/blog/
I still get the source code.

Do you have any idea even on how to debug this? Is there any chance it does not get decoded properly from gzip?

Solved following instructions here:
http://nicolasmlv.com/wordpress-in-your-rails-app/

He used:
gem 'rack-reverse-proxy', github:"drewwed/rack-reverse-proxy", require: 'rack/reverse_proxy

Re-opening issue, since I'm still going to fix that.

Hm, after a bit of research, I see that I have recently merged this exact change here, but only spec, since it was green once I added it. So I expect:

  • either spec did not catch the actual problem,
  • or the issue here is different, that is not present in mentioned fork.

So there is this file: https://gist.github.com/waterlink/32a342bba3cc75ae13a6#file-fetchhead__head-diff

I will try to spot a difference there a bit later.