웹사이트 검색

Debian 8(Jessie)에서 Google PageSpeed 모듈로 nginx를 빌드하는 방법


이 튜토리얼은 다음 OS 버전에 대해 존재합니다.

  • 데비안 7(Wheezy)

이 페이지에서

  1. 1 PageSpeed로 nginx 구축
  2. 2 PageSpeed 구성
  3. 3개의 링크

작성자: F4RR3LL | 스벤

이 가이드에서는 Debian Jessie에서 최신 버전의 nginx_pagespeed 모듈로 nginx를 빌드하는 방법을 설명합니다. PageSpeed 모듈은 웹 성능 모범 사례를 페이지 및 관련 자산(CSS, 자바스크립트, 이미지)에 적용하여 웹사이트 속도를 높이고 로드 시간을 줄입니다.

1 PageSpeed로 nginx 구축하기

nginx는 모듈의 동적 로딩을 지원하지 않으므로 PageSpeed 지원으로 다시 빌드해야 합니다. nginx를 .deb 패키지로 빌드하여 기존 nginx 설치를 nginx로 교체하거나 다른 시스템에 쉽게 설치할 수 있습니다.

먼저 빌드 디렉터리를 만듭니다.

cd /usr/src
mkdir nginx-pagespeed && cd nginx-pagespeed

/etc/apt/sources.list에 Jessie에 대한 deb 및 deb-src 행이 있는지 확인하십시오.

nano /etc/apt/sources.list
[...]
deb http://ftp.de.debian.org/debian jessie main contrib non-free
deb-src http://ftp.de.debian.org/debian jessie main contrib non-free
[...]

패키지 목록을 업데이트하고 몇 가지 필수 구성 요소를 설치합니다.

apt-get update
apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev unzip curl libcurl4-openssl-dev libossp-uuid-dev

nginx 소스 및 빌드 종속성을 다운로드합니다.

apt-get source nginx
apt-get build-dep nginx

nginx 버전을 확인하겠습니다.

ls -l
:/usr/src/nginx-pagespeed# ls -l
insgesamt 1388
drwxr-xr-x 10 root root 4096 Apr 29 22:16 nginx-1.6.2
-rw-r–r– 1 root root 604568 Dez 1 12:51 nginx_1.6.2-5.debian.tar.xz
-rw-r–r– 1 root root 2827 Dez 1 12:51 nginx_1.6.2-5.dsc
-rw-r–r– 1 root root 804164 Sep 17 2014 nginx_1.6.2.orig.tar.gz

nginx 버전은 1.6.2입니다. 다운로드한 nginx 소스의 debian/modules 디렉토리로 이동합니다...

cd /usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/

... 그리고 PageSpeed 소스를 다운로드합니다(이 페이지에서 최신 릴리스를 확인할 수 있습니다: https://github.com/pagespeed/ngx_pagespeed/releases - 이 예에서 버전은 1.9.32.3-beta입니다. 버전이 다른 경우 , 이 자습서의 나머지 부분에서 교체해야 합니다.)

wget https://github.com/pagespeed/ngx_pagespeed/archive/release-1.9.32.3-beta.zip
unzip release-1.9.32.3-beta.zip
mv ngx_pagespeed-release-1.9.32.3-beta/ ngx_pagespeed
cd ngx_pagespeed/
wget https://dl.google.com/dl/page-speed/psol/1.9.32.3.tar.gz
tar -xzvf 1.9.32.3.tar.gz

debian/rules 파일을 편집할 수 있습니다.

nano /usr/src/nginx-pagespeed/nginx-1.6.2/debian/rules

해당 파일에서 세 개의 configure_flags 섹션을 찾을 수 있습니다.

–add-module=$(MODULESDIR)/ngx_pagespeed \ 

추가해야 합니다:

–without-http_uwsgi_module 뒤의 light_configure_flags 섹션,
–with-mail_ssl_module 뒤의 full_configure_flags 섹션,
–add-module=$ (MODULESDIR)/nginx-development-kit 뒤의 extras_configure_flags 섹션

결과 파일은 아래와 같아야 합니다(줄 앞에 공백을 추가하려면 탭 키를 사용하십시오).

#!/usr/bin/make -f
export DH_VERBOSE=1

debian_cflags:=$(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
debian_ldflags:=$(shell dpkg-buildflags --get LDFLAGS)

# export necessary perl hardenging flags
# see: src/http/modules/perl/Makefile.PL
DEBIAN_NGINX_PERL_LDFLAGS:= $(debian_ldflags)
export DEBIAN_NGINX_PERL_LDFLAGS

FLAVOURS := full light extras

MODULESDIR = $(CURDIR)/debian/modules
BASEDIR = $(CURDIR)
$(foreach flavour,$(FLAVOURS),$(eval BUILDDIR_$(flavour) = $(CURDIR)/debian/build-$(flavour)))

DEB_BUILD_ARCH ?=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifeq ($(DEB_BUILD_ARCH),sparc)
	debian_cflags += -m32 -mcpu=ultrasparc
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

# configure flags
common_configure_flags := \
			--with-cc-opt="$(debian_cflags)" \
			--with-ld-opt="$(debian_ldflags)" \
			--prefix=/usr/share/nginx \
			--conf-path=/etc/nginx/nginx.conf \
			--http-log-path=/var/log/nginx/access.log \
			--error-log-path=/var/log/nginx/error.log \
			--lock-path=/var/lock/nginx.lock \
			--pid-path=/run/nginx.pid \
			--http-client-body-temp-path=/var/lib/nginx/body \
			--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
			--http-proxy-temp-path=/var/lib/nginx/proxy \
			--http-scgi-temp-path=/var/lib/nginx/scgi \
			--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
			--with-debug \
			--with-pcre-jit \
			--with-ipv6 \
			--with-http_ssl_module \
			--with-http_stub_status_module \
			--with-http_realip_module \
			--with-http_auth_request_module


light_configure_flags := \
			$(common_configure_flags) \
			--with-http_gzip_static_module \
			--without-http_browser_module \
			--without-http_geo_module \
			--without-http_limit_req_module \
			--without-http_limit_zone_module \
			--without-http_memcached_module \
			--without-http_referer_module \
			--without-http_scgi_module \
			--without-http_split_clients_module \
			--without-http_ssi_module \
			--without-http_userid_module \
			--without-http_uwsgi_module \
			--add-module=$(MODULESDIR)/ngx_pagespeed \
			--add-module=$(MODULESDIR)/nginx-echo

full_configure_flags := \
			$(common_configure_flags) \
			--with-http_addition_module \
			--with-http_dav_module \
			--with-http_geoip_module \
			--with-http_gzip_static_module \
			--with-http_image_filter_module \
			--with-http_spdy_module \
			--with-http_sub_module \
			--with-http_xslt_module \
			--with-mail \
			--with-mail_ssl_module \
			--add-module=$(MODULESDIR)/ngx_pagespeed \
			--add-module=$(MODULESDIR)/nginx-auth-pam \
			--add-module=$(MODULESDIR)/nginx-dav-ext-module \
			--add-module=$(MODULESDIR)/nginx-echo \
			--add-module=$(MODULESDIR)/nginx-upstream-fair \
			--add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module

extras_configure_flags := \
			$(common_configure_flags) \
			--with-http_addition_module \
			--with-http_dav_module \
			--with-http_flv_module \
			--with-http_geoip_module \
			--with-http_gzip_static_module \
			--with-http_image_filter_module \
			--with-http_mp4_module \
			--with-http_perl_module \
			--with-http_random_index_module \
			--with-http_secure_link_module \
			--with-http_spdy_module \
			--with-http_sub_module \
			--with-http_xslt_module \
			--with-mail \
			--with-mail_ssl_module \
			--add-module=$(MODULESDIR)/headers-more-nginx-module \
			--add-module=$(MODULESDIR)/nginx-auth-pam \
			--add-module=$(MODULESDIR)/nginx-cache-purge \
			--add-module=$(MODULESDIR)/nginx-dav-ext-module \
			--add-module=$(MODULESDIR)/nginx-development-kit \
			--add-module=$(MODULESDIR)/ngx_pagespeed \
			--add-module=$(MODULESDIR)/ngx-fancyindex \
			--add-module=$(MODULESDIR)/nginx-http-push \
			--add-module=$(MODULESDIR)/nginx-lua \
			--add-module=$(MODULESDIR)/nginx-upload-progress \
			--add-module=$(MODULESDIR)/nginx-upstream-fair \
			--add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module

%:
	dh  --with systemd

override_dh_auto_configure: $(foreach flavour,$(FLAVOURS),config.arch.$(flavour))
override_dh_auto_build:     $(foreach flavour,$(FLAVOURS),build.arch.$(flavour))
override_dh_strip:          $(foreach flavour,$(FLAVOURS),strip.arch.$(flavour))
override_dh_clean:          $(foreach flavour,$(FLAVOURS),clean.$(flavour))
	dh_clean

override_dh_installinit:
	dh_installinit --no-restart-on-upgrade --no-start --name=nginx

override_dh_systemd_enable:
	dh_systemd_enable --name=nginx

override_dh_installlogrotate:
	dh_installlogrotate --package nginx-common --name=nginx

build.arch.%:
	$(MAKE) -C $(BUILDDIR_$*) build

strip.arch.%:
	dh_strip --package=nginx-$(*) --dbg-package=nginx-$(*)-dbg

config.arch.%:
	dh_testdir
	mkdir -p $(BUILDDIR_$*)
	cp -Pa $(CURDIR)/auto $(BUILDDIR_$*)/
	cp -Pa $(CURDIR)/conf $(BUILDDIR_$*)/
	cp -Pa $(CURDIR)/configure $(BUILDDIR_$*)/
	cp -Pa $(CURDIR)/contrib $(BUILDDIR_$*)/
	cp -Pa $(CURDIR)/src $(BUILDDIR_$*)/
	cp -Pa $(CURDIR)/man $(BUILDDIR_$*)/
	cd $(BUILDDIR_$*) && ./configure $($*_configure_flags)

clean.%:
	rm -rf $(BUILDDIR_$*)

그런 다음 다음을 실행합니다.

cd /usr/src/nginx-pagespeed/nginx-1.6.2/ && dpkg-buildpackage -b

새로운 nginx .deb 패키지를 빌드합니다. 그런 다음 생성된 패키지를 살펴보겠습니다.

cd /usr/src/nginx-pagespeed
ls -l
:/usr/src/nginx-pagespeed# ls -l
insgesamt 99720
drwxr-xr-x 10 root root 4096 Apr 29 22:16 nginx-1.6.2
-rw-r–r– 1 root root 72086 Apr 29 23:06 nginx_1.6.2-5_all.deb
-rw-r–r– 1 root root 3961 Apr 29 23:08 nginx_1.6.2-5_amd64.changes
-rw-r–r– 1 root root 604568 Dez 1 12:51 nginx_1.6.2-5.debian.tar.xz
-rw-r–r– 1 root root 2827 Dez 1 12:51 nginx_1.6.2-5.dsc
-rw-r–r– 1 root root 804164 Sep 17 2014 nginx_1.6.2.orig.tar.gz
-rw-r–r– 1 root root 86540 Apr 29 23:06 nginx-common_1.6.2-5_all.deb
-rw-r–r– 1 root root 83716 Apr 29 23:06 nginx-doc_1.6.2-5_all.deb
-rw-r–r– 1 root root 3403690 Apr 29 23:08 nginx-extras_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 31745456 Apr 29 23:08 nginx-extras-dbg_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 3232788 Apr 29 23:07 nginx-full_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 29932616 Apr 29 23:07 nginx-full-dbg_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 3136400 Apr 29 23:07 nginx-light_1.6.2-5_amd64.deb
-rw-r–r– 1 root root 28975322 Apr 29 23:08 nginx-light-dbg_1.6.2-5_amd64.deb

이제 다음과 같이 PageSpeed를 지원하는 nginx를 설치할 수 있습니다.

dpkg --install nginx-common_1.6.2-5_all.deb nginx_1.6.2-5_all.deb nginx-full_1.6.2-5_amd64.deb

그런 다음 nginx를 다시 시작합니다.

service nginx restart

PageSpeed 모듈이 성공적으로 빌드되었는지 확인합니다.

nginx -V

출력에 ngx_pagespeed 모듈이 표시되어야 합니다.

:/usr/src/nginx-pagespeed# nginx -V
nginx version: nginx/1.6.2
TLS SNI support enabled
configure arguments: –with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' –with-ld-opt=-Wl,-z,relro –prefix=/usr/share/nginx –conf-path=/etc/nginx/nginx.conf –http-log-path=/var/log/nginx/access.log –error-log-path=/var/log/nginx/error.log –lock-path=/var/lock/nginx.lock –pid-path=/run/nginx.pid –http-client-body-temp-path=/var/lib/nginx/body –http-fastcgi-temp-path=/var/lib/nginx/fastcgi –http-proxy-temp-path=/var/lib/nginx/proxy –http-scgi-temp-path=/var/lib/nginx/scgi –http-uwsgi-temp-path=/var/lib/nginx/uwsgi –with-debug –with-pcre-jit –with-ipv6 –with-http_ssl_module –with-http_stub_status_module –with-http_realip_module –with-http_auth_request_module –with-http_addition_module –with-http_dav_module –with-http_geoip_module –with-http_gzip_static_module –with-http_image_filter_module –with-http_spdy_module –with-http_sub_module –with-http_xslt_module –with-mail –with-mail_ssl_module –add-module=/usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/ngx_pagespeed –add-module=/usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/nginx-auth-pam –add-module=/usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/nginx-dav-ext-module –add-module=/usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/nginx-echo –add-module=/usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/nginx-upstream-fair –add-module=/usr/src/nginx-pagespeed/nginx-1.6.2/debian/modules/ngx_http_substitutions_filter_module

2 PageSpeed 구성

PageSpeed를 활성화하려면 /etc/nginx/nginx.conf...를 엽니다.

nano /etc/nginx/nginx.conf

... 그리고 다음 행을 추가하십시오. pagespeed on; 및 pagespeed FileCachePath /var/ngx_pagespeed_cache; 가상 호스트 이전:

[...]
        pagespeed on;
        pagespeed FileCachePath /var/ngx_pagespeed_cache;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
[...]

캐시 디렉토리를 생성하고 nginx를 다시 로드합니다.

mkdir /var/ngx_pagespeed_cache
chown -R www-data:www-data /var/ngx_pagespeed_cache
service nginx reload

페이지를 로드하고 출력에 PageSpeed가 언급되어 있는지 확인합니다.

curl -I -p http://localhost|grep X-Page-Speed
:/usr/src/nginx-pagespeed# curl -I -p http://localhost|grep X-Page-Speed
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
X-Page-Speed: 1.9.32.3-4448

이제 우리는 PageSpeed를 개별적으로 또는 각 가상 호스트를 구성할 수 있습니다. 이와 같이:

nano /etc/nginx/sites-available/example.com.vhost
server {
[...]
  # let's speed up PageSpeed by storing it in the super duper fast memcached
  pagespeed MemcachedThreads 1;
  pagespeed MemcachedServers "localhost:11211";

  # Filter settings
  pagespeed RewriteLevel CoreFilters;
  pagespeed EnableFilters collapse_whitespace,remove_comments;

  #  Ensure requests for pagespeed optimized resources go to the pagespeed
  #  handler and no extraneous headers get set.
  location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
    add_header "" "";
  }
  location ~ "^/ngx_pagespeed_static/" {
  }
  location ~ "^/ngx_pagespeed_beacon$" {
  }
  location /ngx_pagespeed_statistics {
    allow 127.0.0.1;
    deny all;
  }
  location /ngx_pagespeed_global_statistics {
    allow 127.0.0.1;
    deny all;
  }
  location /ngx_pagespeed_message {
    allow 127.0.0.1;
    deny all;
  }
  location /pagespeed_console {
    allow 127.0.0.1;
    deny all;
  }
[...]
}

중요한 행은 적용해야 하는 필터를 PageSpeed에 알려주는 pagespeed EnableFilters 행입니다. 여기에서 모든 필터 목록을 찾을 수 있습니다: http://ngxpagespeed.com/ngx_pagespeed_example/

나중에 nginx를 다시 시작하는 것을 잊지 마십시오.

service nginx reload

지금 페이지를 열고 헤더를 보면(예: FireFox용 Live HTTP 헤더 애드온 사용) x-page-speed 줄이 표시되어야 합니다. 페이지의 HTML 소스를 확인하여 PageSpeed 필터가 예상대로 작동하는지 확인할 수도 있습니다.

3 링크

  • Google 개발자 블로그: http://googledevelopers.blogspot.nl/2013/04/speed-up-your-sites-with-pagespeed-for.html
  • ngx_pagespeed: https://developers.google.com/speed/pagespeed/ngx
  • PageSpeed 구축/사용: https://github.com/pagespeed/ngx_pagespeed
  • ngx_pagespeed 필터 예: http://ngxpagespeed.com/ngx_pagespeed_example/\n