Skip to content

Commit

Permalink
feat(nginx-core): add CORS configs (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD authored Nov 8, 2024
2 parents 01ee1ae + 302c39d commit e381706
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion nginx-core/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ ENV NGINX_ACCESS_LOG="/var/log/nginx/access.log json" \
NGINX_FORCE_REDIRECT_STATUS=307 \
NGINX_AUTO_WEBP="" \
NGINX_ENTRYPOINT_WORKER_PROCESSES_AUTOTUNE=1 \
NGINX_ENTRYPOINT_QUIET_LOGS=""
NGINX_ENTRYPOINT_QUIET_LOGS="" \
NGINX_CORS_ENABLE="" \
NGINX_CORS_ORIGIN="*" \
NGINX_CORS_METHODS="GET" \
NGINX_CORS_HEADERS="*" \
NGINX_CORS_MAXAGE=86400

ENV NGINX_CLIENT_MAX_BODY_SIZE=10m \
NGINX_SENDFILE=on \
Expand Down
11 changes: 11 additions & 0 deletions nginx-core/etc/nginx/entrypoint.d/93-cors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
set -eu

ME=$(basename "$0")

test -n "${NGINX_CORS_ENABLE:-}" && exit 0

echo "$ME: Remove CORS config"
rm -fv /etc/nginx/conf.d/location.d/70-cors.conf

exit 0
16 changes: 16 additions & 0 deletions nginx-core/etc/nginx/templates/location.d/70-cors.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
add_header 'Access-Control-Allow-Origin' $NGINX_CORS_ORIGIN;
add_header 'Access-Control-Allow-Methods' $NGINX_CORS_METHODS;
add_header 'Access-Control-Allow-Headers' $NGINX_CORS_HEADERS;
add_header 'Access-Control-Max-Age' $NGINX_CORS_MAXAGE;

if ($request_method = 'OPTIONS') {
# Handle OPTIONS requests (preflight requests for complex requests)
add_header 'Access-Control-Allow-Origin' $NGINX_CORS_ORIGIN;
add_header 'Access-Control-Allow-Methods' $NGINX_CORS_METHODS;
add_header 'Access-Control-Allow-Headers' $NGINX_CORS_HEADERS;
add_header 'Access-Control-Max-Age' $NGINX_CORS_MAXAGE;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;

return 204;
}

0 comments on commit e381706

Please sign in to comment.