NGINX is a fast and reliable open-source web server. It gained its popularity due to its low memory footprint, high scalability, ease of configuration, and support for the vast majority of different protocols. One of the protocols supported is the relatively new HTTP/2, which was published in May 2015. The main advantage of HTTP/2 is its high transfer speed for content-rich websites. PrerequisitesBefore we get started, we will need a few things:
Step 1 — Installing the Latest Version of NginxSupport of the First, update the list of available packages in the apt packaging system:
Then, install Nginx:
After the installation process finishes, you can check the version of Nginx by typing:
The output should be similar to the following:
Step 2 — Changing the Listening Port and Enabling HTTP/2The first change we will make will be to change the listening port from Let's open the configuration file:
By default, Nginx is set to listen to port
As you can see, we have two different listen variables. The first one is for all Modify the listening port to
Notice that in addition to ssl, we also added http2 to the line. This variable tells Nginx to use HTTP/2 with supported browsers. Step 3 — Changing the Server NameWe use the By default, server_name is set to
Save the configuration file and edit the text editor. Whenever you make changes to Nginx configuration files, you should check the configuration for syntax errors, like this:
If the syntax is error-free, you will see the following output:
Step 4 — Adding the SSL CertificatesNext, you need to configure Nginx to use your Create a directory to store your
Copy your certificate and the private key to this location. We will also rename the files to show which domain they are associated. This will come in handy in the future, when you have more than one domain associated with this server. Replace example.com with your actual hostname:
Now, let's open our configuration file one again and configure SSL.
On new lines inside the server block, define the location of your certificates:
Save the file, and exit the text editor. Step 5 — Avoiding Old Cipher Suites
We will use a really popular cipher set, whose security was approved by Internet giants like CloudFlare. It does not allow the usage of MD5 encryption (which was known as insecure since 1996, but despite this fact, its use is widespread even to this day). Open the following configuration file:
Add this line after
Save the file, and exit the text editor. Once again, check the configuration for syntax errors:
Step 6 — Redirecting all HTTP Request to HTTPSSince we are interested in serving the content through HTTPS only, we should tell Nginx what it should do if the server receives an HTTP request. At the bottom of our file, we will create a new server block for redirecting all HTTP requests to HTTPS (be sure to replace the server name with your actual domain name):
Save the file, and exit the configuration file. Check the configuration for syntax errors:
Step 7 — Reloading NginxThat's it for all the Nginx configuration changes. Since we checked for syntax errors with each change, you should be ready to restart Nginx and test your changes. To summarize, ignoring commented out lines, your configuration file should now look similar to this:
To apply the changes, restart the Nginx server.
|
|