Nginx: Single Server to Serve Both HTTP and HTTPS

In Nginx it is possible to handle both normal HTTP and secure HTTPS connections with single server configuration. It is easier to manage single server configuration than having different server configuration for both HTTP and HTTPS connections. You can configure the server like given below.

server {
    listen              80;
    listen              443 ssl;
    server_name         example.com;
    ssl_certificate     /path/to/example.com.pem;
    ssl_certificate_key /path/to/example.com-privkey.pem;
    ...
}

If you are updating older server configuration, then please remove ssl on; directive if it is there. Otherwise it will make problems when accessing the site with normal HTTP connection.

Note that, you cannot configure server like this on Nginx of version older than 0.7.14. But that is very old version, so it is very unlikely to worry about it.