How to configure nginx to serve static site over https?

by haylee.mertz , in category: Third Party Scripts , a month ago

How to configure nginx to serve static site over https?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , a month ago

@haylee.mertz 

To configure Nginx to serve a static site over HTTPS, you will need to have a valid SSL certificate for your domain. Here are the basic steps to configure Nginx for serving a static site over HTTPS:

  1. Install Nginx on your server if it is not already installed:
1
2
sudo apt-get update
sudo apt-get install nginx


  1. Obtain an SSL certificate for your domain. You can either purchase an SSL certificate from a trusted CA or use a free certificate from Let's Encrypt.
  2. Configure your Nginx server block to serve your static site over HTTPS. Open the Nginx configuration file for your site:
1
sudo nano /etc/nginx/sites-available/your_site


  1. Update the server block configuration to listen on port 443 for HTTPS requests and specify the SSL certificate and key file paths:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
server {
    listen 443 ssl;
    server_name your_domain.com;

    ssl_certificate /path/to/your_ssl_certificate.crt;
    ssl_certificate_key /path/to/your_ssl_certificate_key.key;

    root /path/to/your/static/site;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}


  1. Test your Nginx configuration to check for syntax errors:
1
sudo nginx -t


  1. If no errors are found, reload Nginx to apply the changes:
1
sudo systemctl reload nginx


  1. Update your DNS settings to point your domain to your server's IP address.


Your static site should now be configured to serve over HTTPS using Nginx. You can test your site by entering your domain in a web browser to verify that it loads securely.