Https nginx: Difference between revisions

From VoIPmonitor.org
Jump to navigation Jump to search
(Created page with "== Nginx == === Generating Self-Signed SSL Certificates === First, we will generate a new private key and a self-signed certificate. Navigate to the SSL directory and create...")
 
No edit summary
 
Line 3: Line 3:
=== Generating Self-Signed SSL Certificates ===
=== Generating Self-Signed SSL Certificates ===


First, we will generate a new private key and a self-signed certificate. Navigate to the SSL directory and create the certificates using these commands:
First, generate a new private key and a self-signed certificate. Navigate to the SSL directory and create the certificates using these commands:


<pre>
<pre>
Line 9: Line 9:
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
</pre>
</pre>
Answer the series of questions appropriately to generate the certificate.
Answer the series of questions to generate the certificate.


=== Configuring Nginx to Use SSL ===
=== Configuring Nginx to Use SSL ===


Now that we have our self-signed certificate and private key, we can tell Nginx how to use these to secure traffic.
Now, configure Nginx to use the self-signed certificate and private key.


Open the default Nginx server block file.
Open the default Nginx server block file:


<pre>
<pre>
sudo nano /etc/nginx/sites-available/default
sudo nano /etc/nginx/sites-available/default
</pre>
</pre>
Find the section that begins with '''server''' and update it to include the '''ssl''' directive and point to your SSL certificate and private key like so:
Find the section that begins with '''server''' and update it to include the '''ssl''' directive and point to your SSL certificate and private key. Also, set up a redirection from HTTP to HTTPS:


<pre>
<pre>
Line 26: Line 26:
     listen 80 default_server;
     listen 80 default_server;
     listen [::]:80 default_server;
     listen [::]:80 default_server;
    server_name your_domain.com;
    return 301 https://$host$request_uri;
}


server {
     listen 443 ssl default_server;
     listen 443 ssl default_server;
     listen [::]:443 ssl default_server;
     listen [::]:443 ssl default_server;
     ssl_certificate /etc/nginx/ssl/nginx.crt;
     ssl_certificate /etc/nginx/ssl/nginx.crt;
     ssl_certificate_key /etc/nginx/ssl/nginx.key;
     ssl_certificate_key /etc/nginx/ssl/nginx.key;
     . . .
     . . .
}
}
Line 46: Line 48:
sudo systemctl restart nginx
sudo systemctl restart nginx
</pre>
</pre>
Now, you should be able to access your site via https://. Remember that since this is a self-signed certificate, browsers will generally show a warning since they cannot validate the certificate.
Now, you should be able to access your site via https://. Since this is a self-signed certificate, browsers will generally show a warning because they cannot validate the certificate. However, all traffic should now be redirected to HTTPS.

Latest revision as of 14:24, 7 July 2023

Nginx

Generating Self-Signed SSL Certificates

First, generate a new private key and a self-signed certificate. Navigate to the SSL directory and create the certificates using these commands:

sudo mkdir /etc/nginx/ssl
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt

Answer the series of questions to generate the certificate.

Configuring Nginx to Use SSL

Now, configure Nginx to use the self-signed certificate and private key.

Open the default Nginx server block file:

sudo nano /etc/nginx/sites-available/default

Find the section that begins with server and update it to include the ssl directive and point to your SSL certificate and private key. Also, set up a redirection from HTTP to HTTPS:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name your_domain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;
    . . .
}

Save and exit the file.

Restarting Nginx

Finally, test the configuration and restart Nginx to apply our changes:

sudo nginx -t
sudo systemctl restart nginx

Now, you should be able to access your site via https://. Since this is a self-signed certificate, browsers will generally show a warning because they cannot validate the certificate. However, all traffic should now be redirected to HTTPS.