Django 关闭Debug后使用Nginx做静态文件的访问

时间:2021-11-30 06:56:46

Django 关闭Debug后使用Nginx做静态文件的访问

关闭Django 的Debug参数

1 . 修改settings.py配置文件

DEBUG = False

2 . settings.py添加静态文件目录的指向,以及BASE_DIR的路径

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)

3 . 项目的同级目录创建/static/目录

Nginx配置

1 . 修改nginx.conf配置文件通过upstream将请求抛给 8000端口

worker_processes  2;
events {
worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
tcp_nopush on;
tcp_nodelay on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
client_max_body_size 10m;
client_header_timeout 15;
client_body_timeout 15;
send_timeout 15;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/css text/xml application/javascript;
gzip_vary on; #####################################
upstream python-handler {
#
server 127.0.0.1:8000;
} server {
listen 8080;
server_name 192.8.21.100;
charset UTF-8;
access_log /var/log/nginx/django_pro01_access.log;
error_log /var/log/nginx/django_pro01_error.log; client_max_body_size 75M; location / {
proxy_pass http://python-handler;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /Django/aws/static;
}
}
}
#######################################