2012-08-15 2 views
19

두 시스템 (모두 nginx로드 밸런서와 백업 역할 중 하나)을 사용하고 있습니다. 몇 가지 http 사용자 정의 헤더를 추가하고 사용하고 싶습니다. 헤더 그냥 추가 할 위치 블록에 다음 코드를 추가 추가하려면nginx에서 헤더 (HTTP) 추가 및 사용

예를 들어, 귀하의 제안

upstream upstream0{ 
      #list of upstream servers 
      server backend:80; 
      server backup_load_balancer:777 backup; 
      #healthcheck 
    } 

    server{ 
     listen 80; 
     #Add custom header about the port and protocol (http or https) 
     server_name  _; 
     location/{ 
       proxy_pass "http://upstream0;#" is included since links are not allowed in the post 
     } 

    } 

// 백업 시스템

server{ 
     listen 777; 
     server_name  _; 
     #doing some other extra stuf 

     #use port and protocol to direct 
} 

감사

답변

51

을주십시오 헤더 :

location some-location { 
    add_header X-my-header my-header-content;  
} 

분명히 x-my-header와 my-header-content를 추가 할 내용으로 바꾸십시오. 그게 전부입니다.

+0

감사합니다. 다른 청취자에게 어떻게 읽습니까? – mohan

+2

$ http_HEADER 및 $ send_http_HEADER 변수는 nginx의 헤더 내용에 액세스 할 수 있습니다. http://wiki.nginx.org/HttpCoreModule#Variables – cobaco

+0

'proxy_pass'가 사용될 때'add_header'가 작동합니까? 이 질문은 그것과 모순되는 것 같습니다 : http://stackoverflow.com/questions/14501047/how-to-add-a-response-header-on-nginx-when-using-proxy-pass –

5

$ http_로 시작하는 업스트림 헤더와 추가 사용자 정의 헤더를 사용할 수 있습니다. 예를 들어 :

curl -H "X-Upstream-01: HEADER1" -I http://localhost:11443/ 

응답이 서버에서 seted X-HDR-01, 및 X-업스트림-01에 의해 seted을 포함

add_header X-Upstream-01 $http_x_upstream_01; 
add_header X-Hdr-01 txt01; 

다음, 콘솔 및 사용자의 헤더로 요청을로 이동 고객 :

HTTP/1.1 200 OK 
Server: nginx/1.8.0 
Date: Mon, 30 Nov 2015 23:54:30 GMT 
Content-Type: text/html;charset=UTF-8 
Connection: keep-alive 
X-Hdr-01: txt01 
X-Upstream-01: HEADER1