Nginx Security Hardening Guide
Nginx Security Hardening Guide
The first step in web security is to have SSL implemented so you can access web applications with
https and add a layer of encryption in communication.
Above command will generate CSR and key file at current working directly. Don’t forget to
change the .csr and .key file name.
Get the CSR signed by certificate authority and once you have the signed certificate, you can
implement them in nginx as below.
Edit the file and add following, which will enable nginx to listen on 443 port
server {
listen 443 ssl;
server_name bestflare.com;
ssl on;
ssl_certificate /opt/cert/bestflare.pem;
ssl_certificate_key /opt/cert/bestflare.key;
}
Note: don’t forget to change the certificate and key file path.
Save the configuration and restart the nginx. And SSL cert is implemented successfully.
SSL/TLS Optimization
Having SSL doesn’t mean it’s fully secure and that’s where as a Web Security expert, you need to
apply a configuration to secure the web server.
To start with, I would recommend running SSL scan against the website to find the score and essential
vulnerability.
So current SSL Labs rating is “C” and a target is to make it “A.”
Not having the chain certificate also affect the overall rating and this may show an error when
browsing in a modern browser like Chrome. You need to obtain a chain certificate from authority.
Mostly you will find on their website or just Google it.
Add the chain cert contents in website certificate like below. In my example, it would be
/opt/cert/bestflare.pem
Save the file and restart the nginx
Secure Diffie-Hellman for TLS
Diffie-Hellman is less secure than it was believed. One of the best practices lately added in a list is to
secure diffie-hellman. Generating unique DH GROUP and adding ssl_dhparam in ssl.conf file does
this.
Woo ho! So now you can see it’s “A” rating by SSLLabs. Well done!
Information Leakage
In default nginx installation you will have much sensitive information reveled, which can help hacker
to prepare for an attack.
If you are working on PCI compliance environment, this is considered as information leakage
vulnerability and must fix the item.
You have to use server_tokens off to disable the information leakage. I have explained this in my
previous article. Remove Version from Server Header Banner in nginx
Default nginx configuration is not perfect and can have many vulnerabilities that’s why we harden
them to make it secure.
Most of the time, you need just GET, HEAD & POST HTTP request in your web application. Allowing
TRACE or DELETE is risky as it can allow Cross-Site Tracking attack and potentially allow hacker to
steal the cookie information.
Clickjacking Attack
X-XSS Protection
Inject HTTP Header with X-XSS protection to mitigate Cross-Site scripting attack.
Alternatively, if you may consider using cloud-based security like SUCURI in front of the Nginx server.
Last but not the least, you need to keep your nginx up-to-date as there are many performance
enhancement, security fixes and new features are being added.
Next, you may be interested in learning to build Nginx for high-performance from scratch.
ABOUT THE AUTHOR
Hey,
My name is Chandan and I hope this guide helps you in securing Nginx Web
Server from common online vulnerabilities.
Let's be in touch.