Category Archives: Snippets

Varnish config for wordpress with ngx_pagespeed and wp-touch

This is the Varnish config I am using currently. It is working with wp-touch, pagespeed and wordpress and (bonus) deals with the pagespeed not allowing pages to cache. No time for pretty comments and explanations, here’s the code. I will answer questions, or come back and explain the code in comments – but it is pretty self explanatory.

backend default {
.host = "127.0.0.1";
.port = "80";
.first_byte_timeout = 300s;
}

sub generate_user_agent_based_key {
set req.http.default_ps_capability_list_for_large_screens = "LargeScreen.SkipUADependentOptimizations:";
set req.http.default_ps_capability_list_for_small_screens = "TinyScreen.SkipUADependentOptimizations:";

set req.http.PS-CapabilityList = req.http.default_ps_capability_list_for_large_screens;

# Lazyload
if (req.http.User-Agent ~ “(?i)Chrome/|Firefox/|MSIE |Safari”) {
set req.http.PS-CapabilityList = “ll,ii,dj:”;
}
# lazyload_images (ll), inline_images (ii), defer_javascript (dj), webp (jw) and lossless_webp (ws).
if (req.http.User-Agent ~
“(?i)Chrome/[2][3-9]+\.|Chrome/[[3-9][0-9]+\.|Chrome/[0-9]{3,}\.”) {
set req.http.PS-CapabilityList = “ll,ii,dj,jw,ws:”;
}
# odd ones
if (req.http.User-Agent ~ “(?i)Firefox/[1-2]\.|MSIE [5-8]\.|bot|Yahoo!|Ruby|RPT-HTTPClient|(Google \(\+https\:\/\/developers\.google\.com\/\+\/web\/snippet\/\))|Android|iPad|TouchPad|Silk-Accelerated|Kindle Fire”) {
set req.http.PS-CapabilityList = req.http.default_ps_capability_list_for_large_screens;
}
# mobile
if (req.http.User-Agent ~ “(?i)Mozilla.*Android.*Mobile*|iPhone|BlackBerry|Opera Mobi|Opera Mini|SymbianOS|UP.Browser|J-PHONE|Profile/MIDP|portalmmm|DoCoMo|Obigo|Galaxy Nexus|GT-I9300|GT-N7100|HTC One|Nexus [4|7|S]|Xoom|XT907”) {
set req.http.PS-CapabilityList = req.http.default_ps_capability_list_for_small_screens;
}
# Remove placeholder header values.
remove req.http.default_ps_capability_list_for_large_screens;
remove req.http.default_ps_capability_list_for_large_screens;
}

sub vcl_hash {
# Block 3: Use the PS-CapabilityList value for computing the hash.
hash_data(req.http.PS-CapabilityList);
}
# Block 3a: Define ACL for purge requests
acl purge {
# Purge requests are only allowed from localhost.
“localhost”;
“127.0.0.1”;
#Add your server IP to this list
}
# Block 3b: Issue purge when there is a cache hit for the purge request.
sub vcl_hit {
if (req.request == “PURGE”) {
purge;
error 200 “Purged.”;
}
}

# Block 3c: Issue a no-op purge when there is a cache miss for the purge
# request.
sub vcl_miss {
if (req.request == “PURGE”) {
purge;
error 200 “Purged.”;
}
}

sub vcl_recv {
call generate_user_agent_based_key;

set req.http.X-Forwarded-For = client.ip;
set req.http.Host = regsub(req.http.Host, “:[0-9]+”, “”);

# Block 3d: Verify the ACL for an incoming purge request and handle it.
if (req.request == “PURGE”) {
if (!client.ip ~ purge) {
error 405 “Not allowed.”;
}
return (lookup);
}
# Blocks which decide whether cache should be bypassed or not go here.

# Did not cache the admin and login pages
if (req.url ~ “/wp-(login|admin)”) {
return (pass);
}
// server1 must handle file uploads
if (req.url ~ “media-upload.php” || req.url ~ “file.php” || req.url ~ “async-upload.php”) {
return(pass);
}

// do not cache xmlrpc.php
if (req.url ~ “xmlrpc.php”) {
return(pass);
}

// strip cookies from xmlrpc
if (req.request == “GET” && req.url ~ “xmlrpc.php”){
remove req.http.cookie;return(pass);
}

# Remove the “has_js” cookie
set req.http.Cookie = regsuball(req.http.Cookie, “has_js=[^;]+(; )?”, “”);

# Remove any Google Analytics based cookies
set req.http.Cookie = regsuball(req.http.Cookie, “__utm.=[^;]+(; )?”, “”);

# Remove the Quant Capital cookies (added by some plugin, all __qca)
set req.http.Cookie = regsuball(req.http.Cookie, “__qc.=[^;]+(; )?”, “”);

# Remove the wp-settings-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, “wp-settings-1=[^;]+(; )?”, “”);

# Remove the wp-settings-time-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, “wp-settings-time-1=[^;]+(; )?”, “”);

# Remove the wp test cookie
set req.http.Cookie = regsuball(req.http.Cookie, “wordpress_test_cookie=[^;]+(; )?”, “”);

# Are there cookies left with only spaces or that are empty?
if (req.http.cookie ~ “^ *$”) {
unset req.http.cookie;
}

if (req.http.Accept-Encoding) {
# Do no compress compressed files…
if (req.url ~ “\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$”) {
remove req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ “gzip”) {
set req.http.Accept-Encoding = “gzip”;
} elsif (req.http.Accept-Encoding ~ “deflate”) {
set req.http.Accept-Encoding = “deflate”;
} else {
remove req.http.Accept-Encoding;
}
}

# Cache the following files extensions
if (req.url ~ “\.(css|js|png|gif|jp(e)?g)”) {
unset req.http.cookie;
}

# Check the cookies for wordpress-specific items
if (req.http.Cookie ~ “wordpress_” || req.http.Cookie ~ “comment_”) {
return (pass);
}
if (!req.http.cookie) {
unset req.http.cookie;
}

# — End of WordPress specific configuration

# Did not cache HTTP authentication and HTTP Cookie
if (req.http.Authorization || req.http.Cookie) {
# Not cacheable by default
return (pass);
}

# Cache all others requests
return (lookup);

}

# Block 5b: Only cache responses to clients that support gzip. Most clients
# do, and the cache holds much more if it stores gzipped responses.
if (req.http.Accept-Encoding !~ “gzip”) {
return (pass);
}

# Block 6: Mark HTML uncacheable by caches beyond our control.
sub vcl_fetch {
# For static content related to the theme, strip all backend cookies
if (req.url ~ “\.(css|js|png|gif|jp(e?)g)”) {
unset beresp.http.cookie;
}

# A TTL of 30 minutes
set beresp.ttl = 1800s;

return (deliver);
}
# Block 7: Add a header for identifying cache hits/misses.
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = “HIT”;
} else {
set resp.http.X-Cache = “MISS”;
}
}

Free space in your WordPress install by deleting old image sizes

If you change your theme often, your uploads folder will accumulate thumbnails of images in many sizes that you no longer use. This consumes disk space unnecessarily. I wish someone coded a plugin for this, but failing that, a handy way to do this via SSH is:

find . -name *-250x250.* | xargs rm -f

Where 250×250 is the image size you want to delete. You could also try something like:

find . -name *-250x*.* | xargs rm -f

if you have multiple thumbnail sizes like 250×250 250×300 etc.

What I do is list images in the folders to see the unwanted sizes there, and run this delete a few times with various sizes. A more ruthless person could try something like:

find . -name *-*x*.* | xargs rm -f

I do not recommend this, as it can match several files that you may not want to delete, for example a file with a hyphenated name and the letter x in the last hyphenated word, like “wish-merry-xmas.jpg” for example, which wouldn’t be a resized image, but an original or worse, it could be another file and not an image at all, like “here-are-exact-directions-to-our-property.html”.

But if you have a lot of thumbnail sizes, you may feel tempted anyway. Two suggested precautions. Change directory to your uploads folder (you should do this in any case)
cd /path/to/wprdpress/wp-content/uploads
find . -name *-*x*.* | xargs rm -f

The other precaution to take is to specify extension.
find . -name *-*x*.jpg | xargs rm -f
find . -name *-*x*.png | xargs rm -f

This will give you some protection from inadvertently deleting non-resize uploads like “entertaining-extras.pdf”

of course, if you are a patient soul (or don’t have too many files uploaded), you could find the files before deleting to see if any other files are getting selected along with resizes.

find . -name *-*x*.*
and if all is well
find . -name *-*x*.* | xargs rm -f

Do you have a better method?

Enhanced by Zemanta

How to retrieve your passwords saved in Google Chrome or Chromium on ubuntu linux

If you are like me, you have loads of passwords saved in the Chrome browser. And then you need them. Maybe you want to stop using Chrome, or you want them to enter into some applcation other than the browser… or perhaps you just want a list of all sites you have passwords for – curiosity… whatever the reason, it isn’t easy to discover how to do this.

This solution is based on one I found at The Big Browser with two very minor changes where that code didn’t work for me.

Before beginning, make sure you are signed in from your Google Chrome or Chromium browser and your passwords are synced and available.

Close the browser.

Next, open the browser from the command line, with a temporary profile that stores passwords without encryption. Like so (pick one of the lines depending on the browser you are using)

### Google Chrome
    google-chrome --user-data-dir=/tmp/chrome-tmp --password-store=basic

### Chromium
    chromium --user-data-dir=/tmp/chrome-tmp --password-store=basic

This did not work for me – I use Chromium. I had to use this:

### Chromium
    chromium-browser --user-data-dir=/tmp/chrome-tmp --password-store=basic

I have no idea what happens with Google Chrome, but if the given line doesn’t work, appending -browser to google-chrome is worth a try.

This will open a separate session. Sign in to the browser with your account and wait for a while till your passwords are synchronized. You can surf if you want. When your data is syncronized, your saved websites and bookmarks will be available. You can go to “Manage passwords” in settings and see that your passwords are loaded. If you don’t see them, wait some more time till you do.

Close the browser.

Note: the fastest way to get to the stored passwords page in settings is to type password in the Search box in the top right.

In the terminal, enter:

cd /tmp/chrome-tmp/Default

For the next step, you will need sqlite3 installed. If it isn’t installed, you can install it by entering:

sudo apt-get install sqlite3

Then enter:

sqlite3 'Login Data'

At the changed prompt, now enter the following lines one by one.

.mode csv               # other options are `html', `tabs', etc.
.headers on
.separator ","
.output chrome_passwords.csv
select * from logins;
.exit

This will give you a file called chrome_passwords.csv with your Chrome passwords.

To open it with LibreOffice, type:

libreoffice --calc chrome_passwords.csv

The passwords will open as a spreadsheet. You also will be able to import this spreadsheet into other keyword managers if you need to.

Update/install fail: Server cannot access software repositories

If you are trying to install something and the server waits forever after you answer yes to the update and it accesses the software repository… something like…

apt-get update
...
details of packages
...
Need to get 83.7 MB of archives.
After this operation, 237 kB disk space will be freed.
Do you want to continue [Y/n]?
0% [Connecting to archive.ubuntu.com]

And then nothing. It fails.

Do you have UFW installed? UFW can block outgoing connections from your server, so it becomes unable to access the software repositories. Rather than make exceptions, it is easier to disable the firewall and enable again when update or install is done.
??
If you have UFW (Uncomplicated Firewall) installed, disable it and try again.

ufw disable

Now your upgrades should work. Once you are done, re-enable your firewall.

ufw enable
Enhanced by Zemanta

bash: add-apt-repository: command not found

If you are trying to add a ppa repository and get the following error

bash: add-apt-repository: command not found

You need to install python-software-properties, like so

apt-get install python-software-properties

If you have python-software-properties installed and are still getting this error, there is some problem with the package. Uninstall and reinstall it.

apt-get remove python-software-properties --purge && apt-get install python-software-properties

or

apt-get install --reinstall python-software-properties && sudo dpkg-reconfigure python-software-properties
Enhanced by Zemanta

Need to install jpegtran?

If you are optimizing your blog/site, you may end up needing a package for optimizing images called jpegtran It is a part of another package called 

libjpeg-progs

To install, simply install the package, like so

apt-get install libjpeg-progs
Done.

Need to install jpegtran?

If you are optimizing your blog/site, you may end up needing a package for optimizing images called jpegtran It is a part of another package called 

libjpeg-progs

To install, simply install the package, like so

apt-get install libjpeg-progs
Done.
Screenshot-4

Styling links, but not image links

[slideshow]
Its been an irritating thing with css where you style your text links beyond the typical text-decoration:underline and your images seem to be married to it. It is quite impossible to target the look of text links in general irrespective of class without applying effects meant for text to your images, as you see under the tilted image above.

What looks good for your text links need not necessarily do your images any favours.

I am using a solution that should work for many modern browsers (not till IE8), so this is a catch.

Instead of a:hover{} I use a:not(img) a:hover{text-shadow:whatever; transform:something;etc:etc;} of course, this is going to create problems with a vast percentage of visitors still using < IE9, which don't support :not()

So, my workaround is to use CSS3 effects that IE is unable to render to style text links. If the effects are rendered, they are also not rendered on image links. Otherwise, I style them in a back up manner, which may not be my heart’s desire, but will also not wreck the look of my images. One good way is to change the link color and use text-decoration.