3

My set up is as follows:

OS: CentOS 6.2 running on an OpenVZ virtual machine.

Web server: Nginx listening on port 8080

Reverse proxy: Varnish listening on port 80

The problem is that Varnish redirects my requests to port 8080 and this appears in the address bar like so http://mysite.com:8080/directory/, causing relative links on the site to include the port number (8080) in the request and thus bypassing Varnish.

The site is powered by WordPress.

How do I allow Varnish to use Nginx as the backend on port 8080 without appending the port number to the address?

Edit: Varnish is set up like so:

I have told the Varnish daemon to listen to port 80 by default.

VARNISH_VCL_CONF=/etc/varnish/default.vcl
#
# # Default address and port to bind to
# # Blank address means all IPv4 and IPv6 interfaces, otherwise specify
# # a host name, an IPv4 dotted quad, or an IPv6 address in brackets.
# VARNISH_LISTEN_ADDRESS=
VARNISH_LISTEN_PORT=80
#
# # Telnet admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082
#
# # Shared secret file for admin interface
VARNISH_SECRET_FILE=/etc/varnish/secret
#
# # The minimum number of worker threads to start
VARNISH_MIN_THREADS=1
#
# # The Maximum number of worker threads to start
VARNISH_MAX_THREADS=1000
#
# # Idle timeout for worker threads
VARNISH_THREAD_TIMEOUT=120
#
# # Cache file location
VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin
#
# # Cache file size: in bytes, optionally using k / M / G / T suffix,
# # or in percentage of available disk space using the % suffix.
VARNISH_STORAGE_SIZE=1G
#
# # Backend storage specification
VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
#
# # Default TTL used when the backend does not specify one
VARNISH_TTL=120

The VCL file that Varnish calls (through an include in default.vcl) consists of:

backend playwithbits {
     .host = "127.0.0.1";
     .port = "8080";
}
acl purge {
     "127.0.0.1";
}
sub vcl_recv {
     if (req.http.Host ~ "^(.*\.)?playwithbits\.com$") {
          set req.backend = playwithbits;
          set req.http.Host = regsub(req.http.Host, ":[0-9]+", ""); 
          if (req.request == "PURGE") {
               if (!client.ip ~ purge) {
                    error 405 "Not allowed.";
               }
               return(lookup);
          }
          if (req.url ~ "^/$") {
               unset req.http.cookie;
          }
     }
}
sub vcl_hit {
     if (req.http.Host ~ "^(.*\.)?playwithbits\.com$") {
          if (req.request == "PURGE") {
               set obj.ttl = 0s;
                    error 200 "Purged.";
          }
     }
}
sub vcl_miss {
     if (req.http.Host ~ "^(.*\.)?playwithbits\.com$") {
          if (req.request == "PURGE") {
               error 404 "Not in cache.";
          } 
          if (!(req.url ~ "wp-(login|admin)")) {
               unset req.http.cookie;
          }
          if (req.url ~ "^/[^?]+.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.|)$") {
               unset req.http.cookie;
               set req.url = regsub(req.url, "\?.$", "");
          }
          if (req.url ~ "^/$") {
               unset req.http.cookie;
          }
     }
}
sub vcl_fetch {
     if (req.http.Host ~ "^(.*\.)?playwithbits\.com$") {
          if (req.url ~ "^/$") {
               unset beresp.http.set-cookie;
          }
          if (!(req.url ~ "wp-(login|admin)")) {
               unset beresp.http.set-cookie;
          }
     }
}
2
  • Can you provide the configuration of varnish? Jun 14, 2012 at 20:44
  • @becomingwisest Updated question with information. Jun 14, 2012 at 20:50

4 Answers 4

1

So I managed to fix this problem, apologies to anyone who tried to find the answer as it wasn't quite the issue I diagnosed.

Before I applied varnish I told wordpress my site url was playwithbits.com:8080 so that it would correctly serve content, so it was appending the port number to all my URLs.

I completely forgot about this setting and once I removed the port number my site started to work normally.

2
  • Could you update the answer with the complete working configuration? Jun 16, 2012 at 21:49
  • @MikePennington That is the answer, I didn't change my varnish settings. I changed my site url in the worpress dashboard from plawithbits.com:8080 to plawithbits.com as it was performing redirects. Jun 17, 2012 at 9:06
0

Varnish itself never sends 301 answers by itself (unless you do some major VCL magic), so if you get a redirect, it has to come from your backend.

0

I see you have solved your particular problem. In some cases, like with some CGI programs, the port still shows up in the URL.

To avoid this you could run your Webserver (nginx) on 127.0.0.1:80 and Varnish on 1.2.3.4:80 (external IP).

-1

If your site is sending 301 or 302 response somewhere it should by spec send full URI with http, host and port. When nginx is proxying requests this can be fixed with proxy_redirect option. The same mechanism should exist in varnish. If not it's up to you to rewrite Location: header.

Could you provide heaer for response that sends client to port 8080? Is this headers or direct link somewhere on a site?

1
  • The functionality for purging Varnish at the most useful times (anytime you edit a file in wordpress) is already built into a well known WordPress plugin I use, building a similar plugin would add development time not worth the trouble. I am not sending 301 or 302 responses, the site works on both port 8080 and port 80 however if you request on port 8080 (The same content is loaded) you deal directly with Nginx and bypass Varnish. Jun 14, 2012 at 21:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .