尝试在具有代理缓存的 nginx 服务器中添加另一个位置指令

Trying to add another location directive in an nginx server with proxy cache

我有一个包含几个页面和图像的网站,我设置了一个 nginx 服务器来处理该网站。当我添加一个位置指令来处理图像时,整个网站都没有显示出来,看起来很破烂。

当我的 nginx 文件底部看起来像这样时,网站看起来很完美:

    location = /favicon.ico {
        access_log off;
        log_not_found off;
    }

    location = /robots.txt {
        allow all;
        access_log off;
        log_not_found off;
    }

    location ~ /\. {
        deny all;
    }

    #location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    #   proxy_cache sinuscache;
    #   add_header Pragma public;
    #   add_header Cache-Control "public";
    #   expires 1d;
    #   log_not_found off;
    #}      

    location ~* (\.bak|\.off|\.config|\.exe|\.sql|\.fla|\.psd|\.ini|\.log|\.sh|\.inc|\.swp|\.dist)$ {
        deny all;
        add_header Pragma public;
        add_header Cache-Control "public";
        expires -1d;
        access_log off;
    }

   location / {
      include              /etc/nginx/sites-settings/denyips.conf;
      proxy_pass           http://127.0.0.1:9099;
      proxy_set_header     X-Forwarded-For $remote_addr;
      proxy_cache          sinuscache;
   }
}

当我取消评论时

    #location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    #   proxy_cache sinuscache;
    #   add_header Pragma public;
    #   add_header Cache-Control "public";
    #   expires 1d;
    #   log_not_found off;
    #}  

网站出现故障。如果我做

service nginx configtest

我没有收到任何错误。

新的 location 块不完整 - 您至少需要包含 proxy_pass http://127.0.0.1:9099; 以及 location / 块中的许多其他语句。参见 how nginx processes a request

一些语句可以放在 server 块中以避免复制。例如:

proxy_set_header     X-Forwarded-For $remote_addr;
proxy_cache          sinuscache;

location / {
    include              /etc/nginx/sites-settings/denyips.conf;
    proxy_pass           http://127.0.0.1:9099;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    include              /etc/nginx/sites-settings/denyips.conf;
    proxy_pass           http://127.0.0.1:9099;
    add_header Pragma public;
    add_header Cache-Control "public";
    expires 1d;
    log_not_found off;
}