Tell me more ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

The goal is to get PHP aware of the correct web-facing server_port.

The setup is:

nginx on port 443 reverse proxying to varnish on port 80 reverse proxying to nginx on port 8008 and running php-fpm as a fastcgi.

hitting 80 or 443 both work fine except that $_SERVER['SERVER_PORT'] in php is always showing 8008 by default. I can hard code a fastcgi_param server_port but then it will only be correct for 80 or 443 but not both.

# default
fastcgi_param  SERVER_PORT        $server_port;
# harcode to 80
fastcgi_param  SERVER_PORT        80;

How can I get the proper external-facing server port available to php?

share|improve this question

1 Answer

up vote 6 down vote accepted

Found my own answer

In the SSL nginx, this...

   proxy_set_header X-Forwarded-Proto https;

then in varnish, this...

 sub vcl_recv {
   if (req.http.X-Forwarded-Proto == "https" ) {
     set req.http.X-Forwarded-Port = "443";
   } else {
     set req.http.X-Forwarded-Port = "80";
     set req.http.X-Forwarded-Proto = "http";
   }
 }

then in the final nginx, this...

fastcgi_param  SERVER_PORT        $http_x_forwarded_port;
share|improve this answer
I created an account specifically to say thanks for this. I can't vote it up since I have no cred yet. I've been struggling to get nginx+ssl+varnish+magento working for a long time. Really can't thank you enough. This answer should be far, far higher in search results. There are many un-helpful posts out there which blame magento for causing re-direct loops, but this is the answer to get it working. – Raina Gustafson Apr 16 '12 at 0:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.