Nginx Server Security: Nginx Hardening Guide (2024)

Web server

Nginx is a lightweight, open-source, robust, high-performance HTTP server and a reverse proxy. It’s the most popular web server, beating Apache and IIS.

Nginx is recognized for its stability, performance, rich feature set, easy configuration, and low resource consumption.

While the default configurations are favoured by most people, they are not secure enough, and extra tweaks are needed to reinforce the web server.

Top 7 methods for Nginx hardening

Here, we will look into some actions you can take to strengthen and improve Nginx server security.

1. Disable Any Unwanted Modules

While installing Nginx, in default it includes many modules. Currently, we cannot choose modules at runtime. To disable specific modules, you need to recompile Nginx.

It is advised to disable any modules that are not used as this will decrease the risk of potential attacks by limiting operations.

2. Implement SSL Certificate

The primary and essential step in strengthening your Nginx server security is to include an additional layer of protection using an SSL certificate.

The SSL certificate is a digital certificate powered by cryptography that encrypts data traffic between your web server and the web browser. SSL certificate also forces your website to use the secure HTTPS protocol over HTTP, which transmits traffic in plain text.

You can make use of Let’s Encrypt SSL which is free or any certificate according to your preference. There are two additional settings that should be taken care of after you implement a SSL certificate.

  • Disable weak SSL/TLS protocols

Implementing SSL does not imply that your site is secure. Deprecated versions of SSL such as TLS 1.0, TLS 1.1 are identified as weak, and these protocols are inclined to SSL and TLS vulnerabilities such as POODLE, BEAST, and CRIME.

The most commonly used web browsers like Chrome, Firefox, Safari and Edge no longer support TLS 1.0 and TLS 1.1.

To implement TLS 1.2 and TLS 1.3, we have to edit two configuration files:

  • /etc/nginx/nginx.conf

  • /etc/nginx/sites-available/default

Locate the following line in nginx.conf file

 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE 

And, remove TLS versions 1 and 1.1 and add TLS 1.3

 ssl_protocols TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE 

However, obsolete protocols also may be present in your Nginx server block configuration files. The block configuration files are in the directory /etc/nginx/sites-available/.

Therefore, continue and change your block configuration file as before.

Find the following line

 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 

As before, delete the TLSv1 and TLSv1.1 protocol and add the TLSv1.3.

 ssl_protocols TLSv1.2 TLSv1.3; 
  • Disable weak cipher suites

Weak cipher suites may lead to vulnerabilities, and as a secure practice, we must make sure that only strong ciphers are allowed.

Add the following to the server block in the ssl.conf file

 ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA HIGH !RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS"; 

3. Exclude Server Headers

The default state of a Nginx server returns something like Server: nginx/1.12.1 in the server response or error pages. The best method is to remove the header completely. For that, we require the installation of nginx-extras.

For Debian/Ubuntu

 sudo apt-get install nginx-extras 

For RHEL

 yum install nginx-plus-module-headers-more 

After installing and restarting the Nginx service, add the following lines to your nginx.conf file.

 # /etc/nginx/nginx.conf http { # Basic Settings more_set_headers 'Server: '; 

If no value is specified, the header will be displayed empty. We can also create custom headers as given below.

 # /etc/nginx/nginx.conf http { # Basic Settings more_set_headers 'Server: Custom Header'; 

To completely disable the server header, find server_token in the Nginx configuration file and set it to server_tokens off (by removing # in the nginx.conf file).

Nginx Server Security: Nginx Hardening Guide (1)

4. Resource Control

To counter potential DoS attacks on Nginx it’s possible to set buffer size restrictions for all the clients.

These settings also prevent any potential buffer overflow attacks.

Adding the below lines to the nginx.conf file will prevent any such attack.

 ##buffer policy client_body_buffer_size 1K; client_header_buffer_size 1k; client_max_body_size 1k; large_client_header_buffers 2 1k; ##end buffer policy 

5. Disable unsafe HTTP methods

Another sound practice for hardening Nginx server is to disable unsafe HTTP methods. Most of the time, GET, HEAD & POST methods are only used. Allowing TRACE or DELETE is unsafe as it can lead to attacks such as cross-site tracking.

The line below will only permit GET, POST, and HEAD methods and exclude all the other methods including TRACE and DELETE.

Modify nginx.conf and add the following under server block

 if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 405;s } 

6. Configure Security Headers

For further Nginx hardening, you can add several different HTTP security headers to the server. These HTTP security headers tell the browser how to behave while handling the website content.

I happened to cover an in-depth blog on how you can harden server security by implementing security headers.

Configure and monitor Nginx Logs

Nginx access logs and error logs are enabled by default and are found in logs/error.log and logs/access.log.

We can also specify the logs to be recorded according to their severity level.

Continuously monitoring the Nginx log gives you a better understanding of requests made to your Nginx web server and also helps you to notice any encountered errors. This helps in discovering any attack attempts and also helps in identifying what you should do to optimize the server performance.

7. Update Nginx Server Regularly

Maintaining your web server up to date is mandatory in order to ensure Nginx server security.

Updating the server on time applies the patches that will address pre-existing vulnerabilities. If left ignored, they can be exploited to compromise your server.

Other than that, several performance enhancements, security fixes, and new features are also being added regularly with every update.

Conclusion

I believe I’ve covered all the fundamental actions that you can take to harden your Nginx web server. Leaving the server in default configuration is not a secure practice to follow and what we neglect as a low risk might become a possible threat in the future.

If you have any thoughts on Nginx server security or would like to ask me a question regarding it, please feel free to leave a comment below. I’d love to help you out!

Automated human-like penetration testing for your web apps & APIs

Teams using Beagle Security are set up in minutes, embrace release-based CI/CD security testing and save up to 65% with timely remediation of vulnerabilities. Sign up for a free account to see what it can do for you.

Sign up free

Product tour

Written by

Nginx Server Security: Nginx Hardening Guide (2)

Manieendar Mohan

Cyber Security Lead Engineer

Nginx Server Security: Nginx Hardening Guide (2024)

FAQs

Nginx Server Security: Nginx Hardening Guide? ›

By default, Nginx installs with several older SSL protocols exposed, which could lead to a BEAST (Browser Exploit Against SSL/TLS) attack. Older protocols should therefore be disabled for a better security posture.

Is nginx secure by default? ›

By default, Nginx installs with several older SSL protocols exposed, which could lead to a BEAST (Browser Exploit Against SSL/TLS) attack. Older protocols should therefore be disabled for a better security posture.

Is nginx more secure? ›

Security – both Apache and NGINX are secure and reliable. They also have several security tools to protect a site against DDoS attacks. Support – Apache and NGINX offer community support and documentation to help beginners with issues. However, NGINX offers direct support on the premium version.

How secure is nginx proxy? ›

nginx's core codebase (memory management, socket handling, etc) is very secure and stable, though vulnerabilities in the main binary itself do pop up from time to time. For this reason it's very important to keep nginx up-to-date.

Does NGINX use SSL or TLS? ›

Although the certificate and the key are stored in one file, only the certificate is sent to a client. The directives ssl_protocols and ssl_ciphers can be used to limit connections to include only the strong versions and ciphers of SSL/TLS. By default nginx uses “ ssl_protocols TLSv1 TLSv1.

What is the vulnerability of NGINX? ›

Nginx-UI is a web interface to manage Nginx configurations. It is vulnerable to an authenticated arbitrary command execution via CRLF attack when changing the value of test_config_cmd or start_cmd. This vulnerability exists due to an incomplete fix for CVE-2024-22197 and CVE-2024-22198.

What are the downsides of NGINX? ›

While NGINX boasts numerous advantages, it's essential to consider potential drawbacks: 1) Complex configuration: Some users find NGINX's configuration more intricate than that of other web servers. Configuring it for specific requirements may pose challenges for those unfamiliar with its setup.

Do people still use NGINX? ›

Thanks to its robust architecture, rich feature set, and widespread adoption, NGINX has become a popular choice for developers and system administrators seeking a high-performance web server and reverse proxy solution.

Can NGINX terminate SSL? ›

The operation is called termination because NGINX Plus closes the client connection and forwards the client data over a newly created, unencrypted connection to the servers in an upstream group. In release R6 and later, NGINX Plus performs SSL termination for TCP connections as well as HTTP connections.

How do I protect my NGINX server? ›

The primary and essential step in strengthening your Nginx server security is to include an additional layer of protection using an SSL certificate. The SSL certificate is a digital certificate powered by cryptography that encrypts data traffic between your web server and the web browser.

Is Traefik better than NGINX? ›

Generally, Nginx outshines Traefik in raw performance and resource utilization, but performance can fluctuate based on specific use cases and configurations. Traefik's dynamic configuration allows real-time updates without the need for server restarts, offering flexibility and automatic service discovery.

Can NGINX block IP? ›

Nginx servers allow you to control IP access to your website by creating a custom configuration file. You can view the IP addresses that have visited your website by viewing your access. log file. Users whose IP address is blocked will see a 403 Forbidden error when visiting your website.

How to force NGINX to use HTTPS? ›

To redirect all websites from HTTP to HTTPS, create a single server block that listens on port 80 and uses either the return directive or the rewrite directive to force a redirection.

References

Top Articles
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 6586

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.