1

I am running WordPress under the following configuration:

Varnish (Port 80) -> nginx (Port 8080)

This is all working great except when some plugin or other tries to get my server name using $_SERVER['SERVER_NAME']. When that happens, it is getting back _:8080 as my server name and port. This is what the nginx config has:

server {
listen 8080;
server_name _;

What I need it to return is domain.com but if I change the nginx config, obviously, it stops listening on the port it needs to be (8080) (but changing the server_name works fine).

How can I have nginx listen on 8080 but have $_SERVER['SERVER_NAME'] return the correct port (or rather, not append the wrong port)?

1

_:8080 is the correct value for $_SERVER['SERVER_NAME'] in this configuration. It's explicitly defined to return the value of server_name. From the documentation:

The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.

It sounds like you are looking for $_SERVER['HTTP_HOST']. Note that you also need to have not configured Varnish to alter the Host: request header.

|improve this answer|||||
  • I agree... but I can't control what plugin developers are using to get my server name. Which is why I am looking for a work-around to set it to what they are looking for. I'll check into what we are or are not doing with Host in varnish. Thanks! – Andy Affleck Jul 31 '18 at 18:49
  • 1
    Well, you can't control them directly, but if they're trying to look at SERVER_NAME then that's a pretty serious bug. Plugins should call get_site_url() to find the site URL, and parse it if necessary. You can always report bugs. You can't guarantee a developer will fix bugs, though... – Michael Hampton Jul 31 '18 at 18:50
  • And that's exactly what they did! I am updating the site now to test it out. – Andy Affleck Jul 31 '18 at 19:02
0

You can override the default value of $_SERVER['SERVER_NAME'] by adding a fastcgi_param directive after the include fastcgi_params; statement.

For example:

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param  SERVER_NAME  $http_host;
    ...
}

The value defined in the include file will be silently overwritten with the value of $http_host.

|improve this answer|||||
  • Excellent! I'll try this for the other plugin that does this (see below, one of the devs already updated their code)! – Andy Affleck Jul 31 '18 at 19:12
  • Keep in mind that this might not fix a broken plugin that is inappropriately depending on SERVER_NAME. For instance, this value will never have anything useful on a WP multisite subdomain install. The developer will still need to fix the bug in that case. – Michael Hampton Jul 31 '18 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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