
Enhancing Nginx Server Security by Hardening Configuration
Currently, nginx is the most popular web server, recently beating Apache. It is lightweight, fast, robust, and supports all major operating systems. It is the web server of choice for Netflix, WordPress.com, and other high traffic sites. An nginx server can easily handle 10,000 inactive HTTP connections with as little as 2.5 MB of memory. In this article, we will provide tips on nginx server security, showing you how to secure your nginx installation.
After installing nginx, you should gain a good understanding of its configuration settings, which are found in the nginx.conf file. This is the main configuration file for nginx and therefore most security checks will be done using this file. By default, you can find nginx.conf in [nginx installation directory]/conf on Windows systems, and in /etc/nginx or /usr/local/etc/nginx on Linux systems. You may also need to do some changes to virtual host configuration files, typically contained in the sites-available subdirectory.
Step 1. Disable Any Unwanted nginx Modules
When you install nginx, it automatically includes many modules. Currently, you cannot choose modules at runtime. To disable certain modules, you need to recompile nginx. We recommend that you disable any modules that are not required as this will minimize the risk of potential attacks by limiting allowed operations.
To do this, use the configure option during installation. In the example below, we disable the autoindex module, which generates automatic directory listings, and then recompile nginx.
# ./configure --without-http_autoindex_module
# make
# make install
Step 2. Disable nginx server_tokens
By default, the server_tokens directive in nginx displays the nginx version number. It is directly visible in all automatically generated error pages but also present in all HTTP responses in the Server header.
This could lead to information disclosure – an unauthorized user could gain knowledge about the version of nginx that you use. You should disable the server_tokens directive in the nginx configuration file by setting server_tokens off
.
Step 3. Control Resources and Limits
To prevent potential DoS attacks on nginx, you can set buffer size limitations for all clients. You can do this in the nginx configuration file using the following directives:
- client_body_buffer_size – use this directive to specify the client request body buffer size. The default value is 8k or 16k but it is recommended to set this as low as 1k:
client_body_buffer_size 1k
. - client_header_buffer_size – use this directive to specify the header buffer size for the client request header. A buffer size of 1k is adequate for most requests.
- client_max_body_size – use this directive to specify the maximum accepted body size for a client request. A 1k directive should be sufficient but you need to increase it if you are receiving file uploads via the POST method.
- large_client_header_buffers – use this directive to specify the maximum number and size of buffers to be used to read large client request headers. A
large_client_header_buffers 2 1k
directive sets the maximum number of buffers to 2, each with a maximum size of 1k. This directive will accept 2 kB data URI.
Step 4. Disable Any Unwanted HTTP methods
We suggest that you disable any HTTP methods, which are not going to be utilized and which are not required to be implemented on the web server. If you add the following condition in the location block of the nginx virtual host configuration file, the server will only allow GET, HEAD, and POST methods and will filter out methods such as DELETE and TRACE.
location / {
limit_except GET HEAD POST { deny all; }
}
Another approach is to add the following condition to the server section (or server block). It can be regarded as more universal but you should be careful with if
statements in the location context.
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}
Step 5. Install ModSecurity for Your nginx Web Server
ModSecurity is an open-source module that works as a web application firewall. Its functionalities include filtering, server identity masking, and null-byte attack prevention. The module also lets you perform real-time traffic monitoring. We recommend that you follow the ModSecurity manual to install the mod_security module in order to strengthen your security options.
Note that if ModSecurity does not meet your needs, you can also use other free WAF solutions.
···