我的nginx中的配置
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name 144.48.66.*** ; # 你的服务器公网ip 或者 域名
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static {
alias /usr/local/test_project/static/; # static path
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include uwsgi_params; # the uwsgi_params file you installed
}
# 这里我给图片和JS, CSS 添加了过期时间,路径写你的静态文件路径
location /usr/local/test_project/static/.*\.(jpg|jpeg|png|gif|icon)$
{
expires 30d;
}
location /usr/local/test_project/static/.*\.(js|css)?$
{
expires 10d;
}
}
主要就是expires的值设置, 我这里设置的是30天和10天,因为更改的比较少,设置完之后的,访问一次网页后,对应的静态文件就存到了浏览器中,后面再次请求状态码就是304,也就是从缓存中获取文件
设置完后重启nginx生效, 建议平滑重启nginx (不会影响网站)
nginx 平滑重启
nginx -s reload