Skip to content

Commit

Permalink
feat: support multiple cross-origin
Browse files Browse the repository at this point in the history
  • Loading branch information
int-tt committed Oct 16, 2024
1 parent a803d77 commit 5ae85a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ ENV PORT=8080
# Ref: https://cloud.google.com/load-balancing/docs/https#target-proxies
ENV REAL_IP_HEADER=X-Forwarded-For

# URLs with access to reearth-markplace
ENV CORS_ALLOWED_ORIGINS=""

COPY --from=node /work/dist/ /usr/share/nginx/html
COPY --chown=nginx:nginx docker/reearth_config.json.template /tmp/reearth_config.json.template
COPY --chown=nginx:nginx docker/nginx.conf.template /etc/nginx/templates/nginx.conf.template
Expand Down
17 changes: 17 additions & 0 deletions web/docker/nginx.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ log_format json escape=json '{'

real_ip_header ${REAL_IP_HEADER};

# CORS_ALLOWED_ORIGINS specifies multiple origins to which access is allowed.
# The delimiter is | and the regular expression or is used to match.
# exmaple: CORS_ALLOWED_ORIGINS=https://examplecom|http://localhosts:3000
map $http_origin $cors{
default '';
~^(${CORS_ALLOWED_ORIGINS})$ $http_origin;
}

server {
listen ${PORT};
root /usr/share/nginx/html;
Expand All @@ -25,6 +33,15 @@ server {
error_log /dev/stderr warn;

location / {
add_header 'Access-Control-Allow-Origin' "$cors";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, HEAD OPTION';
add_header 'Access-Control-Allow-Headers' 'Origin, Authorization, Accept';

if ($request_method = 'OPTIONS') {
return 204;
}

try_files $uri /index.html =404;
}

Expand Down

0 comments on commit 5ae85a2

Please sign in to comment.