大网数据广告活动
服务器运维问答

How to set asset caching in nginx proxy_pass location

· 52CE 我爱测

I have a snippet that I use in all of my website which is working fine on static websites, however not so much on proxy_pass websites. I wonder what am I doing wrong and how can I safely include my snippet without making the asse…

问题原文

原帖链接 · sed · CC BY-SA 3.0

I have a snippet that I use in all of my website which is working fine on static websites, however not so much on proxy_pass websites.

I wonder what am I doing wrong and how can I safely include my snippet without making the assets 404?

Here is my server block

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

Here is the 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;
}

社区高分解答

原帖链接 · martyglaubitz · CC BY-SA 3.0

When using proxy_passing, you have to think caching differently. The nginx documentation has a section dedicated to proxy caching.

Thus the most basic example looks like this:

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-10-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。