nginx,

How to set up Nginx reverse proxy for your application

Aug 14, 2021 · 1 min read · Post a comment
How to set up Nginx reverse proxy for your application

Using Nginx reverse proxy could be very useful, especially if you are developing an application with a framework that can run its own server like nodejs, python, ruby on rails, etc… It doesn’t matter if you are running a node-local server with a specific port, but deploying on production is good to have an Nginx reverse proxy.

Prerequisites

  • Nginx

Solution

Step 1. Open the Nginx config directory:

sudo cd /etc/nginx/

Step 2. You need to update the vhost file of your application. The vhost config file is usually stored under sites-available/ or conf.d/ subdirectory. If you want to proxy all the application requests update the root location block with the following config:

server {
    ...
    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://localhost:port;
    }
    ...
}
  • proxy_set_header : It will allow adding fields to the request header which is passed to the proxied server
  • proxy_pass : Accepts protocol or IP address of the proxied server.

Step 3. Test the Nginx configuration and reload:

nginx -t
nginx -s reload

Conclusion

As I mentioned previously, creating an Nginx proxy can cover complex scenarios depending on your requirements, but in this tutorial, you are going to learn how to set up nginx reverse proxy for your application which listens on a specific port. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on telegram.