简体   繁体   English

Nginx 不存储缓存

[英]Nginx isn't storing cache

I'm trying to allow nginx caching in the simplest form.我试图以最简单的形式允许nginx 缓存 But for some reason it's not working.但由于某种原因,它不起作用。 I'm currently using nginx with gunicorn and flask on an ec2 instance.我目前在 ec2 实例上使用 nginx 和 gunicorn 和 flask 。

This is my /etc/nginx/nginx.conf file:这是我的/etc/nginx/nginx.conf文件:

user nginx;

...

proxy_cache_path /var/cache/nginx keys_zone=mycache:10m;
proxy_cache_methods GET HEAD POST;

server {
    listen 80;

    access_log  /var/log/nginx/agori.access.log  main;
    error_log /var/log/nginx/agori.error.log;

    location / {
        proxy_set_header Host $http_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;
        proxy_cache mycache;
        proxy_cache_valid any 48h;
        proxy_buffering on;
        proxy_pass http://unix:/home/ec2-user/src/project.sock;
    }
}

when check the /var/cache/nginx folder, it's empty.检查/var/cache/nginx文件夹时,它是空的。 These are the folders permissions:这些是文件夹权限:

drwxrwxrwx  2 nginx root    6 May 13 14:03 nginx

This is the request and respond headers:这是请求和响应标头:

在此处输入图像描述

PS: This is on mobile(ios) PS:这是手机(ios)

It sounds to me that something in your Nginx config might not be correct (syntax error or not supported by your Nginx version).在我看来,您的 Nginx 配置中的某些内容可能不正确(语法错误或您的 Nginx 版本不支持)。 In most of the case I encountered so far that was the case for me.到目前为止,我遇到的大多数情况都是如此。

You probably know Nginx' reverse proxy example which features the following configuration你可能知道Nginx 的反向代理示例,它具有以下配置

http { proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g; server { location / { proxy_pass http://1.2.3.4; proxy_set_header Host $host; proxy_buffering on; proxy_cache STATIC; proxy_cache_valid 200 1d; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; } } }

I tried to compare that with your configuration file and I my debugging approach would be:我试图将其与您的配置文件进行比较,我的调试方法是:

  1. Does nginx log your requests in access_log ? nginx 是否将您的请求记录在access_log中?
  2. Try to remove whether the example configuration file works after minimal modifications.尝试删除示例配置文件在最小修改后是否有效。
  3. Replace the any with a 200 for a start and see whether that works.any替换为200作为开始,看看是否有效。
  4. If that works, put in step by step all additional config lines like the proxy_cache_methods line.如果可行,请逐步添加所有其他配置行,例如proxy_cache_methods行。

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

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