Example content

How to configure an example Nginx reverse proxy server

What is an Nginx Reverse Proxy?

Most enterprise architectures use a single reverse proxy server to handle all incoming requests.

The proxy server then inspects each HTTP request and identifies which backend system, whether it’s an Apache, Tomcat, Express, or NodeJS server, should handle the request. The reverse proxy then forwards the request to that server, allows the request to be processed, gets a response from that backend server, and then sends the response back to the client. This is the function an Nginx server configured as a reverse proxy would perform.

What are the benefits of an Nginx reverse proxy setup?

Benefits of using Nginx as a reverse proxy include:

  • Customers access all backend resources through a single web address
  • Reverse proxy can serve static content, reducing load on application servers like Express, Tomcat or WebSphere
  • The Nginx reverse proxy can navigate through firewalls that protect backend resources
  • Reverse proxy can act as cache or buffer to reduce latency
  • User access control is greatly simplified with a single point of access to your site

Nginx Reverse Proxy Setup Steps

Follow these steps to install and configure your own Nginx reverse proxy server:

  1. Install Nginx on your Windows or Linux server (prerequisite)
  2. Add the Nginx proxy_pass parameter in a virtual host or the default configuration file
  3. Map a context root to a backend URL
  4. Optionally set headers for the Nginx reverse proxy to use with the backend
  5. Restart the Nginx reverse proxy and test the reverse proxy configuration

Install Nginx

On Ubuntu, the latest version of Nginx can be installed by first running the apt-get update and apt-get upgrade commands, followed by the apt-get install command.

sudo apt-get update -y
Reading package lists... Done
sudo apt-get upgrade -y
Calculating upgrade... Done
sudo apt-get install nginx -y
The following Nginx packages will be installed on Ubuntu 22:
libnginx-mod-http-geoip2 nginx-common nginx-core nginx-proxy
Setting up nginx 1.18 ubuntu 22... Done

How to verify an Nginx installation?

Do your due diligence and check if the Nginx server installation was successful.

A health check should show that Nginx is active.

sudo systemctl status nginx
● nginx - A high performance web, http and reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled;)
   Active: active (Nginx server running)

Add an Nginx proxy_pass parameter

The most important configuration step in an Nginx reverse proxy setup is adding a proxy_pass parameter that maps an incoming URL to a backend server.

In this Nginx reverse proxy example, we will assume that an Apache Tomcat server is running on localhost at port 8080, and an application with the context root of /examples must be proxy by Nginx.

The proxy_pass parameter is what makes the Nginx reverse proxy configuration work.

The proxy_pass is configured in the location section of any virtual host configuration file. To configure an Nginx proxy_pass globally, modify the default file in Nginx sites available case.

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

Nginx proxy_pass example

For this example, we will configure the Nginx reverse proxy location mapping to forward any request that has /examples in the URI to an Apache Tomcat server running on port 8080:

# example nginx reverse proxy mapping
# /etc/nginx/sites-available/default
location /examples {
  proxy_pass http://localhost:8080/examples;
}

Set Nginx proxy header values

By default, Nginx will relay its own IP address to reverse proxy targets.

To have the Nginx relay IP address, host and port information about the client that made the original request, there is proxy_set_header values ​​that can be set.

Additionally, Nginx will buffer data to help handle heavy workloads, but this buffering may be undesirable for applications that require the lowest possible latency. So many Nginx reverse proxy setups are spinning proxy_buffering stopped.

Here is an example of an Nginx reverse proxy that disables buffering while at the same time setting client headers for backend servers handling proxy requests:

# example nginx reverse proxy config file
# /etc/nginx/sites-available/default
location /examples {
  proxy_pass http://localhost:8080/examples;
  proxy_buffering off;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-Host $host;
  proxy_set_header X-Forwarded-Port $server_port;
}

Add these Nginx proxy_pass, proxy_buffering and proxy_set_header changes to your default configuration file and save your changes.

Restart Nginx as reverse proxy

To see Nginx working as a reverse proxy, just restart the server to load the new configuration.

sudo systemctl restart nginx

When the server is online, try to access the main server through the Nginx reverse proxy.

In this example, we can access the Tomcat server running on port 8080 through Nginx.

http://localhost/examples -- proxies for --> http://localhost:8080/examples

The Nginx reverse proxy redirects end up acting as a single entry point for all requests from the sample application to Tomcat. This proves that the configuration of the Nginx reverse proxy configuration was successful.

nginx reverse proxy example

The Nginx reverse proxy example maps the /example URI to an Apache Tomcat server.

And it’s that simple to configure and install an Nginx reverse proxy on Ubuntu.


Source link