Trick a Rails app to think about it on another port

I have a port-based Rails application 8080that I need to trick in order to consider it running on port 80.

I run Varnish on the port 80and redirect requests for nginx to the port 8080, but when the user tries to log in with OmniAuth, and the self-generating device creates the URL for redirection to the server, he thinks that it is on port 8080, which the user will then see.

Is there a way to trick a Rails application to hard-code a port like 80 (I would think that this is bad practice) or does nginx redirect the request as if it is running on port 80?

Since I am not starting the nginx proxy for the Rails application, I cannot think of a way to trick the port.

Has anyone encountered this problem before, if so, what configuration is needed to fix it?

Thanks in advance!

EDIT: Both nginx and Varnish are running on the same server.

+4
source share
4 answers

I have the same setup with Varnish on port 80 and nginx on port 8080, and OmniAuth (no Devise) does the same. I tried to install X-Forwarded-Port, etc. In Varnish and fastcgi_param SERVER_PORT 80;in nginx, both without success. Another part of my setup is Passenger (which you didn't mention), but if you really use Passenger, you can use:

passenger_set_cgi_param SERVER_PORT 80; 

( , http, , server.)

http://modrails.com/documentation/Users%20guide%20Nginx.html#passenger_set_cgi_param

+7

X-Forwarded-Port . . , Google " x- -".

, , X-Forwarded-For X-Forwarded-Proto.


X-Forwarded-For, X-Forwarded-Proto X-Forwarded-Port - - HTTP, Nginx, Squid Varnish, HTTP- "back-end", Rails, , .

, , Nginx Rails-. Rails Thin 127.0.0.1:8080, Nginx 0.0.0.0:80 HTTP 0.0.0.0:443 HTTPS. Nginx Rails-. Rails , IP- 127.0.0.1, 8080, http, 1.2.3.4 https on 443. , Nginx :

X-Forwarded-For: 1.2.3.4
X-Forwarded-Scheme: https
X-Forwarded-Port: 443

Rails .

, , , .

+2

- , .

, apache ...

<VirtualHost *:80>
 ServerName <name>
 DocumentRoot /home/deploy/<name>

 PassengerEnabled off
 ProxyPass / http://127.0.0.1:<port>/
 ProxyPassReverse / http://127.0.0.1:<port>/

</VirtualHost>

:

passenger start -e staging -p 3003 -d
0

, 8080. Rails ( OmniAuth/Devise gem) , 80 ( , ).

; Apache (mod_proxy), ProxyPassReverse -80. , mod_proxy_html port-8080 HTML- -80.

, Varnish VCL - :

sub vcl_fetch {

  ...

  #Rewrite redirect from port 8080 to port 80
  if ( obj.http.Location ~ "^http://[^:]+:8080/.*" ) {
    set obj.http.Location = regsub(obj.http.Location, ""^(http://[^:]+):8080(/.*)","\1\2");
  }
}

( , obj beresp, >= 2.1)

If you need to rewrite HTML pages, it will be much harder to do completely with varnish.

0
source

All Articles