【问题标题】:Trying to add another location directive in an nginx server with proxy cache尝试在具有代理缓存的 nginx 服务器中添加另一个位置指令
【发布时间】:2018-04-19 08:33:27
【问题描述】:

我有一个包含几个页面和图像的网站,并且我已经设置了一个 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

我没有收到任何错误。

【问题讨论】:

    标签: nginx location directive


    【解决方案1】:

    新的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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 2020-09-29
      • 2018-11-05
      • 1970-01-01
      相关资源
      最近更新 更多