Tuesday, December 6, 2016

Enable https for Nginx

Modify listen line in config file for example /etc/nginx/sites-enabled/default.

listen 80 default_server;

and add listen 443 and certificate/key path. Following example is for Let's Encrypt installation.

listen 80;
listen 443 default ssl;
ssl_certificate /etc/letsencrypt/live/domainname/cert.pem;
ssl_certificate_key /etc/letsencrypt/live/domanname/privkey.pem;

To force http to https, add following in the server block say just after above entries. Myserver.com is used as server_name in following example.

server_name myserver.com;
if ($scheme = http) {
      return 301 https://$server_name$request_uri;

Check syntax error in config file:

nginx -t

Reload or restart Nginx server.

/etc/init.d/nginx reload|restart

No comments: