GNU/Linux >> Linux Esercitazione >  >> Ubuntu

Come proteggere Nginx con Letsencrypt su Ubuntu 20.04

È stato sviluppato dall'Internet Security Research Group (ISRG) e considerato affidabile da tutti i principali browser. Viene utilizzato per automatizzare il processo di creazione, convalida, firma, implementazione e rinnovo dei certificati per siti Web sicuri.

Il certificato è valido per soli 90 giorni, quindi dovrai rinnovarlo manualmente o o configurare il sistema di rinnovo automatico,

Let's encrypt supporta l'emissione di certificazioni automatizzate per Apache, Nginx, Plex e HAproxy. Tratteremo nginx in questa guida.

Contenuti correlati

  • Come proteggere Nginx con Letsencrypt su Rocky Linux/Centos 8
  • Come installare e configurare Nginx, WordPress e Mysql 8 in Rocky Linux/Centos 8
  • Come installare Nginx e configurare l'host virtuale in Ubuntu 20.04

Prerequisiti:

  • Un server Ubuntu 20.04 con accesso a Internet e IP pubblico
  • Un nome di dominio valido con DNS puntato al server
  • Accesso root o accesso sudo al server

Installazione del client Let's Encrypt di Certbot

Accedi al server utilizzando ssh [email protected] -p port :

ssh [email protected]

Aggiorna tutti i tuoi pacchetti alle ultime versioni disponibili.

sudo apt update
sudo apt upgrade -y

Installa Nginx

sudo apt install -y nginx

Avvia e abilita nginx

systemctl start nginx
systemctl enable nginx

Creiamo nginx config per site1.citizix.com:

Apri il file di configurazione con il tuo editor di testo:

sudo vim /etc/nginx/conf.d/site1.conf

Aggiungi questo contenuto:

server {
    listen 80;
    server_tokens off;
    client_max_body_size 10M;
    server_name site1.citizix.com;

    access_log /var/log/nginx/site1.citizix.com/access.log;
    error_log /var/log/nginx/site1.citizix.com/error.log;
    ignore_invalid_headers off;

    ## Deny illegal Host headers
    if ($host !~* ^(site1.citizix.com)$ ) {
        return 444;
    }

    root /var/www/site1.citizix.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header   Host $host;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Scheme $scheme;
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

}

Installa il client Certbot

Certbot è uno strumento da riga di comando utilizzato per semplificare il processo di ottenimento e rinnovo dei certificati SSL Let's Encrypt per il tuo sito web. Usa questo comando per installarlo insieme alle dipendenze Python:

sudo apt install certbot python3-certbot-nginx

se hai ufw firewall installato e abilitato, apri il traffico http e https dal web:

ufw allow 80
ufw allow 443
ufw reload

Ottenere un certificato

Ferma nginx:

sudo systemctl stop nginx
sudo certbot --nginx --non-interactive --agree-tos --email [email protected] -d site1.citizix.com

Uscita

# sudo certbot --nginx --non-interactive --agree-tos --email [email protected] -d site1.citizix.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Account registered.
Requesting a certificate for site1.citizix.com
Performing the following challenges:
http-01 challenge for site1.citizix.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/site1.citizix.com.conf
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/site1.citizix.com.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled
https://site1.citizix.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/site1.citizix.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/site1.citizix.com/privkey.pem
   Your certificate will expire on 2021-11-05. To obtain a new or
   tweaked version of this certificate in the future, simply run
   certbot again with the "certonly" option. To non-interactively
   renew *all* of your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

Avvia nginx:

sudo systemctl start nginx

Aggiorna la configurazione di Nginx per reindirizzare il traffico http a https

server {
    server_tokens off;
    client_max_body_size 10M;
    server_name site1.citizix.com;

    access_log /var/log/nginx/site1.citizix.com/access.log;
    error_log /var/log/nginx/site1.citizix.com/error.log;
    ignore_invalid_headers off;

    ## Deny illegal Host headers
    if ($host !~* ^(site1.citizix.com)$ ) {
        return 444;
    }

    root /var/www/site1.citizix.com;

    location / {
        proxy_pass http://127.0.0.1:8096;
        proxy_set_header   Host $host;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Scheme $scheme;
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/site1.citizix.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/site1.citizix.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = site1.citizix.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name site1.citizix.com;
    return 404; # managed by Certbot
}

Ubuntu
  1. Come proteggere Nginx con Lets Encrypt su Ubuntu 20.04 / 18.04

  2. Come installare WordPress con Nginx su Ubuntu 18.04

  3. Proteggi Nginx con Let's Encrypt su Ubuntu 18.04 - Come farlo?

  4. Come proteggere Nginx con Letsencrypt su Rocky Linux/Centos 8

  5. Come proteggere Nginx con Let's Encrypt su Ubuntu 20.04

Come installare Redmine 3.2 con Nginx su Ubuntu 16.04

Come installare OSClass con Nginx su Ubuntu 20.04

Come installare Nginx con ModSecurity su Ubuntu 15.04

Come installare Nextcloud 13 su Ubuntu 16.04 con Nginx

Come installare LetsEncrypt con Nginx su Ubuntu 15.04

Come proteggere Nginx con Let's Encrypt su Ubuntu 20.04