nginx,

How to set up Nginx reverse proxy for your application

Aug 14, 2021 · 1 min read · Post a comment

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 Node.js, 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
  • sudo privileges

Solution

Step 1. Open the Nginx config directory.

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: 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 specifically, you learned 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.