import { NestFactory, Reflector } from "@nestjs/core";
import { Logger, ValidationPipe } from "@nestjs/common";
import { AppModule } from "./app.module";
import { RolesGuard } from "./admin/role.guard";
import * as express from "express";
import { CustomIoAdapter } from "./custom.io.adapter";
import { readFileSync } from "fs";
import * as cors from 'cors';
async function bootstrap() {
let app;
if (process.env.NODE_ENV === "production") {
console.debug("===== [main] 배포 환경에서 실행되는 로그 =====");
Logger.overrideLogger(["log", "warn", "error"]);
const keyPath = "/etc/letsencrypt/live/api.xn--oy2b11o0qft9m.com/privkey.pem";
const certPath = "/etc/letsencrypt/live/api.xn--oy2b11o0qft9m.com/fullchain.pem";
const httpsOptions = {
key: readFileSync(keyPath),
cert: readFileSync(certPath)
};
app = await NestFactory.create(AppModule, { httpsOptions });
} else {
console.debug("===== [main] 개발 환경에서 실행되는 로그 =====");
app = await NestFactory.create(AppModule);
}
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true
})
);
process.env.TZ = "Asia/Seoul";
app.use(cors({
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
allowedHeaders: 'Content-Type, Accept',
credentials: true,
}));
app.useGlobalGuards(new RolesGuard(new Reflector()));
app.useWebSocketAdapter(new CustomIoAdapter(app));
app.use("/uploads", express.static("uploads"));
await app.listen(8000);
}
bootstrap();
22|nestjs-app | TypeError: Invalid character in header content ["Access-Control-Allow-Origin"]
22|nestjs-app | at ServerResponse.setHeader (node:_http_outgoing:662:3)
22|nestjs-app | at applyHeaders (/root/seasonfree-backend/node_modules/cors/lib/index.js:153:15)
22|nestjs-app | at applyHeaders (/root/seasonfree-backend/node_modules/cors/lib/index.js:149:11)
22|nestjs-app | at applyHeaders (/root/seasonfree-backend/node_modules/cors/lib/index.js:149:11)
22|nestjs-app | at cors (/root/seasonfree-backend/node_modules/cors/lib/index.js:187:7)
22|nestjs-app | at /root/seasonfree-backend/node_modules/cors/lib/index.js:224:17
22|nestjs-app | at originCallback (/root/seasonfree-backend/node_modules/cors/lib/index.js:214:15)
22|nestjs-app | at /root/seasonfree-backend/node_modules/cors/lib/index.js:219:13
22|nestjs-app | at optionsCallback (/root/seasonfree-backend/node_modules/cors/lib/index.js:199:9)
22|nestjs-app | at Array.corsMiddleware (/root/seasonfree-backend/node_modules/cors/lib/index.js:204:7) {
22|nestjs-app | code: 'ERR_INVALID_CHAR'
22|nestjs-app | }
After developing WAS using nestJS, I uploaded it to the server and used it well. However, I received a manager call today saying that there is an error all of a sudden. The React and Nest applications are currently running on one server, both of which will communicate through nginx. The following is the nginx configuration file.
[API NGINX]
server {
listen 80;
server_name api.xn--oy2b11o0qft9m.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name api.xn--oy2b11o0qft9m.com;
ssl_certificate /etc/letsencrypt/live/api.xn--oy2b11o0qft9m.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.xn--oy2b11o0qft9m.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
# Add CORS headers
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, OPTIONS, DELETE, PUT' always;
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;
add_header 'Access-Control-Max-Age' 86400 always;
add_header 'Content-Length' 0;
add_header 'Content-Type' 'text/plain charset=UTF-8';
return 204;
}
proxy_pass https://127.0.0.1:8000;
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;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
# location 안에 넣어줘야함
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
}
location /socket.io/ {
# Add CORS headers
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, OPTIONS, DELETE, PUT' always;
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;
add_header 'Access-Control-Max-Age' 86400 always;
add_header 'Content-Length' 0;
add_header 'Content-Type' 'text/plain charset=UTF-8';
return 204;
}
proxy_pass https://127.0.0.1:8000/socket.io/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
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;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
# location 안에 넣어줘야함
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
}
}
[React NGINX]
server {
listen 80;
server_name xn--oy2b11o0qft9m.com;
# HTTP 요청을 HTTPS로 리다이렉트
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name xn--oy2b11o0qft9m.com;
# SSL 인증서 적용하기
ssl_certificate /etc/letsencrypt/live/xn--oy2b11o0qft9m.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xn--oy2b11o0qft9m.com/privkey.pem;
location / {
proxy_pass http://localhost:3000;
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;
}
}
I've tried everything possible, like restarting the server, Googling, asking GPT, etc.