What is a Reverse Proxy?
https://phoenixnap.com/kb/nginx-reverse-proxy
Last updated
https://phoenixnap.com/kb/nginx-reverse-proxy
Last updated
A standard proxy server works on behalf of clients, often by providing privacy or filtering content. A reverse proxy works on behalf of a server, intercepting traffic and routing it to a separate server.
There are several reasons you might want to install a reverse proxy. One of the main reasons is privacy.
If you have multiple servers, a reverse proxy can help balance loads between servers and improve performance. As a reverse proxy provides a single point of contact for clients, it can centralize logging and report across multiple servers.
Nginx can improve performance by serving static content quickly and passing dynamic content requests to Apache servers.
This guide will help you install and configure an Nginx reverse proxy on your system.
Prerequisites
A Linux server with Apache, PHP, and a firewall
Access to a root user with sudo access
Linux command-line or terminal (Ctrl–Alt–T for Ubuntu, Alt–F2 for CentOS)
Package manager (such as APT)
Open a terminal window and enter the following:
Allow the package manager to finish refreshing the software lists, then enter the following:
Allow the process to complete.
Note: This is the easiest way to install Nginx on CentOS or Ubuntu may not load the latest stable release. Move on to Step 2 to add and install from the Nginx software repositories.
In a terminal window, enter the following:
This downloads the signing key for Nginx, which verifies that you’re downloading authentic software.
In the terminal, enter the following:
Enter the following lines in the /etc/apt/sources.list
file you just opened:
Replace <CODENAME>
with the codename for your distribution of Debian.
Save the file and exit.
Note: Nginx developers maintain different directories for different Linux distributions. This guide suggests installing the mainline supported release. As with most software, there are more recent but untested packages. See the documentation for the specific package for your distribution.
To install the latest release of Nginx, use the commands:
To start Nginx:
To enable Nginx:
To check Nginx is running:
The output should show you the service is active (running), as in the image below:
In the terminal, enter the following:
To create a new configuration file, enter:
Replace custom_server
with a name that’s meaningful to you. In the new file, enter:
This is a very basic Nginx reverse proxy example. Nginx is set to listen for all traffic on port 80 for all traffic.
The proxy_pass
command directs all traffic on port 80 to http://my_server
. Just change http://my_server
to the location of your choice, and Nginx will intercept client requests and route them to the location you specify. Once you’ve finished, save the file and exit.
To activate the new Nginx file, enter:
As usual, replace custom_server
with the name of the configuration file you created in Step 5.
To test Nginx:
To restart Nginx:
By default, Nginx buffers traffic for servers that it proxies for. Buffers improve server performance as a server response isn’t sent until the client finishes sending a complete response.
To turn the buffer off, open the configuration file from Step 5. Under the location/section, add the following:
Headers provide the server information about the requests made, or about the client.
Nginx redefines two of the header fields: host
is configured for $proxy_host
, and connection
is configured for close
. If you use those headers, be sure to change the behavior in the configuration file.
If any header strings are empty, Nginx simply eliminates those fields.
To change the way Nginx handles heathers, use the following commands in your configuration file:
This example tells Nginx to set host
to the $host
variable.
To prevent a header field from being passed to the proxied server, use an empty string as follows:
You can use the configuration file to route traffic to several servers. To use this configuration, your configuration file will look similar to this example:
In other words, the HTTP configuration goes outside the server configuration from Step 5.
To create a name for a group of servers, Use the upstream
command:
This designation takes two servers – my.server1.com
and my.server2.com
– and bundles them together. Nginx proxies that group of servers under the name http://server_group
. You can rename them anything you’d like.
This example uses the weight
command to route three requests to my.server1.com
, then 1 request to my.server2.com
. This is one option to manually balance client load between servers. Another method is to simply omit any designation, in which Nginx will round-robin the requests evenly among the listed servers.
Conclusion
Now you know how to set up an Nginx reverse proxy.
It’s an excellent tool for a multiple-server environment, creating a unified client experience. It can also be useful for simpler tasks like keeping a single server anonymous.
Additionally, you can also use our Knowledge Base to learn how to deploy NGINX reverse proxy on Docker.