What is Nginx
Nginx is a lightweight, high-performance web server and reverse proxy server, known for its low memory usage and strong concurrency. It is widely used to deliver web content, terminate encrypted connections, and forward client requests to backend servers.
Unlike the traditional multi-process or multi-thread model, Nginx uses an event-driven, asynchronous, non-blocking I/O model. This allows it to keep resource consumption low and response speed high, even when handling a large number of concurrent connections. Its architecture consists of a master process and multiple worker processes: the master process reads and evaluates the configuration and manages the workers, while the worker processes handle the actual client requests.
How Nginx Works
On an AsterNOS routing device, Nginx is controlled through the device's own command line and a set of configuration files.
Server blocks: A server block defines a virtual host, which can distinguish different websites by domain name, IP address, and port. One http block can contain multiple server blocks. If SSL-encrypted access is used, the certificate and key file paths must be specified in the block as well.
Local content: A server block can serve resources stored on the device, such as files in the local eMMC partition. Removable USB drives or NVMe disks can be mounted and used the same way.
Reverse proxy: A server block can also forward client requests to a backend server. Requests can be split by URL path, so, for example, a path ending in
/static/is served from local storage while all other requests are proxied.Core allocation: Nginx runs together with VPP (Vector Packet Processing) on the device's CPU cores. The core counts for each can be configured to balance packet forwarding and web service performance.
Configuration directories: Nginx files are stored under
/etc/sonic/nginx/. Theconffolder holds the running configuration, theconfigedfolder holds the files read whenevernginx reloadis executed, and thesavedfolder holds configurations saved with thewritecommand. Files should be edited inconfiged, since changes toconfare overwritten at the next reload.Applying changes: After any configuration change, such as updating a server file, changing global parameters, or adjusting the Nginx core count,
nginx reloadmust be executed. To keep the configuration after a device restart, it must be saved withwrite.
Why Nginx is Beneficial
Running Nginx directly on the network device brings web delivery and proxying closer to the traffic:
High concurrency with low resource usage: The event-driven model handles many simultaneous connections efficiently.
Flexible request handling: URL-based rules let one service serve local files for some paths and proxy others to backend servers.
Secure access: SSL support allows encrypted access using certificates stored on the device.
Fewer extra devices: Web content delivery and reverse proxy functions can be provided by the same device that routes the traffic.
Tunable performance: Core allocation, connection limits, keepalive timeouts, and queue cache sizes can be adjusted to fit the workload.