Set Up Nginx Reverse Proxy for Google.com on a Ubuntu 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 provides 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 freely, 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 Google.com, then you can set up Nginx reverse proxy for Google.com on your VPS or cloud server. When you type your own domain name in the browser address bar, your reverse proxy will fetch Google.com and Google search results for you. VPS and cloud server are very affordable these days.
To set up the reverse proxy, we will need to install two Nginx modules:
ngx_http_google_filter_module
which is created to help with proxying Google.com. This module is dependent on another modulengx_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 VPS. So let’s get started.
Step 1: Install Nginx Web Server on Ubuntu
sudo apt install nginx
The Nginx binary needs to be compiled with the --with-compat
argument, which will make dynamic modules binary-compatible with your existing Nginx binary. However, not every Nginx binary shipped from the default Debian/Ubuntu repository is compiled with the --with-compat
argument.
Check the configure arguments of Nginx with the following command:
sudo nginx -V
Now we need to enable source code repository, so we will be able to download Nginx source code.
Ubuntu
sudo apt install software-properties-common sudo apt-add-repository -ss sudo apt update
Note: I don’t recommend installing the latest version of Nginx. The nginx
package from the default Ubuntu repository is fine.
Step 2: Download Nginx Source Package
Run the following command to make a nginx
directory under /usr/local/src/
to store the Nginx source code package. Replace username
with your real username.
sudo chown username:username /usr/local/src/ -R mkdir -p /usr/local/src/nginx
cd into the Nginx source directory.
cd /usr/local/src/nginx/
Download Nginx source package with the command below:
sudo apt install dpkg-dev sudo apt source nginx
If you see the following warning message, you can safely ignore it.
W: Download is performed unsandboxed as root as file 'nginx_1.19.5-0ubuntu1.dsc' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)
Check out the downloaded files.
ls
Sample output:
nginx-1.19.5 nginx_1.19.5-1~bionic.debian.tar.xz nginx_1.19.5-1~bionic.dsc nginx_1.19.5.orig.tar.gz
Step 3: 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.
cd /usr/local/src/ git clone https://github.com/cuber/ngx_http_google_filter_module.git git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
Step 4: Compile Module
Make sure you are in the Nginx source directory.
cd /usr/local/src/nginx/nginx-1.19.5
Install build dependencies for Nginx.
sudo apt build-dep nginx sudo apt install uuid-dev libpcre2-dev
Configure the environment with the following command. We will not compile Nginx itself, but compile the modules only. The --with-compat
flag will make the module binary-compatible with your existing Nginx binary.
sudo ./configure --with-compat --with-openssl=/usr/include/openssl --add-dynamic-module=/usr/local/src/ngx_http_substitutions_filter_module/ --add-dynamic-module=/usr/local/src/ngx_http_google_filter_module/
Build the modules, which will be finished in just a few moments.
sudo make modules
If your encounter errors, check if your server has enough RAM. Once they are complied, copy them to the /usr/share/nginx/modules/
directory.
sudo cp objs/ngx_http_subs_filter_module.so objs/ngx_http_google_filter_module.so /usr/share/nginx/modules/
If you installed Nginx from nginx.org repostiory, you can copy it to /etc/nginx/modules/
directory instead.
sudo cp objs/ngx_http_subs_filter_module.so objs/ngx_http_google_filter_module.so /etc/nginx/modules/
Step 5: Load the Modules
Edit the main Nginx configuration file.
sudo nano /etc/nginx/nginx.conf
Add the following two lines at the beginning of the file.
load_module modules/ngx_http_subs_filter_module.so; load_module modules/ngx_http_google_filter_module.so;
Save and close the file. Then test Nginx configuration.
sudo nginx -t
If the test is successful, reload Nginx.
sudo systemctl reload nginx
Step 6: 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 search.your-domain.com
with your preferred domain name.
sudo nano /etc/nginx/conf.d/search.your-domain.com.conf
Paste the following texts into the file.
server {
listen 80;
server_name search.your-domain.com;
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 VPS. After that, install the certbot client. python3-certbot-nginx
is the Certbot Nginx plugin.
sudo apt install certbot python3-certbot-nginx
Open TCP port 80 and 443 in UFW firewall.
sudo ufw allow 80,443/tcp
Then we can obtain and install a free TLS/SSL certificate with this one-liner. Replace email address and domain name.
sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp -d search.your-domain.com --email your-email-address
Now if you enter search.your-domain.com
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 Google.com.
sudo nano /etc/nginx/conf.d/search.your-domain.com.conf
To enable reverse proxy for Google, simply copy and paste the following directives into the SSL server section.
resolver 8.8.8.8; 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: https://search.linuxbabe.com.
Optimization
Google can detect you are using some program to do the search if Nginx is always 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 www.google.com { server 172.217.0.228:443; server 172.217.3.4:443; server 172.217.4.196:443; server 172.217.4.132:443; server 216.58.219.228:443; server 216.58.192.4:443; server 216.58.194.196:443; server 216.58.218.100:443; server 216.58.219.36:443; server 216.58.194.68:443; server 216.58.221.132:443; server 216.58.221.228:443; server 216.58.203.4:443; server 216.58.197.100:443; server 216.58.199.100:443; } 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:
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 Twitter or like our Facebook page.