본문 바로가기
Ubuntu

[Ubuntu] Nginx 웹서버 활용

by 너드나무 2024. 5. 25.
728x90

서론

http, https 프로토콜에 대한 웹 서버 역할을 수행하는 nginx의 기본 설정 방법들을 정리한다.
ubuntu의 nginx 기본 설치 경로는 /etc/nginx 이다.

HTTPS 설정 (Certbot 활용, 무료 SSL)

  • certbot 설치 및 도메인 연결
$ sudo apt-get update
$ sudo apt-get install certbot python3-certbot-nginx
$ sudo certbot --nginx -d example.com -d www.example.com
  • 적용 시 nginx 설정 파일
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://localhost:8080;
        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;
    }
}
  1. listen 443 ssl : Nginx가 443번 포트에서 SSL을 사용하여 요청을 수신합니다.
  2. ssl_certificate 및 ssl_certificate_key : SSL 인증서와 개인 키의 경로를 지정합니다.
  3. include /etc/letsencrypt/options-ssl-nginx.conf : SSL 옵션을 포함합니다.
  4. ssl_dhparam : DH 매개변수 파일을 포함합니다.
  5. 첫 번째 server 블록은 HTTP 요청을 HTTPS로 리디렉션합니다.

Proxy 포트포워딩 (8080)

  • Port 80(http, http://example.com), 443(https, https://(www.)example.com) -> localhost:8080
server {
    listen 80;
    server_name example.com;

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

server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://localhost:8080;
        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;
    }
}
  1. listen 80 : Nginx가 80번 포트에서 요청을 수신합니다.
  2. server_name example.com : 서버의 도메인 이름을 지정합니다.
  3. location / { ... } : 모든 경로에 대해 요청을 처리하는 블록입니다.
  4. proxy_pass http://localhost:8080 : 모든 요청을 로컬 호스트의 8080번 포트로 포워딩합니다.
  5. proxy_set_header ... : 원래 요청의 헤더 정보를 유지하도록 설정합니다.

Redirect 시나리오

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location /old-path {
        return 301 /new-path;
    }

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

server {
    listen 443 ssl;
    server_name old-domain.com;

    ssl_certificate /etc/letsencrypt/live/old-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/old-domain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    return 301 https://new-domain.com$request_uri;
}
  1. HTTP 요청을 HTTPS로 리디렉션합니다.
  2. /old-path로 들어오는 요청을 /new-path로 리디렉션합니다.
  3. old-domain.com의 요청을 new-domain.com으로 리디렉션합니다.

리소스 처리

server {
    listen 80;
    server_name example.com;

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

    location /static/ {
        alias /var/www/example.com/static/;
        expires 30d;
        add_header Cache-Control "public";
        access_log off;
    }

    location /images/ {
        alias /var/www/example.com/images/;
        expires 30d;
        add_header Cache-Control "public";
        access_log off;
    }
}
  1. /static/ 경로에 있는 요청을 /var/www/example.com/static/ 디렉토리로 매핑합니다.
  2. /images/ 경로에 있는 요청을 /var/www/example.com/images/ 디렉토리로 매핑합니다.
  3. expires 30d : 캐시 만료 시간을 30일로 설정하여 클라이언트의 캐싱을 유도합니다.
  4. access_log off : 정적 파일에 대한 접근 로그를 비활성화하여 로그 파일 크기를 줄입니다.
  5. add_header Cache-Control "public" : 캐시 제어 헤더를 추가하여 파일이 캐시될 수 있음을 명시합니다.

프록시 캐싱

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

    # 캐시 경로 및 캐시 크기 설정
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

    server {
        listen 80;
        server_name example.com;

        location /api/ {
            proxy_pass http://localhost:8080;
            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_cache my_cache;
            proxy_cache_valid 200 1h;
            proxy_cache_use_stale error timeout updating;
        }
    }
}
  1. API 응답에 대한 캐싱 처리로 백엔드 서버의 부하 감소
  2. proxy_cache_path: 캐시 경로, 캐시 크기 및 기타 옵션을 설정합니다.
  3. proxy_cache my_cache;: 지정된 캐시 영역을 사용합니다.
  4. proxy_cache_valid 200 1h;: 200 상태 코드를 가진 응답을 1시간 동안 캐싱합니다.
  5. proxy_cache_use_stale error timeout updating;: 백엔드 서버에 문제가 발생했을 때, 이전에 캐시된 응답을 사용할 수 있도록 설정합니다.
728x90
반응형

'Ubuntu' 카테고리의 다른 글

[certbot] SSL Certification 갱신 방법  (0) 2022.05.17
Deamon 관리 (systemctl, journalctl)  (0) 2022.05.09