Set Up Nginx Reverse Proxy for on a Ubuntu 16.04 VPS

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 create a source list file for Nginx.

sudo nano /etc/apt/sources.list.d/nginx.list

Add the following two lines into the 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 install dpkg-dev

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

Find config.status.nginx: config.env.nginx section.  Add the following two lines in CFLAGS. 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 after --with-ld-opt="$(LDFLAGS)" 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 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. Test Nginx configurations and reload.

sudo nginx -t

sudo systemctl reload nginx

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 from the official PPA. python-certbot-nginx is the Certbot Nginx plugin.

sudo apt install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt update
sudo apt install certbot python-certbot-nginx

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

sudo certbot --authenticator webroot --installer nginx -w /usr/share/nginx/html/ --agree-tos --redirect --staple-ocsp -d --email your-email-address

Now if you enter in your web browser, you will be redirected to HTTPS. If you use Amazon Web Services (AWS) or Google Cloud Platform (GCP), make sure HTTPS traffic is allowed in your firewall settings.


Let’s configure Nginx to act as a reverse proxy for

sudo nano /etc/nginx/conf.d/

To enable reverse proxy for Google, simply copy and paste the following directives into the server section.


location / {
   google on;
   google_scholar on;

Save and close the file. 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;

server {
   listen 443 ssl http2;

These are some of Google’s IP addresses. You can always find working IP addresses by looking up the A records of Google’s country-specific domains like this:

nginx reverse proxy google

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

To prevent Nginx from being replaced by a newer version when running sudo apt upgrade command, you can hold it from upgrading.

sudo apt-mark hold nginx

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: 4 Average: 4.3]

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community ( for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here