Tikejhya: Ashish Nepal

Knowledgebase

Month: April 2013

Varnish Tips/Tricks

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

# Write log into some file
varnishncsa -w /tmp/test.varnish.log

#Varnish throws 503 page
ulimit -n 100000
service varnish restart

# Varnishadin CLI
varnishadm -S /etc/varnish/secret -T 127.0.0.1:8888

# Syntax Check
varnishd -C -f /etc/varnish/default.vcl

varnish [Installation & Configuraiton] – DRAFT

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Varnish has recently been applauded, and works immense in-terms of D/DOS protection, socket tuning, load-balancing, and most importantly caching.

Currently in this article i am setting up Varnish as cache/loadblancing which will forward clean request to webserver.

Varnish -> Web Server
2x 2x

There are not many files that needs to be changed to run varnish by default varnish setup gives quite intresting config which has ability to handle request and cache.

vi /etc/sysconfig/varnish

# Maximum number of open files
NFILES=131072

# Locked shared memory
# Default log size is 82MB + header
MEMLOCK=82000

# Maximum size of corefile
# DAEMON_COREFILE_LIMIT="unlimited"

# Reload VCL set 1 to reload,without restart.
RELOAD_VCL=1

# Main configuration file.
VARNISH_VCL_CONF=/etc/varnish/default.vcl

# Default address and port to bind
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=50

# The Maximum number of worker threads to start
VARNISH_MAX_THREADS=1000

# Idle timeout for worker threads
VARNISH_THREAD_TIMEOUT=120

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

# Cache file location if using file cache
#VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin

# Cache size: in bytes, optionally using k / M / G / T suffix,
# or in percentage of available disk space using the % suffix.
VARNISH_STORAGE_SIZE=7G

# Backend storage specification
# malloc runs from RAM, file from file
VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"
#VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"

# Default TTL used when the backend does not specify one
VARNISH_TTL=120

vi /etc/varnish/default.vcl

# Define the list of web servers
# Port 80 Backend Servers
backend web1 { .host = “192.168.1.73”; .probe = { .url = “/server_status.php”; .interval = 5s; .timeout = 1s; .window = 5;.threshold = 3; }}
backend web2 { .host = “192.168.1.74”; .probe = { .url = “/server_status.php”; .interval = 5s; .timeout = 1s; .window = 5;.threshold = 3; }}

# Define the director that determines how to distribute incoming requests.
director web_director round-robin {
{ .backend = web1; }
{ .backend = web2; }
}

# Respond to incoming requests
sub vcl_recv {
# Set the director to cycle between web servers.
set req.backend = web_director;

if (req.url ~ “^/server_status.php$”) {
return (pass);
}

# Pipe these paths directly to Apache for streaming.
if (req.url ~ “^/backup”) {
return (pipe);
}

# Always cache the following file types for all users.
if (req.url ~ “(?i).(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(?[a-z0-9]+)?$”) {
unset req.http.Cookie;
}
}

sub vcl_hash {
}

# Code determining what to do when serving items from the Apache servers.
sub vcl_fetch {
# Don’t allow static files to set cookies.
if (req.url ~ “(?i).(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(?[a-z0-9]+)?$”) {
# beresp == Back-end response from the web server.
unset beresp.http.set-cookie;
}

# Allow items to be stale if needed.
set beresp.grace = 6h;
}

# In the event of an error, show friendlier messages.
sub vcl_error {
# Redirect to some other URL in the case of a homepage failure.
if (req.url ~ “^/?$”) {
set obj.status = 302;
set obj.http.Location = “http://www.freecomputermaintenance.com/wp-content/uploads/2011/09/Closed_for_Maintenance_JPG.jpg”;
}
}

ref: http://blog.secaserver.com/2012/05/linux-install-configure-varnish-cache-server/

Log & Lockfile bash script

####### Bash Loging Function
function logOutput()
{
logMessage=$1
logTime=`date`
logFile=/var/log/program.log
echo “$logMessage Completed $logTime” >> $logFile
}

# Bash Lockfile
LOCKFILE=”/var/script/program.lock”

if [ -e “${LOCKFILE}” ]; then
echo “Already running.”
exit 99

else

echo $! > “${LOCKFILE}”
chmod 644 “${LOCKFILE}”

###################### Do Whatever you want ##########################
ls -al /home # lol
logOutput ‘oh whatever completed.’

### Ok i dont need lock now.
### Release Lock
rm -f “${LOCKFILE}”

fi

Powered by WordPress & Theme by Anders Norén