How to configure Nginx as a reverse proxy

This post guide you how to configure Nginx as a reverse proxy in just several step. Looking for installing Nginx on Ubuntu here or on Centos here.

In the series about Nginx:

Step 1: Install Nginx

You can ignore this step if you already done. As I told you above, take a look at here or here for installing Nginx on Ubuntu or Centos

Step 2: Edit confiuration file

vi /etc/nginx/nginx.conf

This file will look like this

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

As a reverse proxy, I will configure Nginx listens at port 2810 and forward all requests to localhost:2710 to process. This will looks like this:

# Consider our server named config9.com
Users <----> config9.com:2810 <----> config9.com:2710

So now we need to modify the configuration file to meet our needs. Replace this text

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

with this

    server {
        listen       2810 default_server;
        listen       [::]:2810 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_pass http://localhost:2710;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

and save the file, restart Nginx with this command to apply changes

sudo systemctl restart nginx

To enable nginx to run at system startup:

sudo systemctl enable nginx

Now you can access website at: http://config9.com:2810 and all requests will be forward to http://config9.com:2710 to process.

Related:  Does a reverse proxy make node.js safe?

3 thoughts on “How to configure Nginx as a reverse proxy”

Leave a Reply

Your email address will not be published. Required fields are marked *