0

I've got an apache running wordpress on port 90. I've put a varnish cache in front of it and am running varnish on port 80.

I'm using the 4.0 vcl from here: https://github.com/nicolargo/varnish-nginx-wordpress/blob/master/varnish/varnish4-wordpress

When I request my site on :80 I get 301 redirected to :90 ... Surely that's not the point of varnish? How do I avoid this and stay on :80??

I'm a complete newbie to varnish / vcl so I unfortunately have no idea what to look for. I'm sure it's in the VCL though :)

Related post (but not answer on here either): Why is Varnish redirecting as 301?

Community
  • 1
  • 1

1 Answers1

1

It is surely not in VCL. However, do note that it's much better to start building from the provided default.vcl, adding bits by bits, to understand how it works.

As for why it doesn't work and ways to fix it.

Different ports (duh).

Option #1. Tell your PHP what the real port is.

Perhaps, the most easy fix would be to edit wp-config.php and tell the Wordpress where the request really came from. Add at the top of the file right after <?php:

$_SERVER['SERVER_PORT'] = 80;

Option #2. Just use the same ports.

You can actually use same port number(!) in both Varnish and Apache. Simply bind Varnish and Apache onto different interfaces, but same port numbers. To a Linux machine those are different ports, and there is no issue using same port number.

So Apache will have:

Listen 127.0.0.1:80

Varnish will have in the VCL:

backend default {
    .host = "127.0.0.1";
    .port = "80";
}

Option #3. Don't use Apache. Nginx to the rescue.

A not so easy fix perhaps would be switching to Nginx which can easily circumvent the "port in redirection" problem via simply one line of configuration:

port_in_redirect off;

Wordpress base URLs.

If the Wordpress site URL (in wp-admin) is set to include port number, then naturally that's where it will redirect you. No port number should be present in Wordpress site URL.

Bonus Tip: Fix Wordpress Jetpack in Varnish.

Danila Vershinin
  • 8,725
  • 2
  • 29
  • 35
  • "You can actually use same port number(!) in both Varnish and Apache." Nope, you're setting the `backend` which is the source content from which Varnish will pull from. Changing the Varnish port is done through something like `/etc/sysconfig/varnish` or through the startup script – Don Wilson Jan 10 '19 at 00:52
  • @DonWilson what "Nope"? :) I was mentioning "different interfaces". Which means that if you have Varnish set to listen on *only* 1.2.3.4 (your external IP) and port 80, you can *still* have Apache listen on 127.0.0.1 and port 80. Because essentially those are different ports (since they are on different interfaces) – Danila Vershinin Jan 10 '19 at 11:18