Skip to content

Commit faba152

Browse files
authored
Enhance CORS configuration in nginx.conf
Added CORS headers for OPTIONS requests and updated max age.
1 parent 7fcfd15 commit faba152

1 file changed

Lines changed: 48 additions & 25 deletions

File tree

nginx.conf

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ events {
33
}
44

55
http {
6+
# Map only allows your geode-solutions.com domains (including next.vease.geode-solutions.com)
67
map $http_origin $allow_origin {
7-
~^https://(.*\.)?geode-solutions\.com$ $http_origin;
8-
default "";
8+
~^https://(.*\.)?geode-solutions\.com$ $http_origin;
9+
default "";
910
}
10-
11+
1112
gzip on;
1213
gzip_proxied any;
1314
gzip_types text/plain application/json;
@@ -16,51 +17,56 @@ http {
1617
server {
1718
listen 80;
1819
server_name localhost;
19-
2020
client_max_body_size 0;
2121

22-
location / {
23-
if ($request_method = OPTIONS) {
22+
# ====================== /geode/ location ======================
23+
location ~ "^/geode/" {
24+
# Preflight OPTIONS - handled by nginx (fast, no hit to Flask)
25+
if ($request_method = 'OPTIONS') {
2426
add_header 'Access-Control-Allow-Origin' $allow_origin always;
2527
add_header 'Access-Control-Allow-Credentials' 'true' always;
2628
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
2729
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-Token' always;
28-
add_header 'Access-Control-Max-Age' 86400 always;
29-
add_header 'Vary' 'Origin' always;
30-
add_header 'Content-Type' 'text/plain; charset=utf-8' always;
31-
add_header 'Content-Length' 0 always;
30+
add_header 'Access-Control-Max-Age' 1728000 always; # 20 days
31+
add_header 'Content-Type' 'text/plain; charset=utf-8';
32+
add_header 'Content-Length' 0;
3233
return 204;
3334
}
34-
return 404;
35-
}
36-
37-
location ~ "^/geode/" {
38-
limit_except DELETE GET POST PUT OPTIONS {
39-
deny all;
40-
}
41-
35+
36+
# Normal requests
37+
limit_except DELETE GET POST PUT OPTIONS { deny all; }
38+
4239
add_header 'Access-Control-Allow-Origin' $allow_origin always;
4340
add_header 'Access-Control-Allow-Credentials' 'true' always;
4441
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
4542
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-Token' always;
4643
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
4744
add_header 'Vary' 'Origin' always;
48-
45+
4946
rewrite "^/geode/(.*)" /$1 break;
5047
proxy_pass http://localhost:5000;
5148
proxy_http_version 1.1;
52-
5349
proxy_set_header Host $host;
5450
proxy_set_header X-Real-IP $remote_addr;
5551
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
5652
proxy_set_header X-Forwarded-Proto $scheme;
5753
}
5854

55+
# ====================== /viewer/ location ======================
5956
location ~ "^/viewer/" {
60-
limit_except GET POST OPTIONS {
61-
deny all;
57+
if ($request_method = 'OPTIONS') {
58+
add_header 'Access-Control-Allow-Origin' $allow_origin always;
59+
add_header 'Access-Control-Allow-Credentials' 'true' always;
60+
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
61+
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-Token' always;
62+
add_header 'Access-Control-Max-Age' 1728000 always;
63+
add_header 'Content-Type' 'text/plain; charset=utf-8';
64+
add_header 'Content-Length' 0;
65+
return 204;
6266
}
63-
67+
68+
limit_except GET POST OPTIONS { deny all; }
69+
6470
add_header 'Access-Control-Allow-Origin' $allow_origin always;
6571
add_header 'Access-Control-Allow-Credentials' 'true' always;
6672
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
@@ -71,14 +77,31 @@ http {
7177
rewrite "^/viewer/(.*)" /$1 break;
7278
proxy_pass http://localhost:1234;
7379
proxy_http_version 1.1;
74-
7580
proxy_set_header Host $host;
7681
proxy_set_header X-Real-IP $remote_addr;
7782
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
7883
proxy_set_header X-Forwarded-Proto $scheme;
79-
8084
proxy_set_header Connection "keep-alive, Upgrade";
8185
proxy_set_header Upgrade websocket;
8286
}
87+
88+
# Catch-all for anything else (optional, returns proper CORS even on 404)
89+
location / {
90+
if ($request_method = 'OPTIONS') {
91+
add_header 'Access-Control-Allow-Origin' $allow_origin always;
92+
add_header 'Access-Control-Allow-Credentials' 'true' always;
93+
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
94+
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-Token' always;
95+
add_header 'Access-Control-Max-Age' 1728000 always;
96+
add_header 'Content-Type' 'text/plain; charset=utf-8';
97+
add_header 'Content-Length' 0;
98+
return 204;
99+
}
100+
101+
add_header 'Access-Control-Allow-Origin' $allow_origin always;
102+
add_header 'Access-Control-Allow-Credentials' 'true' always;
103+
add_header 'Vary' 'Origin' always;
104+
return 404;
105+
}
83106
}
84107
}

0 commit comments

Comments
 (0)