Nginx缓存控制头不工作(在日志上获取404)

Nginx缓存控制头不工作(在日志上获取404),nginx,caching,static,cache-control,Nginx,Caching,Static,Cache Control,我正在尝试设置nginx来缓存静态文件,比如图像、css和js 这是我的密码 server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { root /var/www/site; index index.htm

我正在尝试设置nginx来缓存静态文件,比如图像、css和js

这是我的密码

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /var/www/site;
        index  index.html index.htm;
  }

        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
                expires max;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}
}
当我尝试使用它时,我在所有文件上得到404,当我删除位置~*。。。我可以完美地检索所有文件。我的文件在
/var/www/site/images/*/*.jpg
中。我在这里遗漏了什么?

问题在于

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
        expires max;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
它应该设置根路径

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
            root /var/directory/...
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

我对资产使用url重写,为什么我需要在此处指定根路径?@user1658296您必须在服务器{}内的位置外部全局指定根路径,或者必须为指定的每个位置指定根路径。