NGINX as a Reverse Proxy: Benefits and Best Practices

AM
5 min readApr 27, 2023

--

NGINX is a versatile and powerful web server that offers a multitude of functionalities. It can be used for various purposes, such as

  • Serving static content,
  • Reverse proxying,
  • Load balancing,
  • Caching.
(Image courtesy from NGINX)

With its efficient handling of high traffic and ability to handle concurrent connections, NGINX is ideal for supporting web applications, APIs, and microservices.

Its modular design allows users to customize it according to their specific needs, making it a popular choice for web developers and system administrators.

Some common uses of NGINX include load balancing to maximize server availability and speed, caching frequently requested content, and acting as an additional layer of security for the hosting/web server.

NGINX is the most popular and effective reverse proxy engine.

What is a Reverse Proxy in general?

A reverse proxy is actually a server that lives between a client and a web server, forwards client requests to the web server, and returns the web server’s response to the client.

This provides various benefits:

  • Performance Improvement: By caching the frequently requested resources, a reverse proxy server can reduce the load on the web server and improve the response time for its clients.
  • Security: A reverse proxy server can also act as a barrier between the clients and the web server and filter malicious traffic. It also protects the web server from attacks.
  • Load balancing: A reverse proxy can distribute the requests across multiple web servers, improving the scalability and availability of the web server and eventually your web application.

NGINX as a Reverse Proxy

NGINX is a powerful reverse proxy tool that offers many features and benefits. Some of the reasons to use NGINX as your reverse proxy are:

  • High performance: NGINX can handle a large number of simultaneous connections and can process requests quickly, making it a good choice for high-traffic websites and applications.
  • Flexibility: NGINX can be used as a reverse proxy for other web servers, like Apache, IIS, and Tomcat.
  • Caching: NGINX provides advanced caching features that can help to reduce the load on your web server as well as improve the response time for the clients.
  • SSL termination: NGINX can terminate SSL connections, allowing you to offload SSL processing from your web server and improve performance.
  • Load balancing: NGINX provides various load-balancing algorithms and can distribute client requests across multiple web servers, improving the availability and scalability of your web application.

Best Practices for NGINX Reverse Proxy

Some of the NGINX best practices are:

  1. Use NGINX as a front-end server: NGINX serves as a front-end server that accepts and redirects client requests to the back-end web server. By utilizing NGINX’s caching, load-balancing, and SSL termination features, users can still utilize their preferred web server for handling requests.
  2. Configure NGINX for optimal performance: To optimize the performance of NGINX, certain settings like worker processes, worker connections, and keepalive connections must be configured. The configuration should be tested and performance monitored to ensure that NGINX is functioning optimally. It is important to constantly assess the effectiveness of the setup and make any necessary adjustments.
  3. Use caching judiciously: Caching is beneficial to enhance performance, but it may cause issues if not used correctly. To optimize your web application, configure caching settings such as cache expiration and cache size according to your app’s needs. Setting appropriate cache expiration routines will ensure that outdated data does not persist for too long. Also, appropriate cache size will prevent the storage from crashing, enabling the system to store relevant data. Therefore, always be conscious of the cache settings and choose the right configurations for your app.
  4. Enable SSL: NGINX has a built-in SSL module that makes it easy to enable SSL encryption for securing web applications. It is essential to configure SSL settings such as SSL certificate and cipher suite based on your security requirements. With NGINX, you can easily enable SSL and customize the SSL settings accordingly to ensure the highest level of security for your web application. Ensuring your SSL encryption is set properly can help protect your application from potential attacks and keep your data secure.
  5. Use load balancing wisely: Load balancing is essential for enhancing your web application’s viability and scalability, but it can also add intricacy and overhead. However, choosing the right algorithm based on your application’s requirements and testing the setup is vital to ensure seamless operation.

Setting up NGINX in Front of the Apache Tomcat Server

Download NGINX

Nginx expanded

Go to the path of Nginx and use the command “start nginx” to start the Nginx server and verify it by accessing it with the “localhost” on the browser.

Cool, we have an Nginx server running on our Windows, Easy isn’t it?

So, the next step is to configure Nginx as a reverse proxy from one of the applications hosted on some other server like Tomcat, IIS, etc. To achieve this we need to go to the conf directory of the Nginx server and edit ‘nginx.conf’.

Under HTTP/server, we need to add the configuration below, as per this configuration request of localhost will be redirected to the URL configured under proxy_pass, other proxy parameters take care of the header settings.

 location / {
proxy_pass http://localhost:5000;
#root html;
#index index.html index.htm;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}

And now, the restart of the nginx will take these changes and redirect the localhost to the URL “http://localhost:5000” internally and will work as a reverse proxy. If you want to redirect to some additional path on this URL then you can also change the URL to “http://localhost:5000/sompath/”. Please notice the slash at the end.

To restart the nginx server you can use the following commands: (https://gist.github.com/Codes-Lab/bf60ca6185756cbed2528718819f5d15)

cd /nginx
.\nginx.exe -s quit #Stops the nginx
start nginx #Start the nginx

Or task kill can also be used to kill the nginx process.

taskkill /f /IM nginx.exe   #Kills the nginx
start nginx #Starts the nginx s"></scrit>

--

--