How To Setup HTTP Reverse Proxy with Nginx

07/22/2020Infrastructure,NginxNginx,Reverse Proxy

If you want to know how to install NGINX, please visit “How To Install Nginx on Ubuntu 20.04" at first.

Setup HTTP Reverse Proxy!

Let’s try to setup a HTTP reverse proxy server with Nginx as the following picture.

Understand Nginx’s configuration files.

Nginx’s configuration files are located under the /etc/nginx directory.

/etc/nginx$ tree
.
├── conf.d
│   └── << Here, You need to put your webserver's config file!
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules-available
├── modules-enabled
│   ├── 50-mod-http-image-filter.conf -> /usr/share/nginx/modules-available/mod-http-image-filter.conf
│   ├── 50-mod-http-xslt-filter.conf -> /usr/share/nginx/modules-available/mod-http-xslt-filter.conf
│   ├── 50-mod-mail.conf -> /usr/share/nginx/modules-available/mod-mail.conf
│   └── 50-mod-stream.conf -> /usr/share/nginx/modules-available/mod-stream.conf
├── nginx.conf << Main Config file!
├── proxy_params
├── scgi_params
├── sites-available
│   └── default
├── sites-enabled
│   └── default -> /etc/nginx/sites-available/default
├── snippets
│   ├── fastcgi-php.conf
│   └── snakeoil.conf
├── uwsgi_params
└── win-utf

The configuration file “/etc/nginx/nginx.conf" is a parent configuration file. In this file, all child configuration files located on the “/etc/nginx/conf.d/" and “/etc/nginx/sites-enabled/" directory are included at the http{} block.

/etc/nginx/nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
:
:
        ##
        # Virtual Host Configs
        ##
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}
:

So as a basic strategy, we should put customized child configuration files on “/etc/nginx/conf.d/" or “/etc/nginx/sites-enabled/" directories.

Create a Child Config File.

For example, Create the “/etc/nginx/conf.d/domain-a.com.conf" file. Because those child config files are included at the http{} block in the parent config file “/etc/nginx/nginx.conf“, basically, to avoid syntax errors, you need to use server{} blocks that can be located inside the http{} block.

Example: domain-a.com.conf

server {
    listen 80;
    server_name domain-a.com;

 location /{
    proxy_pass  http://server_a:8080;
}

Check config files’ syntax.

$ nginx -t

Reload config files.

$ nginx -s reload

In this example, the requests coming to domain-a.com's 80 port are forwarded by Nginx to the server_a's 8080 port.


Add Another Config files for Multiple Domains

For example, create another config file “/etc/nginx/conf.d/domain-b.com.conf" for the web server “server_b“.

server {
    listen 80;
    server_name domain-b.com;

 location /{
    proxy_pass  http://server_b:8080;
}

After reloading config files, Nginx can forward request as the following picture.


<< Previous Step

Next Step >>