Jan Hřídel

March 8, 2023

Set up an Nginx virtual host as a reverse proxy

To set up an Nginx virtual host as a reverse proxy, just follow these steps:

Install Nginx

First, you need to install Nginx on your server. You can do this by running the following command:

sudo apt-get install nginx

Create a new virtual host configuration file

Create a new configuration file for your virtual host using your favorite text editor. For example, if you want to create a virtual host for example.com, create a file named example.com.conf in the /etc/nginx/sites-available/ directory.

sudo vim /etc/nginx/sites-available/example.com.conf

Add the server block

In the configuration file, add the following server block:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Replace example.com with your domain name, and http://localhost:3000 with the URL of the backend server that you want to proxy to.

Save and exit the file

Save the changes to the file and exit the text editor.

Enable the virtual host

Create a symbolic link to the virtual host configuration file in the /etc/nginx/sites-enabled/ directory.

sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

Restart Nginx

Restart the Nginx server to apply the changes.

sudo systemctl restart nginx


That's it! Your Nginx virtual host is now set up as a reverse proxy. Any requests that come to your domain will be proxied to the backend server that you specified in the proxy_pass directive.

#nginx #debian #ubuntu #proxy #server #config

About Jan Hřídel


Full-stack web developer, web technology enthusiast, lecturer, team player as well as strong individual, daddy & family man.
#WebDevWizard