Skip to content

Commit

Permalink
Merge pull request #312 from boostcampwm-2024/fix/nginx-ip
Browse files Browse the repository at this point in the history
🐛 fix: Nginx Forwarded-IP 전달 설정 및 채팅 날짜 알림 로직 수정
  • Loading branch information
asn6878 authored Jan 2, 2025
2 parents 38a43c8 + b53476b commit 630bd50
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
8 changes: 8 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ server {
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_read_timeout 3600s;

proxy_set_header X-Real-IP $remote_addr; # X-Real-IP 헤더로 실제 IP 전달
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # X-Forwarded-For 헤더로 실제 IP 전달
proxy_set_header X-Forwarded-Proto $scheme; # 클라이언트의 프로토콜 전달
}

# WebSocket 요청 프록시
Expand All @@ -50,5 +54,9 @@ server {
proxy_cache_bypass $http_upgrade;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;

proxy_set_header X-Real-IP $remote_addr; # X-Real-IP 헤더로 실제 IP 전달
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # X-Forwarded-For 헤더로 실제 IP 전달
proxy_set_header X-Forwarded-Proto $scheme; # 클라이언트의 프로토콜 전달
}
}
2 changes: 1 addition & 1 deletion server/src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {

if (this.dayInit) {
this.dayInit = false;
this.emitMidnightMessage();
await this.emitMidnightMessage();
}

this.server.emit('message', broadcastPayload);
Expand Down
18 changes: 13 additions & 5 deletions server/src/common/logger/logger.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@nestjs/common';
import { finalize } from 'rxjs';
import { WinstonLoggerService } from './logger.service';
import { Request } from 'express';

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
Expand All @@ -18,11 +19,7 @@ export class LoggingInterceptor implements NestInterceptor {
if (!url.includes('register') && !url.includes('login')) {
this.logger.log(
JSON.stringify({
host:
request.headers['x-forwarded-for'] ||
request.headers['CF-Connecting-IP'] ||
request.socket?.remoteAddress ||
'unknown',
host: this.getIp(request),
method: request.method,
url: request.url,
body: request.body,
Expand All @@ -39,4 +36,15 @@ export class LoggingInterceptor implements NestInterceptor {
}),
);
}

private getIp(request: Request) {
const forwardedFor = request.headers['x-forwarded-for'];

if (typeof forwardedFor === 'string') {
const forwardedIps = forwardedFor.split(',');
return forwardedIps[0].trim();
}

return request.socket.remoteAddress;
}
}
17 changes: 12 additions & 5 deletions server/src/feed/feed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,7 @@ export class FeedService {
response: Response,
) {
const cookie = request.headers.cookie;
const ip =
request.headers['CF-Connecting-IP'] ||
request.headers['x-forwarded-for'] ||
request.socket?.remoteAddress ||
'unknown';
const ip = this.getIp(request);
if (ip && this.isString(ip)) {
const redis = this.redisService.redisClient;
const [feed, hasCookie, hasIpFlag] = await Promise.all([
Expand Down Expand Up @@ -219,4 +215,15 @@ export class FeedService {
return dateNext.getTime() - dateCurrent.getTime();
});
}

private getIp(request: Request) {
const forwardedFor = request.headers['x-forwarded-for'];

if (typeof forwardedFor === 'string') {
const forwardedIps = forwardedFor.split(',');
return forwardedIps[0].trim();
}

return request.socket.remoteAddress;
}
}

0 comments on commit 630bd50

Please sign in to comment.