ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Nginx source를 이용한 compile 설치
    설치&설정 관련 2019. 7. 5. 00:20
    728x90

    Nginx source를 이용한 컴파일 설치

    오랜만에 Nginx를 설치하고 sub_filter를 사용하였는데 동작을 하지 않는다.

    원인은 http_sub_module이 설치 되지 않아서 발생하는 문제이다.
    nginx에 http_sub_module을 설치하기 위해서는 꼭 source compile 설치를 해야 한다.

    NGINX 컴파일에서 컴파일 방법에 대하여 잘 정리가 되어 있다.

    다음은 내가 사용한 명령어만 정리 하였다.

    # 최초 update & 필수 gcc g++ 설치
    $> sudo apt-get update
    $> sudo apt-get install gcc g++;
    
    # Nginx 설치 디렉토리 설정
    $> mkdir nginx_source
    $> cd nginx_source/
    
    # Nginx 1.15.10 다운
    $> wget https://nginx.org/download/nginx-1.15.10.tar.gz
    $> tar xvf nginx-1.15.10.tar.gz
    
    # Nginx 설치를 위한 모듈들 설치 START
    $> wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.43.tar.gz
    $> tar xvf pcre-8.43.tar.gz
    
    $> wget https://www.openssl.org/source/openssl-1.0.2r.tar.gz
    $> tar -xzvf openssl-1.0.2r.tar.gz
    
    $> wget http://zlib.net/zlib-1.2.11.tar.gz
    $> tar xvf zlib-1.2.11.tar.gz
    # Nginx 설치를 위한 모듈들 설치 END
    
    # 실행 계정 생성
    $> useradd --shell /usr/sbin/nologin www-data
    
    $> cd nginx-1.15.10/
    # 컴파일 옵션 설정 **--with-http_sub_module** 내가 추가한 옵션이다.
    $> sudo ./configure --with-http_sub_module --with-zlib=../zlib-1.2.11 --with-pcre=../pcre-8.43 --with-openssl=../openssl-1.0.2r --with-http_ssl_module --with-debug  --prefix=/usr/local/nginx --user=www-data --group=www-data
    $> sudo make;
    $> sudo make install;
    
    
    # Nginx init 스크립트 처리
    $> sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx;
    $> sudo chmod +x /etc/init.d/nginx;
    $> sudo update-rc.d -f nginx defaults
    $> service nginx status

    이렇게 하고 nginx.conf에 proxy pass 설정을 한다.

    location /test/{ 
        proxy_pass http://lahuman.github.io/; 
    }

    이 경우 기본적으로 주소에 http://server/test 를 호출 하면 http://lahuman.pe.kr 내용을 확인 할 수 있다. 다만 해당 컨텐츠의 링크를 확인하면 절대 경로로 표출 되어 동작이 제대로 되지 않는다.

    rewrite 와 sub_filter를 이용하여 내부 컨텐츠의 경로를 변경 하자
    내부 컨텐츠의 경로를 변경하는 방법으로 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/';
        }

    오랜만에 설정을 하였다.

    설정은 늘 힘들다...

    참조 링크

    728x90
Designed by Tistory.