Skip to content

Commit

Permalink
fix: CORS 처리 Nginx로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
Miensoap committed Nov 11, 2024
1 parent 7a5316f commit 775cfba
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
91 changes: 91 additions & 0 deletions backend/resources/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
events { }

http {
root /usr/share/nginx/html/frontend/dist;
include /etc/nginx/mime.types;

# HTTP 요청을 HTTPS로 리디렉션
server {
listen 80;
server_name api.dailyroad.site dailyroad.site;

location / {
return 301 https://$host$request_uri;
}
}

server {
listen 443 ssl;
server_name dailyroad.site www.dailyroad.site;

ssl_certificate /etc/letsencrypt/live/dailyroad.site/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/dailyroad.site/privkey.pem;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;

location / {
try_files $uri $uri/ /index.html =404;
}

# NestJS API 요청 처리 (추후논의)
location /api/ {
rewrite ^/api/(.*) /$1 break;
proxy_pass http://localhost:8080;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Prefix /api;
proxy_set_header X-Forwarded-Host $http_x_forwarded_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-Proto $scheme;

proxy_pass_request_body on;
proxy_method $request_method;
}
}

# API 서브도메인
server {
listen 443 ssl;
server_name api.dailyroad.site;

ssl_certificate /etc/letsencrypt/live/dailyroad.site/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/dailyroad.site/privkey.pem;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;

# CORS
location / {
set $cors_origin "";
if ($http_origin ~* "^https?://(localhost:5173|dailyroad\.site|api\.dailyroad\.site)$") {
set $cors_origin $http_origin;
}

add_header 'Access-Control-Allow-Origin' $cors_origin always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;


if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' $cors_origin always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Content-Length' 0 always;
return 204;
}

# NestJS 프록시
proxy_pass http://localhost:8080;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
3 changes: 0 additions & 3 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalFilters(new GlobalExceptionFilter());
app.useGlobalPipes(new ValidationPipe({ transform: true }));
app.enableCors({
origin: 'http://localhost:5173',
});
await app.listen(8080);
}

Expand Down

0 comments on commit 775cfba

Please sign in to comment.