서로 다른 두 개의 AWS 인스턴스에서 실행되는 애플리케이션이 있고 IP를 기반으로 “고정”또는 “지속적”세션을 활성화하여 특정 방식으로 웹 소켓 기술을 활용할 수 있습니다.
ip_hash
이 고정 세션을 활성화 하는 데 사용 되는 두 가지 설정이 있습니다 .
첫 번째 설정에서 앱 프로세스는 Nginx 구성과 동일한 인스턴스에서 실행됩니다. 이것은 작동 하고 있으며 세션은 예상대로 지속됩니다.
upstream my_app {
ip_hash;
# local servers
server 127.0.0.1:3001 weight=100 max_fails=5 fail_timeout=300;
server 127.0.0.1:3002 weight=100 max_fails=5 fail_timeout=300;
keepalive 8;
}
두 번째 설정에서는 외부 인스턴스를 가리키고 동일한 효과를 얻으려고합니다. 이 설정이 작동하지 않습니다 . 다시 말해 세션은 여전히로드 밸런싱되고 있습니다.
upstream my_app {
ip_hash;
# external servers
server 111.11.11.11:3001 weight=100 max_fails=5 fail_timeout=300;
server 222.22.22.22:3002 weight=100 max_fails=5 fail_timeout=300;
keepalive 8;
}
ip_hash
올바르게 사용 하고 있습니까? 외부 서버에 “고정”IP 기반 세션을 활성화하려면 어떻게해야합니까?
답변
내 서버는 AWS로드 밸런싱 뒤에 있었으므로 클라이언트 헤더를 항상 반영 할 수 있도록 올바른 헤더를 업스트림으로 전달해야했습니다. 다음 구성으로 문제가 해결되었습니다 (주석을 참조하십시오).
upstream my_app {
ip_hash;
server 111.11.11.11:3001 weight=100 max_fails=5 fail_timeout=300;
server 222.22.22.22:3002 weight=100 max_fails=5 fail_timeout=300;
keepalive 8;
}
server {
server_name my-app.com;
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
# This is necessary to pass the correct IP to be hashed
real_ip_header X-Real-IP;
proxy_pass http://my_app/;
proxy_redirect off;
}
}
답변
Nginx 문서에 따르면 Sticky 세션 지원은 비싼 Plus 버전에서만 사용할 수 있습니다. 나는 대안을 연구 해 왔으며 Nginx 1.5 이상과 호환되지 않는이 오래된 포크는 https://github.com/luin/nginx-sticky-module
또한 LUA 모듈을 만들려고했지만 열거 및 차단을 위해 피어 선택을위한 API 후크가 없습니다.
최신 정보
다른 훌륭한 모듈을 찾았습니다. https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/src를 참조 하십시오.