Stori.
Setup SSL
5 min read

Setup SSL

API

root@localhost:~# sudo snap install core
root@localhost:~# sudo snap refresh core
root@localhost:~# sudo snap install --classic certbot
root@localhost:~# sudo ln -s /snap/bin/certbot /usr/bin/certbot

.

sudo certbot --nginx -d storage.midu.vn

sudo nano /etc/nginx/sites-available/storage.midu.vn

sudo ln -s /etc/nginx/sites-available/storage.midu.vn /etc/nginx/sites-enabled/

server {

  listen 80;

  server_name storage.midu.vn;

  charset utf-8;

	access_log /var/log/nginx/storage.midu.vn.access.log;
  error_log /var/log/nginx/storage.midu.vn.error.log;

  location / {
    proxy_pass http://localhost:9000;
    proxy_read_timeout 300;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_cache_bypass $http_upgrade;
  }
}
server {

  listen 443 ssl http2;
  listen [::]:443 ssl http2;

  server_name storage.midu.vn;

  charset utf-8;
  
  access_log /var/log/nginx/storage.midu.vn.access.log;
  error_log /var/log/nginx/storage.midu.vn.error.log;

  ssl_certificate /etc/letsencrypt/live/storage.midu.vn/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/storage.midu.vn/privkey.pem;

  location / {
    proxy_pass http://localhost:9000;
    proxy_read_timeout 300;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_cache_bypass $http_upgrade;
  }
}

Bây giờ hãy chạy lệnh dưới đây để kiểm tra xem ta đã cấu hình đúng hay không.

sudo nginx -t

Nếu mọi việc thành công thì bạn sẽ nhận được kết quả như sau:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Hãy restart lại Nginx bằng lệnh dưới đây để áp dụng các thay đổi.

sudo systemctl restart nginx

Fix config

nginx: [warn] conflicting server name

rm /etc/nginx/sites-enabled/default

/

Share this article

Share:
Read Next Article