Nginx는 일본 서버호스팅에서 가장 많이 사용되는 웹서버입니다. Apache보다 높은 동시 접속 처리 성능과 낮은 메모리 사용으로 특히 트래픽이 많은 서비스에 적합합니다. 이 글에서는 Nginx 설치부터 성능 최적화, 보안 설정까지 완전하게 안내합니다.

Nginx vs Apache 선택

항목 Nginx Apache
동시 접속 처리 이벤트 기반, 우수 프로세스 기반, 보통
메모리 사용 낮음 높음
정적 파일 제공 매우 빠름 빠름
.htaccess 지원 미지원 지원
리버스 프록시 우수 보통
권장 용도 고트래픽, API, 리버스 프록시 공유 호스팅, .htaccess 필요 시

일본 서버에서 자체 서비스를 운영한다면 대부분의 경우 Nginx를 권장합니다.


1단계: Nginx 설치

apt update
apt install nginx -y

systemctl enable nginx
systemctl start nginx

# 버전 및 설정 확인
nginx -v
nginx -t

2단계: 기본 설정 최적화

# /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
worker_rlimit_nofile 65535;
pid /run/nginx.pid;

events {
    worker_connections 4096;
    multi_accept on;
    use epoll;
}

http {
    # 기본 설정
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;  # Nginx 버전 정보 숨김

    # 타임아웃
    keepalive_timeout 30;
    keepalive_requests 1000;
    client_body_timeout 10;
    client_header_timeout 10;
    send_timeout 30;

    # 버퍼 설정
    client_body_buffer_size 128k;
    client_max_body_size 50m;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 16k;

    # MIME 타입
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # 로그
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log warn;

    # Gzip 압축
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 1000;
    gzip_types
        text/plain text/css text/xml text/javascript
        application/json application/javascript application/xml
        application/rss+xml font/truetype font/opentype
        image/svg+xml;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

3단계: 가상 호스트(Server Block) 설정

# /etc/nginx/sites-available/mysite.conf
server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/mysite;
    index index.html index.php;

    # 보안 헤더
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;

    location / {
        try_files $uri $uri/ =404;
    }

    # PHP 처리 (PHP-FPM 사용 시)
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # 정적 파일 캐싱
    location ~* \.(jpg|jpeg|png|gif|webp|ico|svg|css|js|woff|woff2|ttf)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    # 숨김 파일 차단 (.git, .env 등)
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}

설정 활성화:

ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx

4단계: 리버스 프록시 설정 (Node.js/Python 앱)

upstream myapp {
    server 127.0.0.1:3000;
    keepalive 32;
}

server {
    listen 80;
    server_name api.yourdomain.com;

    location / {
        proxy_pass http://myapp;
        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_read_timeout 90;
        proxy_connect_timeout 90;
    }
}

5단계: 접속 제한 설정 (보안)

http {
    # IP당 연결 속도 제한
    limit_req_zone $binary_remote_addr zone=general:10m rate=30r/m;
    limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
    limit_conn_zone $binary_remote_addr zone=addr:10m;

    server {
        location / {
            limit_req zone=general burst=10 nodelay;
            limit_conn addr 20;
        }

        # 로그인 페이지 강화
        location /login {
            limit_req zone=login burst=5;
        }
    }
}

유용한 Nginx 명령어

nginx -t              # 설정 파일 문법 확인
nginx -s reload       # 무중단 설정 재로드
nginx -s stop         # 즉시 중지
systemctl reload nginx # 설정 재로드 (권장)

# 로그 실시간 모니터링
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log

마치며

Nginx를 올바르게 설정하면 일본 서버에서 빠르고 안전한 웹 서비스를 운영할 수 있습니다. 설정에 어려움이 있으시면 TCP-80.NET 텔레그램 @tcp80net으로 문의해 주세요.