Skip to content

Commit

Permalink
feature:nginx增加缓存功能
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyumc committed Sep 24, 2024
1 parent dd48325 commit 1d2f8b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ media/post_images
_1. 允许从公网访问管理后台:出于安全原因,管理后台只允许本地访问,如果想从公网访问便于管理,替换 `docker-compose.yml ``backend `容器 `- DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1,backend,SERVER_NAME`
中的`SERVER_NAME`为你的服务器域名_

_2. 配置nginx:nginx可以让网站从80端口直接访问,并加快网站请求速度,AppleBlog已经写好了nginx的配置文件,详细请查看nginx的文件夹,需要运行请在配置文件里增加你自己的服务器地址,里面有一个docker-compose.yml,可以直接运行`docker-compose up`运行nginx_
_2. 配置nginx(推荐):nginx可以让网站从80端口直接访问,并加快网站请求速度,AppleBlog已经写好了nginx的配置文件,并且对于常见的压缩请求,图片缓存等做了适配,可以让网站打开速度快几倍,推荐部署

详细请查看nginx的文件夹,需要运行请在配置文件里增加你自己的服务器地址,里面有一个docker-compose.yml,可以直接运行`docker-compose up`运行nginx_

### 从源码部署
当然,也可以从源码部署
Expand Down
25 changes: 23 additions & 2 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ http {
gzip_min_length 1000;
gzip_disable "msie6";

# 请填你自己的服务器地址
# !!!请填你自己的服务器地址
server {
listen 80;
server_name ;
Expand All @@ -34,11 +34,32 @@ http {
}
}

# 请填你自己的服务器地址
# !!!请填你自己的服务器地址
server {
listen 80;
server_name ;

# 针对 Django 提供的 API 图片资源,添加缓存配置
location /api/img/post/ {
proxy_pass http://127.0.0.1:8000;
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;

# 设置缓存响应头,缓存30天
proxy_cache_valid 200 30d;
add_header Cache-Control "public, max-age=2592000";
}

# 针对静态资源,如图片、CSS、JS等,设置缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf)$ {
root /usr/share/nginx/html; # 绑定的静态文件路径
expires 30d; # 设置30天缓存
add_header Cache-Control "public, max-age=2592000";
}

# 代理其他请求到后台服务
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
Expand Down

0 comments on commit 1d2f8b3

Please sign in to comment.