SSL on localhost with nginx

11/14/2011

Install nginx if it’s not already installed:

sudo apt-get install nginx

You must have the SSL module installed. The nginx docs say this is not standard. However, it does come installed on Ubuntu. You can verify by running nginx -V and looking for --with-http_ssl_module.

Next up is generating the SSL certs. Follow the Slicehost docs for this step.

Now you’ll need to update your /etc/nginx/nginx.conf file:

  upstream backend {
    server 127.0.0.1:9000;
  }

  server {
    server_name www.yourdomain.com yourdomain.com;
    rewrite ^(.*) https://www.yourdomain.com$1 permanent;
  }

  server {
    server_name local.yourdomain.com;
    rewrite ^(.*) https://local.yourdomain.com$1 permanent;
  }

  server {
    listen               443;
    ssl                  on;
    ssl_certificate      /etc/ssl/certs/myssl.crt;
    ssl_certificate_key  /etc/ssl/private/myssl.key;
    keepalive_timeout    70;
    server_name www.yourdomain.com local.yourdomain.com;
    location / {
      proxy_pass  http://backend;
    }
  }

Then restart nginx:

sudo nginx -s reload

Finally, in /etc/hosts put:

127.0.0.1   local.yourdomain.com

This will allow you to visit https://local.yourdomain.com/ which will be served up by the server that you have running on port 8080.

Be Sociable, Share!