Written by coh at home

[NGINX] WAS 와 연동하기 본문

카테고리 없음

[NGINX] WAS 와 연동하기

och 2024. 11. 12. 23:36

WAS - nginx 연결

nginx 설치

sudo apt update
sudo apt install nginx
sudo systemctl status nignx

nginx 설정파일 수정.

sudo vi /etc/nginx/sites-available/default

WAS의 포트 8080에 연결하기 위한 설정 파일을 수정. 80번 포트로 들어오는 요청을 WAS의 8080으로 보냅니다.

server {
    listen 80;
    server_name localhost;

    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;
    }
}

$는 변수를 의미한다.
다음은 header에 대한 GPT의 설명이다.

proxy_set_header

proxy_set_header는 Nginx가 요청을 다른 서버(여기서는 Spring Boot 서버)에 전달할 때 요청 헤더를 설정하는 데 사용됩니다. 이를 통해 클라이언트 정보를 프록시 서버가 보낸 요청에도 포함시킬 수 있습니다.

2-1. proxy_set_header Host $host;

• 설명: Host 헤더는 원래 클라이언트 요청에 들어있던 호스트 정보를 전달합니다.
• 역할: 이 설정은 원래 요청의 호스트 이름을 그대로 전달하여, 백엔드(Spring Boot)가 실제 클라이언트가 요청한 호스트 이름을 알 수 있게 합니다.
• 예시: http://example.com에 대한 요청이 들어온 경우, Host 헤더에는 example.com이 포함됩니다.

2-2. proxy_set_header X-Real-IP $remote_addr;

• 설명: X-Real-IP는 클라이언트의 실제 IP 주소를 전달하는 헤더입니다.
• 역할: Nginx가 프록시 역할을 할 때, 백엔드는 프록시 서버(Nginx)의 IP 주소만 알게 됩니다. 따라서 클라이언트의 실제 IP 주소를 전달합니다.
• 변수: $remote_addr는 클라이언트의 IP 주소를 나타내는 Nginx 내장 변수입니다.

2-3. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

• 설명: X-Forwarded-For는 원래 클라이언트와, 요청이 거친 모든 프록시 서버의 IP 주소를 나열하는 헤더입니다.
• 역할: 프록시 체인을 통해 요청이 전달될 때, 각 프록시 서버의 IP 주소를 헤더에 추가하여 백엔드가 요청 경로를 추적할 수 있게 합니다.
• 변수: $proxy_add_x_forwarded_for는 기존 X-Forwarded-For 헤더에 현재 클라이언트의 IP 주소를 추가한 값을 반환하는 Nginx 내장 변수입니다.

2-4. proxy_set_header X-Forwarded-Proto $scheme;

• 설명: X-Forwarded-Proto는 클라이언트가 요청을 보낼 때 사용한 프로토콜(HTTP 또는 HTTPS)을 전달하는 헤더입니다.
• 역할: 백엔드 서버에서 요청이 HTTP 또는 HTTPS 중 어느 프로토콜로 들어왔는지 알 수 있도록 합니다.
• 변수: $scheme은 요청이 들어온 프로토콜(http 또는 https)을 나타내는 Nginx 내장 변수입니다.

nginx 설정 테스트

sudo nginx -t
-> nginx: configuration file /etc/nginx/nginx.conf test is successful

nginx 재시작

sudo systemctl restart nginx

Q. 전부 엔드포인트에 알아서 매핑 되는거야?

모든 URL 경로 (/ 이하의 모든 경로)를 http://localhost:8080에 있는 WAS로 프록시한다.

Q. 정적 파일은 어떻게 처리해

Nginx에서 일부 정적 리소스를 대신 처리하고 나머지는 WAS로 처리할 수도 있다.
/static/ 이하 파일들은 NGINX가 직접 제공하고 나머지 경로는 WAS로 프록시 된다. 일반적으로 정적 파일은 nginx 서버의 /var/www/html 경로 아래에 저장한다. 정적 파일은 WAS에 저장할 수도 있지만 정적 파일이 많거나 WAS의 리소스 제한을 피하려면 nginx에서 제공하는 것이 좋다.

server {
    listen 80;
    server_name localhost;

    location /static/ {
        alias /var/www/html/static/;
    }

    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;
    }
}

예를 들어, http://localhost/static/images/logo.png 요청은 Nginx가 /var/www/html/static/images/logo.png 파일을 반환한다.
WAS가 스프링이라면 다음과 같이 가져올 수 있다.

@RestController
@RequestMapping("/api")
public class HeaderController {

    @GetMapping("/headers")
    public String getHeaders(HttpServletRequest request) {
        // 프록시 서버를 통한 원래 클라이언트의 IP 주소
        String realIp = request.getHeader("X-Real-IP");
        String forwardedFor = request.getHeader("X-Forwarded-For");

        // 원래 요청이 HTTP/HTTPS인지 확인
        String forwardedProto = request.getHeader("X-Forwarded-Proto");

        // 원래 요청한 호스트
        String host = request.getHeader("Host");

        return String.format("X-Real-IP: %s, X-Forwarded-For: %s, X-Forwarded-Proto: %s, Host: %s", 
                realIp, forwardedFor, forwardedProto, host);
    }
}