We have ready Nginx server, ready PHP deamons with tweaked configuration so… what next? It’s time to secure our websites. With Nginx it’s very simple to run SSL/TLS connections and also HTTP/2 – faster protocol, great for mobile devices and new web browsers. Like in previous chapters, we will make changes not only to enable secure connections, but also tweak default settings – be aware, these recommendations will not work with old browsers like Opera 12, Internet Explorer 7 or old Android (2.x) build-in browsers. I thinks is isn’t any drawback, these browsers are obsolote for a long time. Ok, let’s start with Nginx configuration.
Nginx optimizations, tips and tricks (work in progress)
First step is to change listening port for our website and add additional configuration file – I preferer this method, becuase we can separate global, SSL settings for all websites in one file. Edit /etc/nginx-sites-available/your-config:
listen 443 ssl http2;
listen [::]:443 ssl http2;
It’s very simple simple example. First two lines – we change listening port to 443, enable SSL and HTTP/2. Next lines – set certificate and key files – you can generate these files using free Let’s Encrypt or receive from your service provider. We also include new file. Save vhost and add /etc/nginx/config/ssl.conf file with this content:
// Enable SSL
// Set protocols and ciphers - force browsers to use only server ciphers
ssl_protocols TLSv1.2 TLSv1.3;
// Enable HSTS - WARNING - after that, you MUST use SSL on your domain and all subdomains!
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload";
// Disable tickets because it isn't secure
// Stampling and resolve by Google servers
resolver 188.8.131.52 184.108.40.206 valid=300s;
// Additional headers for security
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
There are few settings, I think most are clear, I also add some comments to code. We will use only TLS 1.2 and 1.3 – without SSL and TLS 1.0 and 1.1 because it isn’t secure solution – you can enable these old protocols and change ciphers, but it isn’t good option if you website doesn’t have to be compatible with old devices, old browsers and operating systems. There is also enabled HSTS – it’s great, but you must remember, that all your subdomains must also use secure connections (with includeSubDomains parameter) – without that, browsers will display error and guests will not do anything with that. It’s because HSTS issues block access to website.
There is also enabled SSL stampling to check if certificates are revoked. I also added two important headers to secure websites. It’s should be enabled by default on web browsers, but it’s good idea to set these headers. Ok, we can now go to final step, redirect all non-secure connections to secure versions. Just creathe new vhost:
listen 80 default_server;
listen [::]:80 default_server;
return 301 https://$host$request_uri;
And it’s all. You should test Nginx configuration and restart server.