4

I have a setup (php application) with

:: varnish (port 80) -> apache port (8080)

By default apache adds trailing slashes to directories but when it does it redirect with the port. e.g.

http:/www.domain.com/folder redirect to http:/www.domain.com:8080/folder/

This url with the port causes issues.

So I tried adding the directive DirectorySlash Off in the .htaccess file but the application doesn't play nice anymore. e.g. broken links etc etc

I then tried to update the VCL as described on http://danielmiessler.com/blog/adding-a-trailing-slash-to-directories-using-varnish/

sub vcl_recv {
if ((req.url ~ "/directory" ) && (! (req.url ~ "index.php"))){
   set req.url = req.url "/";
}
}

but I get the error

Message from VCC-compiler:
Expected ';' got '"/"'
(program line 174), at
('input' Line 14 Pos 26)
   set req.url = req.url "/";
-------------------------###-

Does anyone have any suggestions?

Thanks

space_balls
  • 1,383
  • 2
  • 14
  • 29

1 Answers1

1

I'd recommend keeping the redirect logic out of Varnish; the fundamental problem is that redirects based on the DirectorySlash setting will add the internal Apache port (8080) in the redirect.

To fix, try adding the following Apache setting to your Apache config (won't work in htaccess):

UseCanonicalName Off

Per Apache's documentation on UseCanonicalName:

With UseCanonicalName Off Apache httpd will form self-referential URLs using the hostname and port supplied by the client if any are supplied

Which means that apache should respect the incoming client port (80) forwarded by Varnish, even when doing the DirectorySlash redirect.

Jeff Sisson
  • 1,616
  • 11
  • 22
  • 1
    Hi Jeff, thanks very much for advice. It actually made me review my vhost file in even more detail and it looked like the serverName and serverAlias were not set. Once they were set everything worked as expected. – space_balls Jul 30 '14 at 05:23
  • 1
    After setting the `UseCanonicalName` to `Off` in `sites-enabled/000-default.conf` and `apache2.conf` (restarted Apache and varnish of course), the 8080 redirection still happens. Any clue? – Raptor Jul 27 '15 at 03:53