Nginx Testing Environment
We will use the following environment in this guide:
- Debian GNU/Linux 8.1 (jessie).
- IP address: 192.168.0.25 (helptechnews.com) and 192.168.0.26 (helptechnews.com), as described in the IP-based virtual hosts section at
- Nginx version: nginx/1.6.2.
- For your convenience, here is the final configuration file.
With that in mind, let’s begin.
TIP #1: Keep Nginx up to date
At the time of this writing, the latest Nginx versions in the CentOS (in EPEL) and Debian repositories are 1.6.3 and 1.6.2-5, respectively.
Although installing software from the repositories is easier than compiling the program from source code, this last option has two advantages: 1) it allows you to build extra modules into Nginx (such as mod_security), and 2) it will always provide a newer version than the repositories (1.9.9 as of today). The release notes are always available in the Nginx web site.
TIP #2: Remove Unnecessary Modules in Nginx
To explicitly remove modules from Nginx while installing from source, do:
For example:
As you will probably guess, removing modules from a previous Nginx installation from source requires performing the compilation again.
A word of caution: Configuration directives are provided by modules. Make sure you don’t disable a module that contains a directive you will need down the road! You should check the nginx docs for the list of directives available in each module before taking a decision on disabling modules.
TIP #3: Disable server_tokens Directive in Nginx
The server_tokens
directive tells Nginx to display its current version on error pages. This is not desirable since you do not want to share that information with the world in order to prevent attacks at your web server caused by known vulnerabilities in that specific version.
To disable the server_tokens
directive, set if to off inside a server block:
Restart nginx and verify the changes:
TIP #4: Deny HTTP User Agents in Nginx
A HTTP user agent is a software that is used for content negotiation against a web server. This also includes malware bots and crawlers that may end up impacting your web server’s performance by wasting system resources.
In order to more easily maintain the list of undesired user agents, create a file (/etc/nginx/blockuseragents.rules
for example) with the following contents:
And an if statement to return a 403 response if the user agent string is in the black list defined above:
Restart nginx, and all user agents whose string matches the above will be blocked from accessing your web server. Replace 192.168.0.25 with your server’s IP and feel free to choose a different string for the --user-agent
switch of wget:
TIP #5: Disable Unwanted HTTP Methods in Nginx
Also known as verbs, HTTP methods indicate the desired action to be taken on a resource served by Nginx. For common web sites and applications, you should only allow GET, POST, and HEAD and disable all others.
To do so, place the following lines inside a server block. A 444 HTTP response means an empty response and is often used in Nginx to fool malware attacks:
To test, use curl to send a DELETE request and compare the output to when you send a regular GET:
TIP #6: Set Buffer Size Limitations in Nginx
To prevent buffer overflow attacks against your Nginx web server, set the following directives in a separate file (create a new file named /etc/nginx/conf.d/buffer.conf
, for example):
The directives above will ensure that requests made to your web server will not cause a buffer overflow in your system. Once again, refer to the docs for further details on what each of them does.
Then add an include directive in the configuration file:
TIP #7: Limit the Number of Connections by IP in Nginx
In order to limit the connections by IP, use the limit_conn_zone
(in a http context or at least outside the server block) and limit_conn (in a http, server block, or location context) directives.
However, keep in mind that not all connections are counted – but only those that have a request processed by the server and its whole request header has been read.
For example, let’s set the maximum number of connections to 1
(yes, it’s an exaggeration, but it will do the job just fine in this case) in a zone named addr (you can set this to whatever name you wish):
A simple test with Apache Benchmark (Perform Nginx Load) where 10
total connections are made with 2
simultaneous requests will help us to demonstrate our point:
See the next tip for further details.
TIP #8: Setup Monitor Logs for Nginx
Once you have performed the test described in the previous tip, check the error log that is defined for the server block:
You may want to use grep to filter the logs for failed requests made to the addr zone defined in TIP #7:
Likewise, you can filter the access log for information of interest, such as:
- Client IP
- Browser type
- HTTP request type
- Resource requested
- Server block answering the request (useful if several virtual hosts are logging to the same file).
And take appropriate action if you detect any unusual or undesired activity.
TIP #9: Prevent Image Hotlinking in Nginx
Image hotlinking happens when a person displays in another site an image hosted on yours. This causes an increase in your bandwidth use (which you pay for) while the other person happily displays the image as if it was his or her property. In other words, it’s a double loss for you.
For example, let’s say you have a subdirectory named img
inside your server block where you store all the images used in that virtual host. To prevent other sites from using your images, you will need to insert the following location block inside your virtual host definition:
Then modify the index.html
file in each virtual host as follows:
192.168.0.26 | 192.168.0.25 |
<!DOCTYPE html> <html> <head> <meta charset=”utf-8″> <title>Nginx means power</title> </head> <body> <h1>Nginx means power!</h1> <img src=”http://192.168.0.25/img/nginx.png” /> </body> </html> | <!DOCTYPE html> <html> <head> <meta charset=”utf-8″> <title>helptechnews loves Nginx</title> </head> <body> <h1>helptechnews loves Nginx!</h1> <img src=”img/nginx.png” /> </body> </html> |
Now browse to each site and as you can see, the image is correctly displayed in 192.168.0.25 but is replaced by a 403 response in 192.168.0.26:
Note that this tip depends on the remote browser sending the Referer field.
TIP #10: Disable SSL and only Enable TLS in Nginx
Whenever possible, do whatever it takes to avoid SSL in any of its versions and use TLS instead. The following ssl_protocols
should be placed in a server or http context in your virtual host file or is a separate file via an include directive (some people use a file named ssl.conf
, but it’s entirely up to you):
For example:
TIP #11: Create Certificates in Nginx
First off, generate a key and a certificate. Feel free to use a different type of encryption if you want:
Then add the following lines inside a separate server block in preparation for the next tip (http --> https
redirection) and move the SSL-related directives to the new block as well:
In the next tip we will verify how our site is now using a self-signed cert and TLS.
TIP #12: Redirect HTTP traffic to HTTPS in Nginx
Add the following line to the first server block:
The above directive will return a 301 (Moved permanently) response, which is used for permanent URL redirection whenever a request is made to port 80 of your virtual host, and will redirect the request to the server block we added in the previous tip.
The image below shows the redirection and confirms the fact that we are using TLS 1.2 and AES-256 for encryption:
Comments
Post a Comment