[NGINX] proxy_pass 설정 후 상세 내용 LINK URL 패턴 자동 변경
Nginx에서 proxy_pass를 이용하여 다른 사이트를 서비스 할 경우, 내용에 있는 LINK가 절대 경로로 되어있을 경우 해결 하는 방법에 대하여 알아 본다.
우선 다음과 같은 설정이 있다는 전제로 시작 한다.
location /test/{
proxy_pass http://lahuman.pe.kr/;
}
이 경우 기본적으로 주소에 http://server/test 를 호출 하면 http://lahuman.pe.kr 내용을 확인 할 수 있다. 다만 해당 컨텐츠의 링크를 확인하면 절대 경로로 표출 되어 동작이 제대로 되지 않는다.
- 내부 컨텐츠의 경로를 변경하는 방법으로 sub_filter를 이용할 수 있다.
location /test/ {
rewrite ^/test(/.*)$ $1 break;
proxy_pass http://lahuman.pe.kr/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
proxy_set_header Accept-Encoding "";
sub_filter_types *;
sub_filter_once off;
sub_filter '(?i)src=[\"\']/' 'src="/test/';
sub_filter '(?i)href=[\'\"]/' 'href="/test/';
sub_filter '/doc4sm/' '/test/doc4sm/';
}
몇가지 옵션이 있지만, 간단하게 위와 같이 설정 하면 컨텐츠의 내부 링크 주소가 변경된다.
MD FILE :
proxy_pass-sub_filter.md