Skip to main content

Apache vs Nginx

Apache vs Nginx 아키텍처, 성능, 설정 비교


📚 시리즈 네비게이션

이전현재다음
Tomcat OverviewApache vs NginxApache + Tomcat

시리즈 목차


🎯 개요

항목Apache HTTP ServerNginx
출시1995년2004년
개발Apache Software FoundationIgor Sysoev (현 F5)
아키텍처프로세스/스레드 기반이벤트 기반 (비동기)
점유율레거시 환경에서 강세신규 환경에서 강세
설정.htaccess 지원conf 파일만

🏗️ 아키텍처 비교

Apache: 프로세스/스레드 기반

Apache는 MPM (Multi-Processing Module) 방식으로 요청을 처리함.

MPM Prefork

  • 요청당 하나의 프로세스
  • 안정적, mod_php와 호환
  • 메모리 사용량 높음

MPM Worker

  • 프로세스 내 여러 스레드
  • Prefork보다 효율적
  • 스레드 안전하지 않은 모듈 사용 불가

MPM Event

  • Worker 기반 + Keep-Alive 연결 분리 처리
  • 가장 효율적인 MPM
  • Apache 2.4+ 기본값

Nginx: 이벤트 기반

  • 적은 수의 Worker 프로세스 (보통 CPU 코어 수)
  • 각 Worker가 수천 개의 연결 처리 (비동기 I/O)
  • 메모리 효율적

아키텍처 비교 요약

항목Apache (Prefork)Apache (Event)Nginx
동시 연결낮음중간높음
메모리높음중간낮음
CPU높음중간낮음
C10K 문제취약개선됨강함

C10K 문제: 동시 10,000개 연결 처리. Nginx는 이 문제를 해결하기 위해 설계됨.


⚡ 성능 비교

정적 파일 처리

항목ApacheNginx
처리량보통2-3배 빠름
동시 접속수백~수천수만
메모리 사용연결당 증가거의 일정

동적 콘텐츠

항목ApacheNginx
PHPmod_php (내장)PHP-FPM (별도)
성능비슷함비슷함
설정간단약간 복잡

동적 콘텐츠는 결국 백엔드(PHP, Tomcat 등)가 처리하므로 Web Server 차이가 크지 않음.

벤치마크 예시 (참고용)

# 정적 파일 10,000 요청, 동시 100 연결
# (실제 성능은 환경마다 다름)

Apache (Event MPM):
Requests per second: 8,500
Transfer rate: 45 MB/sec

Nginx:
Requests per second: 22,000
Transfer rate: 120 MB/sec

📝 설정 방식 비교

Apache 설정

메인 설정:

# /etc/httpd/conf/httpd.conf (CentOS)
# /etc/apache2/apache2.conf (Ubuntu)

ServerRoot "/etc/httpd"
Listen 80

# MPM 설정
<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
</IfModule>

# 가상 호스트
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html

<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

.htaccess (디렉토리별 설정):

# /var/www/html/.htaccess
RewriteEngine On
RewriteRule ^old-page$ /new-page [R=301,L]

# 인증
AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user

장점:

  • .htaccess로 디렉토리별 설정 가능
  • 서버 재시작 없이 설정 변경
  • 공유 호스팅에서 유용

단점:

  • .htaccess 파싱 오버헤드
  • 설정 파일이 분산됨

Nginx 설정

메인 설정:

# /etc/nginx/nginx.conf

user nginx;
worker_processes auto; # CPU 코어 수

events {
worker_connections 1024;
use epoll; # Linux
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

sendfile on;
keepalive_timeout 65;

# 가상 호스트
include /etc/nginx/conf.d/*.conf;
}

가상 호스트:

# /etc/nginx/conf.d/example.conf

server {
listen 80;
server_name example.com;
root /var/www/html;

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

location /api {
proxy_pass http://127.0.0.1:8080;
}

location ~* \.(jpg|jpeg|png|gif|css|js)$ {
expires 30d;
add_header Cache-Control "public";
}
}

장점:

  • 설정 파일 중앙 집중
  • 파싱 오버헤드 없음
  • 문법이 깔끔함

단점:

  • .htaccess 없음 (디렉토리별 설정 불가)
  • 설정 변경 시 reload 필요

설정 비교 요약

항목ApacheNginx
문법XML 스타일C 스타일
.htaccess지원미지원
설정 적용즉시 (.htaccess)reload 필요
분산 설정가능불가

🔌 모듈 시스템

Apache 모듈

동적 로딩:

# 모듈 활성화
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule proxy_module modules/mod_proxy.so

주요 모듈:

모듈기능
mod_rewriteURL 재작성
mod_sslHTTPS
mod_proxy리버스 프록시
mod_phpPHP 내장 실행
mod_jkTomcat AJP 연동
mod_securityWAF

Nginx 모듈

Nginx는 컴파일 시점에 모듈이 결정됨 (정적 링크).

확인:

nginx -V  # 컴파일된 모듈 목록

주요 모듈:

모듈기능
ngx_http_ssl_moduleHTTPS
ngx_http_proxy_module리버스 프록시
ngx_http_upstream_module로드밸런싱
ngx_http_gzip_module압축
ngx_http_rewrite_moduleURL 재작성

Nginx Plus (상용) 또는 OpenResty는 동적 모듈 지원.


🆚 언제 뭘 쓰나?

Apache 선택

상황이유
레거시 시스템기존 .htaccess 설정 활용
공유 호스팅사용자별 설정 필요
mod_php 사용PHP 내장 실행
복잡한 URL Rewritemod_rewrite 강력함
Apache 전용 모듈mod_security 등

Nginx 선택

상황이유
고성능/대용량동시 접속 처리 우수
정적 파일 서빙빠르고 효율적
리버스 프록시설정 간단, 성능 좋음
로드밸런서upstream 설정 강력
마이크로서비스API Gateway 역할
컨테이너 환경가볍고 빠름

함께 쓰기 (Nginx + Apache)


📊 점유율 트렌드

연도ApacheNginx
201060%5%
201550%15%
202035%30%
202430%35%

Nginx가 역전한 상황. 특히 상위 트래픽 사이트에서 Nginx 점유율이 높음.


🔗 시리즈 네비게이션

이전다음
Tomcat OverviewApache + Tomcat Integration

시리즈 목차로 돌아가기


🔗 참고 자료