5

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?

MDrollette
  • 325
  • 3
  • 12

1 Answers1

15

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;
MDrollette
  • 325
  • 3
  • 12
  • 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 00:16