- Setup cho docker compose bao gồm những service sau
services:
card_nginx:
container_name: card_nginx
image: nginx:1.25.3
restart: always
ports:
- 80:80
- 443:443
volumes:
- ./config/config.d:/etc/nginx/conf.d/
- ./config/nginx.conf:/etc/nginx/nginx.conf
- ./data/nginx/log/:/var/log/nginx
- ./data/nginx/sites/:/usr/share/nginx/html
- ./certbot/conf/:/etc/nginx/ssl/:ro
- ./certbot/www/:/var/www/certbot/:ro
certbot:
container_name: card_certbot
image: certbot/certbot:latest
volumes:
- ./certbot/www/:/var/www/certbot/:rw
- ./certbot/conf/:/etc/letsencrypt/:rw
- File
nginx.conf cấu hình mẫu như sau:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main escape=json '$remote_addr - $host - $remote_user [$time_local] "$request" '
'Status=$status Bytes-Sent=$body_bytes_sent Body="$request_body" Referer="$http_referer" '
'User-Agent="$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
client_max_body_size 100M;
gzip on; # enable gzip
gzip_http_version 1.1; # turn on gzip for http 1.1 and higher
gzip_disable "msie6"; # IE 6 had issues with gzip
gzip_comp_level 5; # inc compresion level, and CPU usage
gzip_min_length 100; # minimal weight to gzip file
gzip_proxied any; # enable gzip for proxied requests (e.g. CDN)
gzip_buffers 16 8k; # compression buffers (if we exceed this value, disk will be used instead of RAM)
gzip_vary on; # add header Vary Accept-Encoding (more on that in Caching section)
gzip_types text/plain;
gzip_types text/css;
gzip_types application/javascript;
gzip_types application/json;
gzip_types application/vnd.ms-fontobject;
gzip_types application/x-font-ttf;
gzip_types font/opentype;
gzip_types image/svg+xml;
gzip_types image/x-icon;
include /etc/nginx/conf.d/*.conf;
}
- Để xin SSL thì cần cấu hình cổng 80 trỏ vào folder
acme-challenge.
- Lắng nghe cổng 80, khi xin SSL thì các yêu cầu xác thực luôn đi vào cổng 80 và folder “/.well-known/acme-challenge/”.
- Các điều hướng khác thì sẽ được điều hướng sang cổng 443.
// config/config.d/cert.conf
server{
listen 80;
listen [::]:80;
server_name abc.com;
server_tokens off;
location /.well-known/acme-challenge/ {
allow all;
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
- Các config khác cho các site thì cấu hình như sau. Cấu hình bên dưới để lắng nghe các yêu cầu ở cổng 443
- Trả về cerificate mỗi khi thiết lập kết nối bảo mật (https).
- Đường dẫn “/” là để trả về các file tĩnh được build từ react app hoặc mã html.
- Khối “/api” là khi đường dẫn bắt đầu từ api thì sẽ điều hướng request sang service xử lý api, sử dụng từ khoá proxy_pass để điều hướng.
server{
listen 443;
server_name site.com;
ssl_certificate /etc/nginx/ssl/live/site/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/live/site/privkey.pem;
// Đường dẫn này để trả về file
location / {
root /usr/share/nginx/html/clipboard-app;
try_files $uri $uri/ /index.html;
}
// Đường dẫn này để proxy pass sang một service khác
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://172.21.0.22:3000$request_uri;
proxy_hide_header 'X-Powered-By';
add_header 'X-Powered-By' 'Do Manh Tai Developer';
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
Command chạy xin SSL
- Command chạy khi chưa có SSL. Sau khi chạy lệnh này kết quả sẽ có được 1 file ssl_certificate (fullchain.pem) và ssl_certificate_key (privkey.pem)
docker-compose run --rm certbot certonly \
-v \
--webroot \
--webroot-path=/var/www/certbot \
--email manhtai831@gmail.com \
--agree-tos \
--no-eff-email \
-d site1.io.vn \
-d site2.io.vn \
- Sau mỗi 3 tháng, SSL mà Let Encrypt sẽ hết hạn và cần làm mới SSL thì chạy lệnh sau. Lệnh bên dưới sẽ làm mới cert bạn sẽ không cần phải đi kiểm tra lại các file certificate ở trong các file conf nữa.
#!/bin/sh
/usr/local/bin/docker compose run --rm certbot \
-v certonly \
--non-interactive \
--agree-tos \
--force-renewal \
--webroot \
--webroot-path=/var/www/certbot \
--cert-name napthe247.io.vn-0001 \
-d site1.io.vn \
-d site2.io.vn \
- Thêm command này vào để chạy lập lịch tự động làm mới certificate mỗi 3 tháng như sau:
crontab -e
- Thêm 1 dòng mới.
- Giải thích: Vào lúc 5h sáng ngày 1 mỗi 3 tháng thì chạy script làm mới cert
0 5 1 */3 * path/to/bin/auto-renew-cert-job