nginx过期标头和反向代理无法正常工作

Kir*_*ril 2 reverse-proxy nginx expires-header

我试图在nginx(0.7.67)上为静态文件配置Expires头.静态文件由Golang反向代理提供:

location /rev/ {
  proxy_pass http://localhost:8910/;
  proxy_redirect off;
  proxy_set_header  Host               $host;
  proxy_set_header  X-Real-IP          $remote_addr;
  proxy_set_header  X-Forwarded-For    $proxy_add_x_forwarded_for;
  proxy_set_header  X-Forwarded-Proto  https;

  # I am putting this here, because nginx only uses one location. Is this OK?
  location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
    expires 30d;
  }
}
Run Code Online (Sandbox Code Playgroud)

当我这样做时,重启nginx没有错误,但静态文件不再提供.

已经尝试了以下星座,但它不起作用:

server {
  ...
  location /rev/ {
    proxy_pass http://localhost:8910/;
    proxy_redirect off;
    proxy_set_header  Host               $host;
    proxy_set_header  X-Real-IP          $remote_addr;
    proxy_set_header  X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto  https;
  }

  location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
      expires 30d;
  }
}
Run Code Online (Sandbox Code Playgroud)

问题:如何为反向代理后面的应用程序上的静态文件应用expires头?

小智 7

我设法完成它的唯一方法是这样:

location / {
    proxy_pass  http://tomcat;
}

# CACHING WITH NO LOGGING
location ~* ^.+\.(atom|bmp|bz2|doc|docx|eot|exe|gif|gz|ico|jpeg|jpg|mid|midi|mp4|ogg|ogv|otf|pdf|png|ppt|pptx|rar|rss|rtf|svg|svgz|swf|tar|tgz|ttf|txt|wav|woff|xls|zip)$ {
    access_log  off;
    log_not_found   off;
    expires     max;
    proxy_pass  http://tomcat;
}

# CACHING WITH 404 LOGGING
location ~* ^.+\.(css|js|xml)$ {
    access_log  off;
    log_not_found   on;
    expires     max;
    proxy_pass  http://tomcat;
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!