NGINX as a Reverse Proxy

Sadil Chamishka
2 min readDec 4, 2019

--

Nginx is a web server and also can be used as a reverse proxy.

apt install nginx

First, we can disable the default virtual host that is pre-configured when Nginx is installed.

unlink /etc/nginx/sites-enabled/default

We can edit nginx.conf file in order to configure the NGINX server to act as a reverse proxy.

we can set worker_processes count based on the number of cores allocating for worker processers. Also, set worker_connections as the number of connections concurrently handled by one processor.

helloworld is a service behind the NGINX proxy with endpoint port 80. Load balancing will happen over the 2 hosts mentions there.

upstream helloworld {
server 54.234.84.228:8080;
server 35.173.242.196:8080;
}
server {
listen 80;
listen [::]:80
access_log /var/log/nginx/reverse-access.log;
error_log /var/log/nginx/reverse-error.log;
location / {
proxy_pass http://helloworld;
}
}

We can also configure the way of load balancing as follows.

  • round-robin — requests to the application servers are distributed in a round-robin fashion,
  • least-connected — next request is assigned to the server with the least number of active connections,
  • ip-hash — a hash-function is used to determine what server should be selected for the next request (based on the client’s IP address). Therefor one client will direct to the same server.

--

--