본문 바로가기

리눅스

Apache에 커스텀 헤더 추가하기

반응형

Apache에 커스텀 헤더 추가하기

Apache에서 커스텀 HTTP 응답 헤더를 추가하는 가장 일반적인 방법은 mod_headers 모듈을 사용합니다.

mod_headers 모듈 활성화 확인

vim /usr/local/apache2/conf/httpd.conf
LoadModule headers_module modules/mod_headers.so

또는(로드된 모듈)

/usr/local/apache2/bin/apachectl -M | grep headers_module
 headers_module (shared)

Apache 설정 파일에 추가

vim /usr/local/apache2/conf/httpd.conf
더보기

---

<IfModule headers_module>
    #
    # Avoid passing HTTP_PROXY environment to CGI's on this or any proxied
    # backend servers which have lingering "httpoxy" defects.
    # 'Proxy' request header is undefined by the IETF, not listed by IANA
    #
    RequestHeader unset Proxy early
</IfModule>

---

<IfModule headers_module>
    RequestHeader unset Proxy early
    Header always set X-Served-By "web-01"
</IfModule>
  • Header set → 정상 응답(200~399)에만 적용(기본)
  • Header always set → 200, 301, 404, 500 등 모든 응답 상태 코드에 헤더 적용
728x90

보안 관련: RequestHeader unset Proxy early

RequestHeader unset Proxy early
  • httpoxy 취약점 방어용 (CVE-2016-5387)
  • 프록시 서버 뒤에서 CGI 스크립트가 HTTP_PROXY 환경변수를 잘못 해석하는 문제 방지
  • 이 줄은 유지하는 게 안전합니다
항목 설명
전역 적용 httpd.conf에 넣으면 모든 VirtualHost, 모든 사이트에 적용
가상호스트 오버라이드 가능 특정 VirtualHost에서 Header unset으로 제거 가능
에러 페이지에도 적용 always 사용해야 404/500 등에도 헤더 보임

Apache 설정 테스트 & 재시작

문법 체크

/usr/local/apache2/bin/apachectl -t
Syntax OK

재시작

/usr/local/apache2/bin/apachectl restart

확인 방법

curl -I http://your-server-ip/
HTTP/2 200 
date: Thu, 13 Nov 2025 01:31:50 GMT
content-type: text/html; charset=UTF-8
cf-ray: 99da83084ed08b6c-ICN
X-Served-By: web-01
cf-cache-status: DYNAMIC
server: cloudflare

curl로 확인

curl -s -I https://www.scbyun.com | grep x-served-by
x-served-by: web-01

 

728x90
반응형