DEV Community

Cover image for Nginx: custom log file, reverse proxy, and load balancer
Ivan Yu
Ivan Yu

Posted on • Originally published at ivanyu2021.hashnode.dev

Nginx: custom log file, reverse proxy, and load balancer

Summary

This blog is an explanation of Nginx configuration of:

  • Custom log file, specifies log files path
  • Reverse proxy server, hide the web application server from host
  • Load balancer, distribute network requests to different servers

Custom Log file, specifies log files path

Specifies the log file so you can get the log when any issue occurs.

You can tell Nginx to log the information by adding 2 parameters access_log and error_log

server {
    listen 30066;

    access_log /var/log/nginx/reverse-access.log;
    error_log /var/log/nginx/reverse-error.log;
}
Enter fullscreen mode Exit fullscreen mode
  • access_log = log files consist of all requests access the server (filename should be different from access.log)
  • error_log = log files, which record error (filename should be different from error.log)

If you do not include the settings access_log or error_log, although you can still find the log files under /var/log/nginx/, those files are empty

Reverse proxy server, hide the web application server from host

By using the reverse proxy server, you can hide the server hosting web application from user.

Consider the code below:

image.png

  • Only open 30066 port for external access in the Linux hosting Ngnix
  • User access Ngnix by 30066
  • Ngnix passes the requests from 30066 to 30077 by
location / {
    proxy_pass http://localhost:30077;
}
Enter fullscreen mode Exit fullscreen mode
  • For 30077, it is the server that hosts the HTML and js files
server {
    listen 30077;

    root /usr/share/nginx/html;
    index index.html index.htm;
    location / {
        try_files $uri $uri/ /index.html =404;
    }
}
Enter fullscreen mode Exit fullscreen mode

You can copy the code below:

server {
    listen 30066;

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

## real front-end server
server {
    listen 30077;

    root /usr/share/nginx/html;
    index index.html index.htm;
    location / {
        try_files $uri $uri/ /index.html =404;
    }
}
Enter fullscreen mode Exit fullscreen mode
  • You can also set up different rules to pass to different locations
server {
    listen 30066;

    location /sysaid {
        proxy_pass http://172.18.0.5:31339;
    }

    location / {
        proxy_pass http://localhost:30077;
    }
}
Enter fullscreen mode Exit fullscreen mode

The above code means that:

  • First, it checks whether the request url has /sysaid, if yes, it will pass to http://172.18.0.5:31339
  • Otherwise, it will pass to http://localhost:30077

Load balancer, distribute network request

When there is a lot of users or the web is heavily used, we need to distribute the workload to different web servers in order to maintain the stability and performance.

Consider the code below:

image.png

  • First, user access Nginx by 30066 port
  • Then, Nginx pass the request to the proxy
  • Ngnix will pass the request to the upstream server web
  • Finally, it will pass to request to either one of the server specifies under upstream web

Requests will be distributed equally to each server.

For example, if you have 4 requests coming in order (A, B, C, D), A and C will be passed to web_primary_1 and B and D will be passed to web_secondary_1

Top comments (0)