I am trying to use Varnish as a reverse caching proxy for my nginx install on Ubuntu 10.10, I have set up Varnish on port 8080 for testing and nginx is operating normally on port 80.
My nginx.conf:
user {user} {group};
worker_processes 16;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
client_max_body_size 4M;
client_body_buffer_size 128k;
access_log /var/log/nginx/access.log;
gzip on;
gzip_proxied any;
gzip_comp_level 2;
gzip_disable "MSIE [1-6].(?!.*SV1)";
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml$
sendfile on;
tcp_nopush off;
keepalive_timeout 30;
tcp_nodelay on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
My site’s .conf file:
server {
listen 80;
server_name www.mylesgray.com dev.mylesgray.com;
access_log /var/www/mylesgray.com/logs/access.log;
error_log /var/www/mylesgray.com/logs/error.log;
location / {
root /var/www/mylesgray.com/public;
index index index.php;
try_files $uri/ $uri index.php?q=$uri&$args;
port_in_redirect off;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
expires 30d;
root /var/www/mylesgray.com/public;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(.*)$;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/mylesgray.com/public/$fastcgi_script_name;
include fastcgi_params;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
location ~ /.ht {
deny all;
}
location ~ /.git {
deny all;
}
# include /var/www/mylesgray.com/public/nginx.conf;
}
upstream backend {
server 127.0.0.1:9000;
}
My Varnish conf:
backend default {
.host = "localhost";
.port = "80";
}
acl purge {
"localhost";
}
sub vcl_recv {
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.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
}
sub vcl_miss {
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.url ~ "^/$") {
unset beresp.http.set-cookie;
}
if (!(req.url ~ "wp-(login|admin)")) {
unset beresp.http.set-cookie;
}
}
So with all that when you goto my site on port 8080: http://www.mylesgray.com:8080 it just redirects to plain old: http://www.mylesgray.com What it should be doing instead is that (as it stands) if I access port 80 I should get nginx serving files only, and if I access 8080 I should have nginx + varnish serving files.
I’m doing this to test the performance benefits of varnish over plain nginx.
Any help much appreciated!
My best guess would be some kind of SEO plugin that’s thinking “oh a request on port 8080? That shouldn’t be, I’ll be kind and rewrite to port 80”:
You may want to normalize your host headers by stripping the port numbers:
If needed, have a look at some more examples at these configuration examples for Varnish