====== nginx Proxy ====== * [[http://wiki.nginx.org/HttpProxyModule|nginx HttpProxyModule]] Proxy Module은 Upstream Module과 함께 사용한다. * [[http://wiki.nginx.org/HttpUpstreamModule|HttpUpstreamModule]] ===== server_names_hash_bucket_size ===== 서버를 많이 추가할 경우 ''server_names_hash_bucket_size'' 관련 오류가 발생할 수 있다. 이때는 해당 값을 늘려준다. http { server_names_hash_bucket_size 64; } ===== Tomcat 연동 ===== * 로컬의 ''/example'' 컨텍스트를 ''www.example.com''으로 연동하고자 할 때 다음과 같이 선언한다. server { server_name www.example.com; location / { proxy_pass http://localhost:8000/example/; proxy_redirect http://www.example.com/example/ http://www.example.com/; proxy_cookie_path /example /; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; # ELB 등 중간자가 또 있을 경우에도 제대로 작동할까? proxy_set_header Host $host; proxy_read_timeout 60s; # 응답을 읽기까지의 타임아웃. send_timeout 과 함께 조정 } } * Tomcat의 server.xml의 ''''는 [[apache:proxy|Apache Proxy Module]]와 같다. * retry 설정은 Upstream 모듈을 사용한다. TODO ===== X-Forwarded-For ===== * L7 연동하고, ProxyPass를 사용하는 경우 L7과 웹 서버를 거치는 IP 주소를 ''X-Forwarded-For'' 헤더에 저장해야하며, 이를 통해 해당 헤더의 첫번째 값으로 요청을 보낸 클라이언트의 IP 주소를 알 수 있다. * Apache는 ProxyPass를 사용할 경우 기본으로 ''X-Forwarded-For'' 헤더를 구성하지만 nginx는 직접 구성해줘야 한다. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; * [[java:tomcat|Tomcat]]의 [[http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/valves/RemoteIpValve.html|RemoteIpValve]]로 remote IP 값을 보정해 줄 수 있다. ===== Sticky Session ===== * [[http://code.google.com/p/nginx-sticky-module/|nginx sticky module]] * 소스 컴파일해 설치해야만 한다. ===== Upstream 헤더 내용 로그로 찍기 ===== * ''$upstream_http_[headername]''이 응답 헤더 내용을 저장한 변수이다. * ''$http_[requestHeaderName]''이 요청 헤더 내용을 저장한 변수이다. * [[http://xyduan.com/logging-request-reponse-header-to-nginx/|logging request and response header to nginx access log]] ===== Keep Alive ===== * nginx - upstream 간에 Keep Alive 설정을 하는 것이 성능상 유리하다. * [[https://jojoldu.tistory.com/322|3. AWS Beanstalk을 이용한 성능 튜닝 시리즈 - Nginx 튜닝]] * [[https://brunch.co.kr/@alden/17|nginx + tomcat 연동에 대하여]] * [[https://brunch.co.kr/@alden/11|nginx upstream 성능 최적화]] ===== 참고 ===== * [[http://chase-seibert.github.io/blog/2011/12/21/nginx-ssl-reverse-proxy-tutorial.html|nginx + SSL reverse proxy tutorial]]