/엔지니어/네트워킹 / 서버/Nginx 웹서버 기본 설정과 리버스 프록시
네트워킹 / 서버초급ubuntudebiancentosnginx웹서버proxy

Nginx 웹서버 기본 설정과 리버스 프록시

Nginx 설치부터 정적 파일 서빙, 리버스 프록시, 로드 밸런싱 설정까지 실무에서 바로 쓸 수 있도록 설명합니다.

설치

Bash
sudo apt update && sudo apt install -y nginx
sudo systemctl enable --now nginx
sudo nginx -t          # 설정 문법 검사
sudo nginx -s reload

설정 파일 구조

CODE
/etc/nginx/
├── nginx.conf
├── sites-available/
│   └── myapp.conf
└── sites-enabled/
    └── myapp.conf -> ../sites-available/myapp.conf

정적 파일 서빙

Nginx
server {
    listen 80;
    server_name example.com;

    root /var/www/html;
    index index.html;

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

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

    gzip on;
    gzip_types text/plain text/css application/json application/javascript;
}

리버스 프록시

Nginx
server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://127.0.0.1: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;

        proxy_connect_timeout 30s;
        proxy_read_timeout    60s;
    }
}

로드 밸런싱

Nginx
upstream backend {
    least_conn;
    server 10.0.0.1:3000 weight=3;
    server 10.0.0.2:3000;
    server 10.0.0.3:3000 backup;
}

server {
    listen 80;
    location / {
        proxy_pass http://backend;
    }
}

보안 헤더

Nginx
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header Strict-Transport-Security "max-age=31536000" always;
server_tokens off;

설정 활성화

Bash
sudo ln -s /etc/nginx/sites-available/myapp.conf \
           /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
#nginx#웹서버#proxy#loadbalancer#reverse-proxy
편집 안내 · Editorial Note

이 가이드는 AI 도구를 활용해 초안을 구성하고 사람이 명령어·문맥을 검토해 발행했습니다. 운영체제와 도구 버전에 따라 결과가 달라질 수 있으므로 적용 전 공식 문서를 함께 확인하세요. 오류를 발견하시면 이메일로 제보해 주세요.

관련 공식 문서NGINX 공식 문서

질문 & 답변 (Q&A)

이 가이드에 대해 궁금한 점을 질문해보세요. 확인 후 답변드립니다.