GuidesTech

AdBlocking with NGINX: Serving 1 pixel GIF and 204 No Content

There are two parts to a network-based ad block solution – resolving known advertisement host names to another server and have the server present a 1 pixel transparent GIF (for images) or 204 No Content HTTP response. This will effectively hide advertisements. This article addresses the latter part of having a server present 1 pixel transparent GIF and 204 No Content HTTP response for other files types.

 

Why use NGINX when there are pixelserv and nullserv?

If you recently upgraded to ASUS RT-AC68U with TomatoUSB (Shibby) and had been using the AdBlock script, you will (probably) realise that previous Pixelserv does not execute on ASUS RT-AC68U as it was not compiled for  ARM architecture.

Instead of cross-compiling Pixelserv for ASUS RT-AC68U, I decided to leverage NGINX web server in TomatoUSB (Shibby). This way, the ad block solution will not be  limited to ASUS RT-AC68U, you can also set up NGINX on Raspberry Pi or any other devices.

 

Serving 1 pixel transparent GIF and 204 No Content

Below is the config file I have created for ASUS RT-AC68U on TomatoUSB (Shibby). You can replicate the relevant lines to your NGINX config. The important lines that actually does the work are highlighted.

 

For those using TomatoUSB (Shibby)’s NGINX Web Server

If you are using TomatoUSB (Shibby)’s NGINX Web Server, simply add the following into the Custom configuration under the Web Server Settings. What it does is defining another HTTP instance that listens on port 80.

It should look like the following.

TomatoUSB NGINX Web Server
TomatoUSB NGINX Web Server

 

How does it work?

This section explains the above-mentioned highlighted lines and what they actually do.

Set the expiration time longer

This basically instructs the browser to cache the response so that it does not request it again unless the browser cache is purged.

Redirecting image request and serving 1 pixel transparent GIF

This regular expression (regex) attempts to match all request that ends with .png, .gif, .jpg and .jpeg and serves a empty_gif (1 pixel transparent GIF) instead.

Redirecting other requests and serves 204 No Content

This regular expression matches all requests that slipped through the previous one (essentially a catch all rule) and returns 204 No Content to the browser. To the browser, this codes means “Okay, I saw your request but I got nothing for you”.

 

Conclusion

And that is pretty much about it! I will be updating this set of codes when I got new ideas. Do check back for more.

 

 

Treat shadowandy!

If these step-by-step guides have been very helpful to you and saved you a lot of time, please consider treating shadowandy to a cup of Starbucks.  

This site contains links to Amazon. These links will take you to some of the products mentioned in today’s article. As an Amazon Associate, I earn from qualifying purchases.
28 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments