[Nginx] Adding Dynamic Module

반응형

이번글에서는 Nginx에 dynamic module을 추가하는 방법에 대해 알아보도록 하겠습니다.

1. Dynamic Module이란?

nginx의 module에는 defualt module인 static과 추가적으로 사용할 수 있는 dynamic moudle이 있습니다.

이전글 https://velog.io/@minholee_93/Nginx-Overview-Install 에서 설명드렸다 싶이 Nginx를 source code version으로 install 하면 여러가지 dynamic module을 손쉽게 추가해 사용할 수 있습니다.

image.png

현재 nginx에 설치된 module은 다음의 명령어로 확인할 수 있습니다.

nginx -V

현제 제 nginx에는 static moudle들만 설치되어 있습니다.

image.png

install 할 수 있는 moudle은 nginx configure 파일이 있는 directory로 이동한 뒤 아래의 명령어로 확인 할 수 있습니다.

./configure --help

image.png

위의 moudle 중에 dynamic으로 끝나는 module 들이 dynamic moudle이며, 아래의 명령어로 dynamic moudle만 검색할 수 있습니다.

./configure --help | grep dynamic

image.png

2. Change Configure

이제 nginx의 configure를 수정해 image filter dynamic moudle을 추가해보겠습니다.

신규로 moudle을 추가할때는 기존의 moudle들도 빼놓지 않고 configure에 추가해야 합니다. 이를 위해 nginx -V 명령어를 사용해 기존의 moudle을 확인하고 추가할 moudle을 맨뒤에 입력합니다.

old version

## custom configuration setting

./configure --sbin-path=/usr/bin/nginx 
--conf-path=/etc/nginx/nginx.conf 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log 
--with-pcre 
--pid-path=/var/run/nginx.pid 
--with-http_ssl_module

add image filter dynmaic moudle version

## custom configuration setting

./configure --sbin-path=/usr/bin/nginx 
--conf-path=/etc/nginx/nginx.conf 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log 
--with-pcre 
--pid-path=/var/run/nginx.pid 
--with-http_ssl_module 
--with-http_image_filter_module=dynamic

다음으로 install한 dynmaic moudle을 configure의 directory로 이동시키기위해 아래와 같은 구문을 추가로 입력합니다.

--modules-path=/etc/nginx/modules

add moudle directory version

## custom configuration setting

./configure --sbin-path=/usr/bin/nginx 
--conf-path=/etc/nginx/nginx.conf 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log 
--with-pcre 
--pid-path=/var/run/nginx.pid 
--with-http_ssl_module 
--with-http_image_filter_module=dynamic
--modules-path=/etc/nginx/modules

gd library가 없다는 에러가 발생할 경우 아래의 명령어로 다운받습니다.

# download gd library
sudo yum install gd-*

image.png

configure 설정이 끝나면 아래의 명령어를 순서대로 입력해 새로운 version의 nginx를 생성합니다.

## compile configuration source
make

## install compile source
make install

## reload nginx
systemctl reload nginx

이후 다시한번 nginx -V로 nginx의 configuration을 확인해보면 아래와 같이 dynamic moudle이 추가된 것을 확인할 수 있습니다.

image.png

module directory도 정상적으로 생성되었습니다.

image.png

3. Change nginx.conf

이제 위에서 다운받은 dynmaic module을 사용하기 위해 nginx.conf를 수정해야 합니다.

static module과 다르게 dynamic module은 nginx가 실행될때 같이 load 되지 않으므로, nginx에서 사용하기 위해선 아래와 같이 load_module 구문을 포함시켜야 합니다.

load_module modules/ngx_http_image_filter_module.so;

events {
    worker_connections  1024;
}

http {
        include mime.types;

        server {
                listen 80;
                server_name 13.125.252.144;
                root /sites/demo;
        }
}

이후 /thumb.png request에 대해 image를 180도 회전시키도록 아래와 같이 location을 작성해보겠습니다.

load_module modules/ngx_http_image_filter_module.so;

events {
    worker_connections  1024;
}

http {
        include mime.types;

        server {
                listen 80;
                server_name 13.125.252.144;
                root /sites/demo;

                location =/thumb.png {
                    image_filter rotate 180;
                }
        }
}

reload 후 /thumb.png로 접근해보면 아래와 같이 뒤집어진 그림이 return 되는 것을 확인할 수 있습니다.

image.png

위와같이 configure를 수정해 nginx의 새로운 version을 만든뒤, nginx.conf에 install한 dynamic moudle을 load하면 손쉽게 dynamic module을 사용할 수 있습니다. 👏👏👏


참고 자료 : https://www.udemy.com/course/nginx-fundamentals/


반응형

'Nginx' 카테고리의 다른 글

[Nginx] Compressed Response with gzip  (0) 2020.08.31
[Nginx] Header & Expire  (0) 2020.08.31
[Nginx] Buffer & Timeout  (0) 2020.08.27
[Nginx] Worker Process  (0) 2020.08.27
[Nginx] Logging  (0) 2020.08.27

댓글

Designed by JB FACTORY