Skip to main content

Set Up Nginx Reverse Proxy for on a Ubuntu 16.04 VPS

Nginx reverse proxy Google search

This tutorial covers the steps of configuring Nginx reverse proxy for the Google search engine.

In some countries and areas in the world like China, Google search engine is blocked. In this information age, search engines are the online portals to knowledge and Google is the #1 search engine that provide quality search results. We believe that freely accessing knowledge is a basic and important human right. knowledge is the foundation for human potential, freedom and opportunity and everyone should have access to knowledge for free, without any restriction or limitation.

Nginx is a popular web server as well as a reverse proxy. A reverse proxy is a proxy on behalf of another server. If you have a VPS or cloud server in another country that can freely access, then you can set up Nginx reverse proxy for on your VPS or cloud server. When you type your own domain name in the browser address bar, your reverse proxy will fetch and Google search results for you. VPS and cloud server are very affordable these days.

To set up the reverse proxy, we will need a Nginx module called ngx_http_google_filter_module which is created to help with proxying  This module is dependent on another module called ngx_http_substitutions_filter_module.  They are both open source third-party modules, we will have to manually compile Nginx with these two modules. Once done, all left to do is add a few simple directives in Nginx server block configuration file and reload.

This tutorial will show you how to set it up on a Ubuntu 16.04 VPS. So let’s get started.

1. Back up Nginx Configuration Files

Once Nginx is compiled and installed, your original Nginx config files will be overwritten. You can use the following command to back up the main config file and server block files to your home directory. The tilde represents your home directory.

cp /etc/nginx/nginx.conf /etc/nginx/sites-available/*.conf /etc/nginx/conf.d/*.conf ~

2. Add Official Nginx Repository

The tutorial will demonstrate compiling the latest Nginx mainline version, 1.11.2 at the time of this writing. Ubuntu 16.04 repository has Nginx 1.10. We add the official Nginx repository as shown below.

First fetch the Nginx GPG key and import it to Ubuntu 16.04 system.


sudo apt-key add nginx_signing.key

Then edit the sources.list file.

sudo nano /etc/apt/sources.list

Add the following two lines at the end of this file. The deb-src line allows us to download Nginx source packages with apt source command.

deb xenial nginx
deb-src xenial nginx

Press Ctrl+O to save the file. Press Ctrl+X to close the file. Then update local package index.

sudo apt update

Now the Nginx official repository is added to Ubuntu 16.04.

3 Download Nginx Source Package

We will make a nginx directory under the home directory to store the Nginx sources and then cd into that directory.

mkdir ~/nginx && cd ~/nginx

Download Nginx source package with the below command:

sudo apt source nginx

Check out the downloaded files.

ls ~/nginx/


nginx-1.11.2                               nginx_1.11.2-1~xenial.dsc
nginx_1.11.2-1~xenial.debian.tar.xz        nginx_1.11.2.orig.tar.gz

4. Download the Two Third-Party Modules

Install git tool.

sudo apt install git

Clone the ngx_http_google_filter_module and ngx_http_substitutions_filter_module repository from Github.

git clone
git clone

5. Edit Nginx Compilation Rules

Edit Nginx compilation rule file.

sudo nano ~/nginx/nginx-1.11.2/debian/rules

At the end of COMMON_CONFIGURE_ARGS section, add the following two lines. Replace username with your actual username. These two lines specifies the location of ngx_http_google_filter_module and ngx_http_substitutions_filter_module module.

--add-module=/home/username/nginx/ngx_http_substitutions_filter_module \

This tutorial downloads these two modules to the /home/username/nginx directory. If you use root account, then replace /home/username with /root. because the home directory for the root user is /root.

Notice that you need to append a backslash at the --with-ld-opt line like the screenshot below. If you don’t, the –add-module lines you added will be ignored when Nginx is being compiled.

nginx reverse proxy for

Now save and close the file.

6. Start the Compilation !

Make sure you are in the Nginx source directory.

cd ~/nginx/nginx-1.11.2/

Install all the needed dependencies to build our Nginx deb package.

sudo apt build-dep nginx

Now use the following command to build the deb package.

sudo dpkg-buildpackage -b

Take a cup of coffee and wait a few minutes. On a 512M Digital Ocean Droplet this build process took around 3 minutes. When it’s done, there will be 7 deb files in ~/nginx/ directory. We only need to install the nginx_1.11.2-1~xenial_amd64.deb or nginx_1.11.2-1~xenial_i386.debpackage, depending on your OS architecture. The others are Nginx dynamic module package and a debug package. You can install them as well if you like.

If you have installed Nginx before, it’s time to remove the old version and then install the new version.

sudo apt remove nginx nginx-common nginx-full

cd ~/nginx

sudo dpkg -i nginx_1.11.2-1~xenial_amd64.deb


sudo dpkg -i nginx_1.11.2-1~xenial_i386.deb

Now let’s start Nginx.

sudo systemctl start nginx

If you see the following error message.

Failed to start nginx.service: Unit nginx.service is masked.

Then unmask nginx and issue the start command again.

sudo systemctl unmask nginx

Note that the Nginx process might run as user nginx or www-data. This can be changed by editing the first line in /etc/nginx/nginx.conf file. Just make sure Nginx run as the same user with PHP-FPM.

Now check the config arguments of Nginx.

sudo nginx -V

If you see the following texts at the end, then the two modules are successfully added to Nginx.

--add-module=/home/username/nginx/ngx_http_substitutions_filter_module --add-module=/home/username/nginx/ngx_http_google_filter_module

7. Configure the Nginx Reverse Proxy

In this step, we will first obtain and install a free TLS/SSL certificate from Let’s Encrypt to protect what visitors are searching through our reverse proxy. Then we configure the reverse proxy.

This is extremely important! An unencrypted Google mirror can be blocked by the Great firewall of China!

Create a new server block config file under /etc/nginx/conf.d. Replace with your preferred domain name.

sudo nano /etc/nginx/conf.d/

Paste the following texts into the file.

server {
   listen 80;

   root /usr/share/nginx/html;

   location ~ /.well-known/acme-challenge {
      allow all;

Save and close the file. Now in your DNS settings, point your domain name to the IP address of your Ubuntu 16.04 VPS. After that, install the certbot client. It’s still called letsencrypt in Ubuntu 16.04 repositoy.

sudo apt install letsencrypt

Then we can obtain a free TLS/SSL certificate with this one-liner. Replace email address and domain name.

sudo letsencrypt certonly --webroot --agree-tos --email <your-email-address> -d <> -w /usr/share/nginx/html

Now let’s configure Nginx to use TLS encryption and act as a reverse proxy for

sudo nano /etc/nginx/conf.d/

Change the contents in this file to the following. Replace the red-colored text with your own domain name.

proxy_temp_file_write_size 128k;
proxy_temp_path /var/cache/nginx/temp;
proxy_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=cache_one:100m inactive=7d max_size=10g;

server {
  listen 80 default_server;
  listen [::]:80 default_server;

  # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
  return 301 https://$host$request_uri;

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;

  # certs sent to the client in SERVER HELLO are concatenated in ssl_certificate
  ssl_certificate /etc/letsencrypt/live/;
  ssl_certificate_key /etc/letsencrypt/live/;
  ssl_session_timeout 1d;
  ssl_session_cache shared:SSL:50m;
  ssl_session_tickets off;

  # Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
  #ssl_dhparam /path/to/dhparam.pem;

  # modern configuration. tweak to your needs.
  ssl_protocols TLSv1.2;
  ssl_prefer_server_ciphers on;

  # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
  add_header Strict-Transport-Security max-age=15768000;

  # OCSP Stapling ---
  # fetch OCSP records from URL in ssl_certificate and cache them
  ssl_stapling on;
  ssl_stapling_verify on;

  ## verify chain of trust of OCSP response using Root CA and Intermediate certs
  #ssl_trusted_certificate /path/to/root_CA_cert_plus_intermediates;


  location / {
     google on;
     google_scholar on;

  location ~ /.well-known/acme-challenge {
     allow all;

As you can see, to enable reverse proxy for Google, we used google on and google_scholar on directive in the location section. It’s as simple as that. At the beginning of this file, we also enabled caching for Google search results. Save and close the file.

Then create two directories to store Nginx temporary files and Google search cache.

sudo mkdir -p /var/cache/nginx/temp

sudo mkdir -p /var/cache/nginx/cache

Test Nginx configuration.

sudo nginx -t

If the test is successful, reload Nginx.

sudo systemctl reload nginx

Now in your browser address bar, type your domain name. You should see Google search! Try to search a few words. Here’s my reverse proxy for Google: (This demo reverse proxy is no longer publicly available.)

Nginx reverse proxy for Google search


Google can detect you are using some program to do the search if Nginx is alway contacting the same Google server. If detected, you will be asked to enter verification code to verify you are a human. To prevent being detected, we can add upstream directive in Nginx server block config file.

upstream {

server {
   listen 80;

These are some of Google’s IP addresses.  Save your file and reload Nginx. From here on out, Nginx will fetch Google search results from these IP addresses in a round-robin fashion.

To display Google in a different language, use google_language directive.

location / {
    google on;
    google_scholar on;
    google_language "en";

This will use English. Supported languages are listed below.

ar    -> Arabic
bg    -> Bulgarian
ca    -> Catalan
zh-CN -> Chinese (Simplified)
zh-TW -> Chinese (Traditional)
hr    -> Croatian
cs    -> Czech
da    -> Danish
nl    -> Dutch
en    -> English
tl    -> Filipino
fi    -> Finnish
fr    -> French
de    -> German
el    -> Greek
iw    -> Hebrew
hi    -> Hindi
hu    -> Hungarian
id    -> Indonesian
it    -> Italian
ja    -> Japanese
ko    -> Korean
lv    -> Latvian
lt    -> Lithuanian
no    -> Norwegian
fa    -> Persian
pl    -> Polish
pt-BR -> Portuguese (Brazil)
pt-PT -> Portuguese (Portugal)
ro    -> Romanian
ru    -> Russian
sr    -> Serbian
sk    -> Slovak
sl    -> Slovenian
es    -> Spanish
sv    -> Swedish
th    -> Thai
tr    -> Turkish
uk    -> Ukrainian
vi    -> Vietnamese

Comments, questions and suggestions are always welcome. If you found this post useful, please subscribe to our free newsletter or follow us on Google+, Twitter or like our Facebook page.

Rate this tutorial
[Total: 0 Average: 0]