如何在 nginx proxy_pass 位置设置资产缓存

如何在 nginx proxy_pass 位置设置资产缓存

我有一个在我的所有网站中都使用的代码片段,它在静态网站上运行良好,但在网站上却运行不佳proxy_pass

我想知道我做错了什么,以及如何安全地包含我的代码片段而不使资产变成 404?

这是我的服务器块

server {
  server_name jenkins.fabrikam.com;
  include /etc/nginx/location.conf; # All assets are 404 with this
  location / {
    proxy_pass http://localhost:8080;
    include /etc/nginx/proxy_params;
  }
}

这里是location.conf

# cache.appcache, your document html and data
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
  expires -1;
}

# Feed
location ~* \.(?:rss|atom)$ {
  expires 1h;
  add_header Cache-Control "public";
}

# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
  expires 1M;
  access_log off;
  add_header Cache-Control "public";
}

# CSS and Javascript
location ~* \.(?:css|js)$ {
  expires 1y;
  access_log off;
  add_header Cache-Control "public";
}

# Prevent clients from accessing hidden files (starting with a dot)
# This is particularly important if you store .htpasswd files in the site hierarchy
# Access to `/.well-known/` is allowed.
# https://www.mnot.net/blog/2010/04/07/well-known
# https://tools.ietf.org/html/rfc5785
location ~* /\.(?!well-known\/) {
  deny all;
}

# Prevent clients from accessing to backup/config/source files
location ~* (?:\.(?:bak|conf|dist|fla|in[ci]|log|psd|sh|sql|sw[op])|~)$ {
  deny all;
}

# Remove useless acccess logs from these files
location = /favicon.ico {
  log_not_found off;
  access_log off;
}

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

答案1

使用 proxy_passing 时,你必须以不同的方式思考缓存。nginx文档有一个专门用于代理缓存的部分。

因此最基本的例子如下所示:

http {
    proxy_cache_path /data/nginx/cache keys_zone=one:10m;
    server {
        proxy_cache on;
        location / {
            proxy_pass http://localhost:8000;
        }
    }
}

相关内容