Serving HTTPS traffic with nginx
Now that we've got the SSL certificate in our server, we can tell NGINX to use it. All we have to do is modify the configuration file. Open the application's configuration file. From the course, this is on /etc/nginx/sites-enabled/items-rest.conf
.
Find the following lines:
And we are going to change then to this:
We want to change the listen
because http
traffic accesses our server on port 80, but https
traffic uses port 443.
The other fields tell nginx to use SSL, and the locations of the certificate and key.
HTTPS rewrites
Sometimes, users will access the http
version of our site—accidentally or because they have it bookmarked. We can tell nginx to redirect users to the https
version of the site when this happens.
At the bottom of our config file, add the following lines:
What that does is listen on port 80, and rewrite the URL to the https
protocol. The $1
appends at the end of the domain name whatever users requested. For example, $1
could be /stores
.
Restart nginx
Finally, reload and restart nginx to apply the changes.
Verify it all works
Access your https
site or API using Postman. It should all work!
Last updated