Nginx Guide: Essentials

The first part of this nginx article talked about the architecture and the main nginx configuration file. This article continues from there and talks about the nginx essentials: the modules that are needed and other advanced configuration techniques.

Nginx Modules

The real power of nginx lies in its modules. The entire stack can be imagined as made up of modules which enhance or build on top of the capabilities of the nginx core. To use them, they have to be enabled during compilation from source and cannot be enabled at run time.

Some modules are automatically compiled in unless otherwise stated. Of these, let’s take a look at some of the interesting ones and see which are widely used.

Limiting and Restricting Access

Access (ngx_http_access_module): This allows limiting access to certain IP addresses.

location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}

HTTP Auth (ngx_http_auth_basic_module): Allows limiting access by HTTP Basic Auth (username / password).

location / {
    auth_basic           "password";
    auth_basic_user_file conf/htpasswd;
}

Subrequest Auth (ngx_http_auth_request_module): Implements client authorization based on the result of a subrequest.

Limit connections (ngx_http_limit_conn_module): Allows you to define the maximum number of simultaneous connections from a single IP address.

Limit requests (ngx_http_limit_req_module): Limits the number of requests from a single IP.

Most modules that are included with nginx and can be enabled during compile are listed here in great detail. Let’s instead take a look at some third-party modules that can be really helpful.

Third-Party Modules

These modules need to be downloaded and then compiled with nginx like what you see in the following line of code (assuming that you’re working in the nginx source directory):

./configure --add-module=/path/to/module/source

The nginx wiki lists some of the well known third-party modules. Of all of these, let’s talk about a module that rewrites web pages and associated assets to reduce latency and bandwidth.

ngx_pagespeed

This is an nginx module developed by Google under the PageSpeed project. The purpose is obviously to improve page loading times with the help of a traffic-optimizing nginx module.

You can install this module by following the ‘Install ngx_pagespeed’ tutorial. This module is highly recommended as a must-have on your servers!

nginx-rtmp-module

Live streaming, recording, online transcoding, advanced buffering techniques. It is really easy to get started using this.

nginx-push-stream-module

An http stream push module for nginx. Supports EventSource Long polling. Example: WebSocket.

Other Essentials

While using nginx, it also becomes important to use the following supporting nginx utilities:

  1. ngxtop: ngxtop parses nginx access log and outputs top-like metrics of the nginx server.
  2. ngx_openresty: Turns nginx into a fully fledged web app server.
  3. server configs: Is a repository for configuration snippets of nginx. Really important when you are getting started and want to set up the configuration correctly. Helps you get started with the right configuration to improve the web site’s performance and security, and appropriate resource usage.
  4. lua-nginx-module: Embeds the power of Lua into Nginx.
  5. h5ai: Worth exploring. A modern HTTP web server index for Apache httpd, lighttpd, nginx and Cherokee.
  6. Collection of nginx resources.
  7. How to write nginx modules.

Source:: Net Tuts

Leave a Reply

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