【发布时间】:2022-01-27 14:47:20
【问题描述】:
我使用 Gunicorn 部署了我的 Django 项目,但现在无法使用 Nginx 缓存。
如何在使用 Gunicorn 的项目上开始缓存以及哪种缓存方法是 Django 的标准。
我尝试使用 Ngnix 缓存,但它不起作用。
这是我的 Ngnix conf 和 Caching Conf 的示例
Ngnix 会议
/etc/nginx/sites-available/my-project-config
upstream daphne_server {
server unix:/var/www/my-project-config/env/run/daphne.sock fail_timeout=0;
}
upstream gunicorn_server {
server unix:/var/www/my-project-config/env/run/gunicorn.sock fail_timeout=0;
}
server {
listen 8000;
server_name my_server_ip_address or domain name;
client_max_body_size 100M;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/my_project_dir/public_html;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location /ws/ {
proxy_pass http://localhost:8001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
}
缓存配置
/etc/nginx/sites-available/my-project-cache-config
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=custom_cache:10m inactive=60m;
upstream origin_server {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name _;
location / {
include proxy_params;
proxy_pass http://origin_server;
proxy_cache custom_cache;
proxy_cache_valid any 10m;
add_header X-Proxy-Cache $upstream_cache_status;
}
}
Gunicorn 插槽
/etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
Gunicorn 服务
sudo nano /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/my_project_dir
ExecStart=/home/ubuntu/my_project_dir/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
my_project.wsgi:application
[Install]
WantedBy=multi-user.target
【问题讨论】:
-
你能举一个你的缓存配置的例子吗?
标签: django nginx caching gunicorn django-deployment