Port_in_redirect not working?

Hi all,

I posted this question on SO but haven’t received much attention, so I
figured here would be better.

Basically I am attempting to run varnish on port 80 and my nginx server
on 8080, however any request to 80 redirects and appends the port to the
URL.

i.e. http://site.com/ -> http://site.com:8080/

From everything I’ve read it seems that ‘port_in_redirect off’ should
disable this, however it does not appear to be working. I’ve used a
workaround for php by using “fastcgi_param SERVER_PORT 80;”, which
works, but requests to my “location /” are getting the port appended.

My server setup is:

server {
listen 8080 default;
server_name “” xxx.xxx.xxx.xxx; #just using IP here (no domain
yet)

port_in_redirect off;
server_name_in_redirect off;

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


location / {
        root   /var/www/site/html/;
        index  index index.php;
        try_files $uri/ $uri /index.php?q=$uri&$args;
}

}
upstream backend {
server 127.0.0.1:9000;
}

I have tried port_in_redirect in both the “location /” and the server
blocks, but neither works.

Can anyone shed any light on this problem? I’m currently using nginx
v1.0.9 on a ubuntu server.

[Link to SO question (full .conf there):
http://stackoverflow.com/questions/8026763/nginx-port-in-redirect-not-working]

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,217928,217928#msg-217928

Hello!

On Mon, Nov 07, 2011 at 11:17:28AM -0500, olan wrote:

yet)
try_files $uri/ $uri /index.php?q=$uri&$args;
v1.0.9 on a ubuntu server.

[Link to SO question (full .conf there):
http://stackoverflow.com/questions/8026763/nginx-port-in-redirect-not-working]

The “port_in_redirect” directive only alters behaviour of nginx
itself (for trailing slash redirects, rewrites and so on), and
it’s expected that it does nothing for redirects returned by php.

With “fastcgi_param SERVER_PORT 80;” php should be fine, and I
suspect that problem you see is actually related to your browser
(or varnish) cache sitting here from previous configurations.

If clearing cache doesn’t help, please provide debug log (see
http://wiki.nginx.org/Debugging), it will show what goes on here.

Maxim D.

Hi Maxim, thanks for getting back to me.

I’ve enabled debugging and the output is here:
http://pastebin.com/Dmw2nJMY. I’ve cleared all caches and restarted
varnish, nginx and php5-fpm and mysql.

I didn’t explain myself very well earlier. The “fastcgi_param
SERVER_PORT 80” does work for php pages (it’s in my location ~ .php$
block) but the problem I’m having is with my server root.

http://site.com/page.php <-- works fine
http://site.com/ redirects to http://site.com:8080
http://site.com/index.php redirects to http://site.com:8080

One thing I noticed is “location /” block is used first, the index.php
is tried, and then the “location ~ .php$” block processes this page. I
would have thought that the SERVER_PORT would have rewritten the port
used at this stage…?

Any help is greatly appreciated!
Thanks,

Olan

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,217928,217939#msg-217939

On 7 November 2011 22:44, Nginx U. [email protected] wrote:

You can try tricking php by putting $_SERVER[“SERVER_PORT”] right at
the top of your index php (or use autoprepend in php.ini so it applies
to all php files).

That should read - You can try tricking php by putting
$_SERVER[“SERVER_PORT”] = “80”;

On 7 November 2011 21:25, olan [email protected] wrote:

Hi Maxim, thanks for getting back to me.

I’ve enabled debugging and the output is here:
http://pastebin.com/Dmw2nJMY. I’ve cleared all caches and restarted
varnish, nginx and php5-fpm and mysql.

Line 245 of your debug shows the issue. php is sending a header - http
fastcgi header: “Location: http://XXX.XXX.XXX.XXX:8080/”. The
proxy_redirect directive handles this for proxy_pass setups but there
isn’t an equivalent for fastcgi.

You can try tricking php by putting $_SERVER[“SERVER_PORT”] right at
the top of your index php (or use autoprepend in php.ini so it applies
to all php files).

I just had a problem with this with a proxy setup as the
proxy_redirect only accepts variables in the redirect part such that I
had to do …

proxy_redirect http://example-1.com:8080/ http://example-1.com/;
proxy_redirect http://example-2.com:8080/ http://example-2.com/;
proxy_redirect http://example-3.com:8080/ http://example-3.com/;

proxy_redirect http://example-n.com:8080/ http://example-n.com/;

If the directive accepted variables in the original uri part, a single
line …

proxy_redirect http://:8080$host/ http://$host/;

… would have done the job and given flexibility. I have to remember
to add every domain I create to the list.

Anyway, I digress. Try the trick on PHP. Nothing can be done in Nginx
until a fastcgi_redirect directive is introduced.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs