How to Configure Nginx Reverse Proxy (Ubuntu 16)

Nginx is becoming a highly popular option for users looking for a powerful resource-friendly web server or a reverse proxy to their existing Apache server configuration.

In this article, we’ll show you how to install Nginx in your Ubuntu 16 Cloud VPS Hosting account and set it up as a reverse proxy for Apache.

Apache is Already Installed

You may have noticed when first setting up your account that Apache is already installed and working as your active web server. (You can verify this by visiting your primary domain and seeing the Ubuntu server default page.)

This means you’ll likely find most success using Nginx as a reverse proxy, which we will show you how to do in this article.

Install Nginx

The first step in this process is to install Nginx. Make sure you are logged into your VPS with the sudo user you created. You will just need to run this command and respond “Yes” to any installation prompts that may pop up:

sudo apt-get -y install nginx

Once the installation is complete, we will begin configuring Apache and Nginx.

Configure Apache

We will first need to change the default ports for accessing Apache. By changing the default port for Apache, we will be making sure that Nginx has the first opportunity to serve any static resources needed.

You can use the nano text editor to edit the ports file:

sudo nano /etc/apache2/ports.conf

Update the “Listen” line, changing the port number from 80 to 8000:

- Listen 80
+ Listen 8000

We will also need to edit the configuration file for virtual hosts:

sudo nano /etc/apache2/sites-available/000-default.conf

Changing the port again:

- <VirtualHost>
+ <VirtualHost>

Make sure to restart the Apache service:

sudo service apache2 restart

Those are the essential configurations for Apache. There are more advanced configurations you can apply, but these port changes cover the basics.

Configure Nginx

Now we will need to configure Nginx to serve as our reverse proxy.

Make sure that Nginx services are enabled:

sudo systemctl enable nginx.service

Restart Nginx to make sure any changes are applied:

sudo service nginx restart

Now we will add some proxy parameters to the necessary configuration file:

sudo nano /etc/nginx/proxy_params

Add the extra parameters to the bottom of the proxy_params file (Bear in mind, the + signs are added to demonstrate the additional lines and should not be included in your file. For a plain text version of the file you can copy and paste, click here.):

proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;  + client_max_body_size 100M; + client_body_buffer_size 1m; + proxy_intercept_errors on; + proxy_buffering on; + proxy_buffer_size 128k; + proxy_buffers 256 16k; + proxy_busy_buffers_size 256k; + proxy_temp_file_write_size 256k; + proxy_max_temp_file_size 0; + proxy_read_timeout 300;

As we did above for Apache, we will next be editing the virtual hosts configuration file, this time for Nginx:

sudo nano /etc/nginx/sites-available/default

Make sure that the document root is the same for Nginx, and make sure to include our proxy parameters file. An easy way to make this change would be copy your existing configuration file first:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default-dist

Now you have an untouched version of the file your distribution came with; this way you can safely delete the contents of the original and paste in these lines:

server {     listen 80 default_server;     listen [::]:80 default_server;      root /var/www/html;      # Add index.php to the list if you are using PHP     index index.html index.htm index.nginx-debian.html;      server_name _;      location / {       proxy_pass https://localhost:8000;       include /etc/nginx/proxy_params;     } }

Make sure to “reload” Nginx in order to apply these new settings.

sudo service nginx reload

How to Test Your Configuration

You’ll notice that if you reload your page in a browser, you will not see any immediate change. So how do we know if our changes took effect?

There are many different ways to do this, but one easy way is to run this command from your local computer in a new terminal window, of course making sure to substitute “” for the primary domain of your site:

curl -s -D - "" | grep "^Server:"

The output of this command will return information about your server. If you see “Server: nginx/1.10.3 (Ubuntu)” then you completed this tutorial correctly and you now have Nginx configured as a reverse proxy for Apache.

Learn more from our Cloud Server Hosting Product Guide.

With our Cloud Server Hosting, you can deploy a lightning-fast, reliable cloud platform with built-in redundancy – ensuring the availability of your environment!
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!