i want to use varnish in my symfony app for cache control,
i have used virtual host, here is my virtual host file.
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName varnish-local.com
ServerAlias www.varnish-local.com
DocumentRoot /var/www/html/varnish-software/public
DirectoryIndex index.php
<Directory /var/www/html/varnish-software/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/symfony_error.log
CustomLog ${APACHE_LOG_DIR}/symfony_access.log combined </VirtualHost>
vcl 4.1;
backend default {
.host = "127.0.0.1";
.port = "8000";
}
sub vcl_recv {
if (req.method == "PURGE") {
return (purge);
}
}
sub vcl_backend_response {
if (beresp.status == 404 || beresp.status == 500) {
set beresp.ttl = 0s;
}
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
when i run my app from symfony:serve command, then i see that my varnish serer is running through http://127.0.0.1:6081/ but how can i use it with my 8000 port, also how can i use it with my virtual host.
when i run varnish-local.com i want to get data from varnish server, so instead of using 127.0.0.1:6081 i want to use varnish in my varnish-local.com.
so can anyone please provide information regarding this issue, thank you.
Running Varnish on port 8000
The built-in listening port for Varnish is indeed
6081. This is done to avoid port clashes with existing servers.The
-aruntime parameter can be used to set it to values like80or8000. Here's an examplevarnishdcommand including the common runtime parameters:This command will probably be part of your systemd configuration. You can choose to run this command directly on your local machine for testing.
A better VCL file
The VCL code you're using is pretty bare bones. I suggest you use the example VCL file we created at Varnish Software: https://www.varnish-software.com/developers/tutorials/example-vcl-template/
While it's not framework-centrics, it does work pretty will out-of-the-box. I'm using it myself for a Symfony website I built.
If you're using specific cookies in your Symfony application, you might need some site-specific tuning though.
Virtual hosts & Varnish
Varnish supports virtual hosts out-of-the-box and will create a hash that identifies a cached object based on the URL and the
Hostheader.It's this
Hostheader that is used by HTTP to enforce multiple domains on a single server. That's basically what Virtual hosting does.