config: enhance nginx with CORS headers and proper root directory setup

This commit is contained in:
Greg 2025-05-27 00:48:24 +02:00
parent 214491c1dc
commit 4dd08654c8

View File

@ -1,6 +1,8 @@
server { server {
listen 80; listen 80;
server_name localhost; server_name localhost;
root /usr/share/nginx/html;
index index.html;
# Enable gzip compression # Enable gzip compression
gzip on; gzip on;
@ -12,7 +14,12 @@ server {
include /etc/nginx/auth.conf; include /etc/nginx/auth.conf;
# Serve static files # Serve static files
try_files $uri $uri/ /index.html; try_files $uri $uri/ =404;
# CORS headers
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization' always;
# Handle preflight requests # Handle preflight requests
if ($request_method = 'OPTIONS') { if ($request_method = 'OPTIONS') {
@ -26,6 +33,17 @@ server {
} }
} }
# Special location for index.html to avoid redirection loops
location = /index.html {
# Include authentication configuration
include /etc/nginx/auth.conf;
# CORS headers
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization' always;
}
# Proxy requests to the data API # Proxy requests to the data API
location /data/ { location /data/ {
proxy_pass http://localhost:3000/data/; proxy_pass http://localhost:3000/data/;