![URL 첫 번째 헤더를 기반으로 nginx 요청을 리디렉션하는 방법](https://linux55.com/image/136519/URL%20%EC%B2%AB%20%EB%B2%88%EC%A7%B8%20%ED%97%A4%EB%8D%94%EB%A5%BC%20%EA%B8%B0%EB%B0%98%EC%9C%BC%EB%A1%9C%20nginx%20%EC%9A%94%EC%B2%AD%EC%9D%84%20%EB%A6%AC%EB%94%94%EB%A0%89%EC%85%98%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95.png)
nginx 서버에서 처리하는 3개의 애플리케이션 서버가 있습니다.
upstream he {
server h1.abc.example.com;
server h2.abc.example.com;
}
특정 헤더 값을 기반으로 요청을 리디렉션하는 방법은 무엇입니까? 예를 들어
abc-h1.abc.example.com should go to server
h1.abc.example.com
def-h1.abc.example.com should go to server
h2.abc.example.com
모든 요청의 위치는 동일합니다 -h1.abc.example.com
.
답변1
Nginx는 확실히 이 작업을 수행할 수 있습니다. 호스트별 헤더( server_name
)를 지정하고 업스트림을 분리하여 각 호스트에 하나씩 할당하기만 하면 됩니다.
다음과 같은 nginx 서버 조각이 작동할 수 있습니다(내 생각이며 테스트되지 않음).
upstream one { server h1.abc.example.com; }
upstream two { server h2.abc.example.com; }
server {
listen 8080;
server_name abc-h1.abc.example.com;
location / {
proxy_pass one;
}
}
server {
listen 8080;
server_name def-h1.abc.example.com;
location / {
proxy_pass two;
}
}
HTTP가 아닌 엔드포인트로 트래픽을 보내려는 경우 다른 프록시 핸들러( fastcgi_pass
, uwsgi_pass
, scgi_pass
, ) 가 있습니다.memcached_pass
편집: 오류 수정server_name