Enable Perfect Forward Secrecy for nginx

Camilo Nova

Camilo Nova


2 min read.

Recently there have been some security concerns about SSL enabled sites, here I will show you how to make your site stronger to bad people by limiting the requests a user can make and enabling Perfect Forward Secrecy.

First of all we are going to limit the requests to your web server, this will prevent brute force attacks to your site. Nginx has a built-in module to limit the requests from the same IP address, this can be enabled changing your site file, the one is inside sites-available folder, like this:

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
    location / {
        limit_req      zone=one burst=10 nodelay;

The first line is going to create a zone named one with a 10mb size for a shared memory zone and to limit the requests to 1 per second. Later at the location level, it should useful if you set it just for the application and not in the static content location, you define to limit the requests using the zone we just created before with a burst of 10 and the nodelay option which will show an error page to someone who reach the limit of requests, otherwise it will only delay the response. You can get more information at the official documentation.

Next we are going to set the Perfect Forward Secrecy, this should be a must have when thinking about security, so we are going to add some "randomness" to our key file, the one we use for the SSL certificate; to find which one are you using, just go to your site file and search for the ssl_certificate parameter and there should be the path to your file, let's suppose is named my-key.crt, then we in the command line we issue:

$ openssl dhparam -rand - 2048 >> /path/to/my-key.crt

This is going to add a random data on you key to make it more strong, now we just need to add the ciphers to your nginx.conf file to make it work:

http {
        # SSL settings
        ssl_session_cache    shared:SSL:10m;
        ssl_session_timeout  10m;
        # Forward secrecy settings
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

The cipher list is kind of long, but it will work on most older browsers, if you don't need to think about IE 8 then you can use this short one:

ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";

We only need to restart nginx service and see how it works, you can check your site here:

Now your site should have an A+ rating.

Get a monthly update from our CEO:

Learn about how we think:

Temas para Sublime Text

Al momento de escribir código de programación que mejor herramienta que Sublime Text , usted puede hacer un sin número ...

Carlos Niño

Carlos Niño

Published Mar 28, 2014

Getting Started With Django Rest Framework

When developing web applications, sometimes we need to abstract the way we interact with data, so that we can access it and m...

Pablo Vallejo

Pablo Vallejo

Published Mar 27, 2014

How to use mixins to process a form in Django

Django is a quite powerful Python framework, but unfortunately it doesn't offer any solutions to process forms that work with...

Vera Mazhuga

Vera Mazhuga

Published Mar 24, 2014

Own your tech to own your destiny.

Let's Talk