How to setup an Nginx Reverse Proxy

Discover the power of Nginx reverse proxy in optimizing server performance, enhancing security, and delivering a seamless user experience - unlock load balancing, caching, SSL termination, and more to take your online presence to new heights.

Updated: 29 Jun, 23 by Susith Nonis 7 Min

List of content you will read in this article:

Reverse Proxy is a service that sends the user's request to one or more other servers and finally gets the answer from those servers and returns it to you as the user. Since Nginx has high efficiency and scalability, this web server is usually placed in this mode, that is, as a reverse proxy in front of HTTP servers or non-HTTP servers. 

In short, the Nginx web server can manage all files related to your site. You can store this information without worry and with full confidence on Nginx and make the necessary changes to them if needed.

One of the main features of Nginx can be called constant RAM consumption at high pressures. Advanced technologies in processing this server make it possible to use the amount of server RAM in high requests. Web servers can process the request in the available ways. Nginx also uses a special method in which a new thread is created for different requests, and this topic is also kept in the pool so that new requests use idle threads in the pool.

Further, one of the most used configurations that use Nginex as a Reverse Proxy is to place this web server in front of Node.js, Python, Java, Apache + PHP software. In this article, we will set up Nginx as a Reverse Proxy; stay with us.

Nginx web server is an open-source proxy that can support protocols such as HTTP, HTTPS, SMTP, POP3, and IMAP. You can use this web server on Linux and Windows. If you are looking for a very high-speed web server, we recommend Nginx.

For this, you need to use Reverse Proxy. In the way that you determine, for example, all requests that reach the server from port 80 must pass through port 3000 inside the server. To do this, first, enter the Nginx configuration file. By default, this file is located in the following path:

/etc/nginx/sites-available/default.

A server block identifies each of your servers. So, to create a new server, first, add a server block to the code:

server {

        listen 80;

        listen [::]:80;

}

You can see that the server will now respond to the requests it receives from port 80. Both in IP version 4 and in IP version 6. It now specifies how requests sent to the server should be processed:

Location / {

    proxy_pass http://127.0.0.1:3000;

}

What you see, all the requests that are sent to the root of the server are forwarded to port 3000 on the local IP. Now, add the logs for debugging:

access_log /var/log/nginx/reverse-access.log;

error_log /var/log/nginx/reverse-error.log;

You can see the complete code of nginx view below:

server {

        listen 80;

        listen [::]:80;

        access_log /var/log/nginx/reverse-access.log;

        error_log /var/log/nginx/reverse-error.log;

        Location / {

                    proxy_pass http://127.0.0.1:3000;

  }

}

You can check whether the file is written correctly or not with the following execution command:

And finally, restart the nginx service:

sudo systemctl restart nginx.service

After running the node program (or any other server) on port 3000, all requests on port 80 will be processed by this server. Reverse Proxy does this.

You can also add more settings to your settings file:

  • Setting the HTTP version:

proxy_http_version 1.1;

  • Cache bypass settings:

proxy_cache_bypass $http_upgrade;

  • If you use WebSockets:

proxy_set_header Upgrade $http_upgrade

  • Host header setting:

proxy_set_header Host $host;

  • IP list of all proxies:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  • The header IP address of the visitor:

proxy_set_header X-Real-IP $remote_addr;

  • The main host addresses header:

proxy_set_header X-Forwarded-Host $host;

  • Main port address header:

proxy_set_header X-Forwarded-Port $server_port;

It is good at:

  • very high performance,
  • stability and less use of resources;
  • Support and simultaneous management with low RAM;
  • gzip compression and extranet;
  • SPDY protocol support;
  • Authentication using external HTTP servers;
  • Load balancing capability;
  • Fault-tolerant;
  • Optional installing on the virtual server;
  • Compatibility with IPv6;
  • STARTTL support;
  • XSLT processing;
  • TLS support;
  • SSL support.

Regarding the Apache web server, one of the main problems is the limited number of connections, which greatly increases the memory consumption. Still, the Nginx web server has solved this problem and released it under the BSD license.

  • You can follow the following applications in this way:
  • Hide server inventory
  • Passing all requests through the reverse proxy
  • Broadcast requests specifically
  • Reduce content pressure
  • Request compression between servers

To express more features of this web server that affect its performance, as mentioned above, we can pinpoint the matter of memory load balancing, bandwidth access control, and the ability to integrate with other applications. In fact, Engine X is a suitable option for website architects launching existing sites. This web server is the second most popular open-source web server on the Internet. This web server's best point is in supporting static files. This service has a very low RAM consumption, but its response speed for requests is very high. This service is used for load balance systems. You can visit the Serverclick website to buy the best Nnginex service.

🎁🎁Experience the ultimate hosting solution with our high-performance VPS, providing you with unrivalled speed, reliability, and scalability for your website or application.🎁🎁

An extended version of the settings file:

server {

        listen 80;

        listen [::]:80;

        access_log /var/log/nginx/reverse-access.log;

        error_log /var/log/nginx/reverse-error.log;

        Location / {

        proxy_pass http://127.0.0.1:3000;

            proxy_http_version 1.1;

            proxy_cache_bypass $http_upgrade;

            proxy_set_header upgrade $http_upgrade;

            proxy_set_header connection "upgrade";

            proxy_set_header host $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;

            proxy_set_header X-Forwarded-Host $host;

            proxy_set_header X-Forwarded-Port $server_port;

  }

In conclusion, setting up an Nginx reverse proxy is a powerful and efficient solution for enhancing your web server's performance, security, and flexibility, allowing you to seamlessly route and manage incoming traffic to multiple backend servers and ensuring a smooth and reliable user experience for your website or application. Following the steps outlined in this guide, you can confidently configure your Nginx reverse proxy and unlock the benefits of load balancing, caching, SSL termination, and much more. So, optimize your server infrastructure and take your online presence to the next level with Nginx reverse proxy.

People also read: 

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.