前回、nginx_image_filterで画像のリサイズをするっていう記事を書いたけど、
前回の記事だと元画像よりも大きく拡大したいという事が出来ないという問題が発生してしまう。
なのでこれを解決するにはngx_small_lightというnginxのモジュールを使用することで可能になる。
けどこれを使うにはnginxの再コンパイルが必要になるという感じで中々に面倒なので、
今回はngx_small_lightを入れてnginxを再コンパイルして、small_lightで変換するまでのお話をば。
■ngx_small_lightをコンパイルするためのツールのインストール
# yum groupinstall -y "Development Tools" # yum install -y git # yum -y install libunwind perl-ExtUtils-Embed gperftools gperftools-libs gperftools-devel libxslt libxslt-devel GeoIP GeoIP-devel
■ngx_small_lightに必要なパッケージのインストール
# yum install -y pcre-devel zlib-devel openssl-devel gd-devel # yum install -y ImageMagick ImageMagick-devel # yum install http://pkgs.repoforge.org/imlib2/imlib2-1.4.4-1.el6.rf.x86_64.rpm http://pkgs.repoforge.org/imlib2/imlib2-devel-1.4.4-1.el6.rf.x86_64.rpm
■nginxをダウンロード
# cd /usr/local/sbin # wget http://nginx.org/download/nginx-1.9.3.tar.gz # tar zxfv nginx-1.9.3.tar.gz # cd nginx-1.9.3
■ngx_small_lightをダウンロード
# git clone https://github.com/cubicdaiya/ngx_small_light.git # cd ngx_small_light/ # ./setup --with-imlib2 --with-gd # enable ImageMagick and Imlib2 and GD # ngx_small_light=`pwd` # cd ..
■ngx_small_lightを含んだnginxをコンパイル
# ./configure \ --add-module=$ngx_small_light \ --prefix=/usr/local/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --http-client-body-temp-path=/var/lib/nginx/tmp/client_body \ --http-proxy-temp-path=/var/lib/nginx/tmp/proxy \ --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \ --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \ --http-scgi-temp-path=/var/lib/nginx/tmp/scgi \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/lock/subsys/nginx \ --user=nginx \ --group=nginx \ --with-file-aio \ --with-ipv6 \ --with-http_ssl_module \ --with-http_spdy_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_xslt_module \ --with-http_image_filter_module \ --with-http_geoip_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_degradation_module \ --with-http_stub_status_module \ --with-http_perl_module \ --with-mail \ --with-mail_ssl_module \ --with-pcre \ --with-google_perftools_module \ --with-debug \ --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' \ --with-ld-opt=' -Wl,-E' # make # make installec2と同じ設定で行うために大量にconfigureの記述。
■/etc/init.d/nginxの作成
# vi /etc/init.d/nginx ---------- ここから ---------- #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) sysconfig="/etc/sysconfig/$prog" lockfile="/var/lock/subsys/nginx" pidfile="/var/run/${prog}.pid" NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f $sysconfig ] && . $sysconfig start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc -p $pidfile $prog retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest_q || return 6 stop start } reload() { configtest_q || return 6 echo -n $"Reloading $prog: " killproc -p $pidfile $prog -HUP echo } configtest() { $nginx -t -c $NGINX_CONF_FILE } configtest_q() { $nginx -t -q -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } # Upgrade the binary with no downtime. upgrade() { local oldbin_pidfile="${pidfile}.oldbin" configtest_q || return 6 echo -n $"Upgrading $prog: " killproc -p $pidfile $prog -USR2 retval=$? sleep 1 if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then killproc -p $oldbin_pidfile $prog -QUIT success $"$prog online upgrade" echo return 0 else failure $"$prog online upgrade" echo return 1 fi } # Tell nginx to reopen logs reopen_logs() { configtest_q || return 6 echo -n $"Reopening $prog logs: " killproc -p $pidfile $prog -USR1 retval=$? echo return $retval } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest|reopen_logs) $1 ;; force-reload|upgrade) rh_status_q || exit 7 upgrade ;; reload) rh_status_q || exit 7 $1 ;; status|status_q) rh_$1 ;; condrestart|try-restart) rh_status_q || exit 7 restart ;; *) echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}" exit 2 esac ---------- ここまで ---------- # chmod 755 /etc/init.d/nginx # chkconfig --add nginx # chkconfig --list nginx # chkconfig nginx on
■nginx.confの編集
# vi /etc/nginx/nginx.conf ---------- ここから ---------- #user nobody; worker_processes 10; error_log /var/log/nginx/error.log; #error_log /var/log/nginx/error.log notice; #error_log /var/log/nginx/error.log info; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; include ./conf.d/*.conf; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } ---------- ここまで ----------
■logを格納するフォルダの作成
# mkdir /var/log/nginx
■/etc/nginx/conf.d/virtual.confの作成そして起動
# mkdir /etc/nginx/conf.d/ # vi /etc/nginx/conf.d/virtual.conf ---------- ここから ---------- # # A virtual host using mix of IP-, name-, and port-based configuration # server { listen 80; server_name www.hogehoge.com; root /var/www/html/; small_light on; small_light_pattern_define low dw=320,ds=s,q=100,jpeghint=y; small_light_pattern_define medium dw=640,ds=s,q=100,jpeghint=y; small_light_pattern_define high dw=960,ds=s,q=100,jpeghint=y; small_light_pattern_define thum dw=150,dh=150,cw=150,ch=150,da=s,ds=s,q=100,jpeghint=y; location ~* ^/resize/(low|medium|high|thum)/(.*)$ { set $type $1; set $file $2; set $engine "imagemagick"; proxy_pass http://127.0.0.1/small_light(p=$type,e=$engine)/img_folder/$file; } location ~ ^/small_light[^/]*/img_folder/(.+)$ { set $file $1; proxy_pass http://hogehoge.s3.amazonaws.com; rewrite ^ /img_folder/$file break; } location / { proxy_set_header Host $host; proxy_set_header X-RealIP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:8080; } } ---------- ここまで ---------- #service nginx start
ってな感じでやるとngx_small_lightをmoduleとして入れたnginxの再コンパイルをし、
さらにs3に入れた画像をngx_small_lightによってリサイズするというものが出来る。
特に変なこだわりが無ければ、この手順通りにやると問題はないんじゃないかと。
ちなみに他のサイトなどを見てると、RHEL環境にて入れようとしてる場合が多く、
その場合とAmazon Linuxの場合は/etc/init.d/nginxが違うので要注意。
そのままservice nginx startとやろうと思っても下記のようなエラーが出てしまう。
/etc/init.d/nginx: line 2: =: command not found /etc/init.d/nginx: line 3: Should: command not found /etc/init.d/nginx: line 5: Save: command not found /etc/init.d/nginx: line 7: syntax error near unexpected token `newline' /etc/init.d/nginx: line 7: `なのでネットで他のサイトなどを見て行うのもいいけど、ちゃんと自分の環境と照らし合わせる必要あり。'
ということでこれを使うとかなり簡単に画像の縮小やら拡大が出来るのでおすすめみたいな。
0 件のコメント:
コメントを投稿