简体   繁体   English

DJango + nginx + gunicorn:如何开始缓存,哪种方式更标准?

[英]DJango + nginx + gunicorn: how to start caching and which way is more standard?

I deployed my Django project using Gunicorn, but now can't use Nginx caching.我使用 Gunicorn 部署了我的 Django 项目,但现在不能使用 Nginx 缓存。

How to start caching on a project that use Gunicorn and which caching method is standard for Django.如何在使用 Gunicorn 的项目上开始缓存,以及 Django 的标准缓存方法。

I try to use Ngnix caching but it won't work.我尝试使用 Ngnix 缓存,但它不起作用。

Here is an example of my Ngnix conf and Caching Conf这是我的 Ngnix conf 和 Caching Conf 的示例

Ngnix 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; } }

Cache Config缓存配置

/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 Socket独角兽插座

/etc/systemd/system/gunicorn.socket

 [Unit] Description=gunicorn socket [Socket] ListenStream=/run/gunicorn.sock [Install] WantedBy=sockets.target

Gunicorn Service独角兽服务

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

I am also using gunicorn to host a django webserver on Nginx, and I did not run into this problem.我还使用 gunicorn 在 Nginx 上托管 django 网络服务器,我没有遇到这个问题。 I don't believe it should matter that you are hosting with gunicorn.我认为您使用 gunicorn 托管并不重要。 As long as you configure caching for the directory that contains all of your static files it should work.只要您为包含所有 static 文件的目录配置缓存,它就应该可以工作。

Here's an example where the static files are cached for a year:下面是 static 文件缓存一年的示例:

    location /static {
        root [location of /static folder];
        expires 1y;
        access_log off;
        add_header Cache-Control "public";
    }

If this doesn't solve the problem please add your cache settings for Nginx to your question如果这不能解决问题,请将 Nginx 的缓存设置添加到您的问题中

If you are trying to cache django views or templates, look into django-cahceops or a similar package如果您尝试缓存 django 视图或模板,请查看django-cahceops或类似的 package

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM