digitalocean / nginxconfig.io

⚙️ NGINX config generator on steroids 💉
https://do.co/nginxconfig
MIT License
27.66k stars 2.04k forks source link

Disable caching when using a reverse proxy #467

Open ProphetLamb opened 7 months ago

ProphetLamb commented 7 months ago

Generate a general.conf without media caching for reverse proxy consumption.

Information

The routing does not pass through the proxy when location handling for media caching is present in the general.conf, instead nginx attempts to route these requests via the default site. The directory of the default site - in my case - is empty. It doesn't contain the css or js resources the web app at the proxy_pass offers. It is not desired and not reasonable to cache a proxy location by default.

Details

Description

This is caused by the location handling in general.conf included in every reverse proxy domain.

# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
    expires 7d;
}

# svg, fonts
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
    add_header Access-Control-Allow-Origin "*";
    expires    7d;
}

Steps to reproduce

  1. Create a basic node application and serve at localhost:3000.
  2. Create a NGINX Config with a proxy pass from the subdomain app.example.com to localhost:3000.
  3. Open your browser devtools network tab
  4. Request app.example.com in your browser
  5. Media types handled in general.conf will fail with 404.

Expected behavior

Resources are requested from the reverse proxy

Screenshots

example

-  - [25/Feb/2024:19:51:08 +0000] "GET /_app/immutable/chunks/entry.aDhfE7Nt.js HTTP/2.0" 404 548 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
-  - [25/Feb/2024:19:51:08 +0000] "GET /_app/immutable/chunks/index.CA4DBg1v.js HTTP/2.0" 404 548 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"