nginx: add CORS for /storage#19
Conversation
WalkthroughAdds a commented CORS configuration block to the non-SSL server’s /storage location in nginx/templates/default.conf.template, including documentation comments and disabled add_header directives. Inserts a blank line before the alias directive. No functional changes; headers remain inactive unless uncommented. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
nginx/templates/default.conf.template (1)
35-41: CORS preset needs fixes before use: avoid wildcard+credentials, handle OPTIONS, addalways, and expose range headers.If enabled as‑is, this template would:
- Send
Access-Control-Allow-Credentials: truewith*origin (invalid per CORS).- Miss
HEADmethod and preflight handling forOPTIONS.- Omit
always, so headers won’t appear on 206/4xx and some preflight responses.- Omit
Access-Control-Expose-Headersneeded for range/streaming.Apply this improved commented preset so it’s correct when uncommented:
- # Uncomment the following lines to allow CORS - # add_header 'Access-Control-Allow-Origin' '*'; - # add_header 'Access-Control-Allow-Credentials' 'true'; - # add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; - # add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; - # add_header 'Access-Control-Max-Age' 3600; + # CORS (public, no credentials). Safe default when /storage is read-only: + # add_header 'Access-Control-Allow-Origin' '*' always; + # add_header 'Access-Control-Allow-Methods' 'GET, HEAD, OPTIONS' always; + # add_header 'Access-Control-Allow-Headers' 'DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Range' always; + # add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; + # add_header 'Access-Control-Max-Age' 86400 always; + # if ($request_method = 'OPTIONS') { return 204; } + + # If you must support credentials, DO NOT use '*' for ACAO. + # Prefer whitelisting and echoing the origin (example shown below).Example (credentials + whitelist). Place near http{} or server{} and then uncomment in this location:
# http { ... } # map $http_origin $cors_origin { # default ""; # "~^https?://(app|www)\.example\.com$" $http_origin; # } # In location /storage: # add_header 'Access-Control-Allow-Origin' $cors_origin always; # add_header 'Vary' 'Origin' always; # add_header 'Access-Control-Allow-Credentials' 'true' always; # add_header 'Access-Control-Allow-Methods' 'GET, HEAD, OPTIONS' always; # add_header 'Access-Control-Allow-Headers' 'DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,Range' always; # add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; # add_header 'Access-Control-Max-Age' 86400 always; # if ($request_method = 'OPTIONS') { return 204; }Please confirm:
- Do you need credentials? If yes, which exact origins should be allowed?
- What Nginx version are you deploying? (
alwaysrequires 1.7.5+, common but worth confirming.)
moeflow-com/moeflow-frontend#41 may require this preset , if site and /storage are in different origins
Summary by CodeRabbit