DEV Community

Juliana Dias
Juliana Dias

Posted on

ChatGPT: basic example of an Nginx configuration file using an auto-signed SSL certificate

This article was created by ChatGPT

Here is a basic example of an Nginx configuration file using an auto-signed SSL certificate:

Copy code
server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;

    location / {
        # your application code here
    }
}
Enter fullscreen mode Exit fullscreen mode

Here are the steps to generate the auto-signed SSL certificate and key files:

Generate the SSL certificate and key files using OpenSSL:

$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout /etc/ssl/private/nginx-selfsigned.key \
  -out /etc/ssl/certs/nginx-selfsigned.crt
Enter fullscreen mode Exit fullscreen mode

This command will prompt you for some information to include in the certificate, such as the Common Name (CN) which should be set to your domain name (e.g. example.com).

Verify that the certificate and key files were generated:

$ ls -l /etc/ssl/private/nginx-selfsigned.key /etc/ssl/certs/nginx-selfsigned.crt
Enter fullscreen mode Exit fullscreen mode

You should see output similar to:

-rw-r--r-- 1 root root 1091 Feb 24 15:44 /etc/ssl/certs/nginx-selfsigned.crt
-rw-r--r-- 1 root root 1675 Feb 24 15:44 /etc/ssl/private/nginx-selfsigned.key
Enter fullscreen mode Exit fullscreen mode

Copy the configuration file above into your Nginx server block, replacing the example.com server name with your own domain name.

Reload the Nginx configuration:

$ sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

That's it! Your Nginx server should now be configured to use the auto-signed SSL certificate.

Top comments (0)