Skip to content

Instantly share code, notes, and snippets.

@anhtran
Last active July 22, 2022 03:45
Show Gist options
  • Save anhtran/d8e7fca00e28c5a505c99e551b0f637f to your computer and use it in GitHub Desktop.
Save anhtran/d8e7fca00e28c5a505c99e551b0f637f to your computer and use it in GitHub Desktop.
Cấu hình mẫu nginx dành cho các website viết bằng Django - Sample nginx configurations for Django website
# === CẤU HÌNH MẪU NGINX DÀNH CHO 1 WEBSITE VIẾT BẰNG DJANGO ===
# Ở đây chúng ta sẽ cài đặt cho 3 domain: mysite.com | static.mysite.com | media.mysite.com
# Ngoài ra, nguyên tắc của tôi là mỗi 1 website sẽ có 1 linux user riêng với những quyền riêng tránh can thiệp vào các user khác.
# INSTRUCTION
# 1. Thay thế `mysite` với tên của domain website;
# 2. Điều chỉnh port 9999 thành port mà bạn sử dụng tại máy chủ WSGI hoặc ASGI.
# 3. Điều chỉnh MEDIA_ROOT và STATIC_ROOT lại cho phù hợp với cấu hình của nginx ở đây.
upstream mysite_django {
server 127.0.0.1:9999;
}
server {
listen 80;
server_name mysite.com;
root /home/mysite/mysite.com;
index index.html index.htm;
access_log off;
error_log /home/mysite/mysite.com/nginx-errors.log;
recursive_error_pages on;
server_tokens off;
# tương đương với dung lượng tải lên tối đa của CloudFlare ở gói miễn phí
client_max_body_size 100M;
sendfile on;
include mime.types;
default_type application/octet-stream;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon application/octet-stream;
gzip on;
location / {
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_read_timeout 60s;
proxy_send_timeout 180s;
proxy_pass http://mysite_django;
}
location ~ /.well-known {
allow all;
}
location /robots.txt {
alias /home/mysite/mysite.com/robots.txt;
expires 30d;
access_log off;
}
}
server {
listen 80;
server_name static.mysite.com;
access_log off;
error_log off;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon application/octet-stream;
gzip on;
root /home/mysite/mysite.com/static;
index index.html index.htm;
location / {
alias /home/mysite/mysite.com/static;
expires 365d;
add_header Vary Accept-Encoding;
access_log off;
# cho phép mọi domain có thể dùng file tĩnh của domain này
# nếu muốn giới hạn lại thì có thể thêm domain cụ thể, ví dụ:
# add_header 'Access-Control-Allow-Origin' 'https://developer.mozilla.org';
# Nguồn tham khảo: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
}
server {
listen 80;
server_name media.mysite.com;
access_log off;
error_log off;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon application/octet-stream;
gzip on;
root /home/mysite/mysite.com/media;
index index.html index.htm;
location / {
alias /home/mysite/mysite.com/media;
expires 365d;
add_header Vary Accept-Encoding;
access_log off;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment