Linux NginX


Nginx(엔진엑스)는 높은 성능, 안정성, 풍부한 기능 세트, 간단한 구성 및 낮은 리소스 소비를 목표로 하는 웹 서버 및 리버스 프록시 서버입니다. 여기서는 Nginx의 설치, 설정, 주요 명령어 및 사용 예제에 대해 상세히 설명합니다.

Nginx 설치

Debian/Ubuntu

# 패키지 목록 업데이트
sudo apt-get update

# Nginx 설치
sudo apt-get install nginx

# Nginx 서비스 시작
sudo systemctl start nginx

# Nginx 서비스 자동 시작 설정
sudo systemctl enable nginx

Red Hat/CentOS

# EPEL 저장소 추가
sudo yum install epel-release

# Nginx 설치
sudo yum install nginx

# Nginx 서비스 시작
sudo systemctl start nginx

# Nginx 서비스 자동 시작 설정
sudo systemctl enable nginx

Nginx 기본 설정 파일

Nginx의 기본 설정 파일은 /etc/nginx/nginx.conf에 위치하며, 주요 설정 파일 구조는 다음과 같습니다.

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

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

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";

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

기본 서버 블록 설정

서버 블록은 Nginx에서 웹 서버를 구성하는 기본 단위입니다. 기본 서버 블록 설정 파일은 /etc/nginx/sites-available/default에 위치합니다.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name _;

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

Nginx 명령어

Nginx 시작, 정지 및 재시작

# Nginx 시작
sudo systemctl start nginx

# Nginx 정지
sudo systemctl stop nginx

# Nginx 재시작
sudo systemctl restart nginx

# Nginx 설정 파일 다시 로드 (재시작 없이 설정 변경 적용)
sudo systemctl reload nginx

# Nginx 상태 확인
sudo systemctl status nginx

Nginx 설정 파일 테스트

# Nginx 설정 파일 문법 확인
sudo nginx -t

Nginx 설정 예제

1. 리버스 프록시 설정

Nginx를 리버스 프록시로 설정하여 백엔드 서버로 요청을 전달할 수 있습니다.

server {
    listen 80;
    server_name example.com;

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

2. SSL 설정

SSL을 사용하여 HTTPS로 서버를 설정할 수 있습니다.

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;

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

server {
    listen 80;
    server_name example.com;

    return 301 https://$host$request_uri;
}

3. 정적 파일 제공

Nginx를 사용하여 정적 파일을 제공할 수 있습니다.

server {
    listen 80;
    server_name static.example.com;

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

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

Nginx 모니터링 및 로그 관리

로그 파일 위치

  • 액세스 로그: /var/log/nginx/access.log
  • 에러 로그: /var/log/nginx/error.log

로그 파일 확인

# 실시간 로그 확인
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log

결론

Nginx는 웹 서버 및 리버스 프록시 서버로서 매우 강력한 기능을 제공하며, 높은 성능과 안정성을 자랑합니다. 다양한 설정 옵션과 명령어를 통해 Nginx를 효과적으로 관리하고 사용할 수 있습니다. 위의 내용을 참고하여 Nginx를 설치하고 설정하는 데 도움이 되길 바랍니다.


Leave a Reply

Your email address will not be published. Required fields are marked *