O NGINX ( Engine X ) é sem dúvida um dos web servers mais optimizados em termos de velocidade de entrega de conteúdo. O módulo NGX PageSpeed dá um gás ainda maior na entrega de conteúdo no web server. Neste artigo irei exemplificar como fazer a instalação do NGX PageSpeed no NGINX.
Principais funcionalidades
HTTPS support with URL control.
Image optimization: stripping meta-data, dynamic resizing, recompression.
CSS and JavaScript minification, concatenation, inlining, and outlining.
Small resource inlining.
Deferring image and JavaScript loading.
HTML rewriting.
Cache lifetime extension.
Allows configuring for multiple servers and many others.
Instalação
Estarei utilizando o Debian 9 para instalar o NGX PageSpeed rodando com um NGIX 1.10.1
Vamos a instalação:
1) Execute o comando via CLI
$ sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip 2) Criando o diretório para download dos pacotes. Começaremos pelo NGINX
$ mkdir -p ~/make_nginx $ cd ~/make_nginx $ wget -c https://nginx.org/download/nginx-1.13.2.tar.gz $ tar -xzvf nginx-1.13.2.tar.gz 3) Baixando o PageSpeed
$ wget -c https://github.com/pagespeed/ngx_pagespeed/archive/v1.12.34.2-stable.zip $ unzip v1.12.34.2-stable.zip 4) Mova o PageSpeed para o diretório do NGINX.
$ cd ngx_pagespeed-1.12.34.2-stable/ $ wget -c https://dl.google.com/dl/page-speed/psol/1.12.34.2-x64.tar.gz $ tar -xvzf 1.12.34.2-x64.tar.gz 5) Configurando e compilando o NgINX e o PageSpeed
$ cd ~/make_nginx/nginx-1.13.2 $ ./configure --add-module=$HOME/nginx/ngx_pagespeed-1.12.34.2-stable/ ${PS_NGX_EXTRA_FLAGS}
$ make $ sudo make install
$ sudo ln -s /usr/local/nginx/conf/ /etc/nginx $ sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
6) Criando o arquivo do Unit para o SystemD
$ sudo vi /lib/systemd/system/nginx.service
[Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target 7) Iniciando o NGINX
$ sudo systemctl start nginx $ sudo systemctl enable nginx 8) Configurando o módulo PageSpeed
$ sudo mkdir -p /var/ngx_pagespeed_cache $ sudo chown -R nobody:nogroup /var/ngx_pagespeed_cache
Para habilitar o módulo do PageSpeed no NGINX abra o arquivo de configuração do NGIX em
$ sudo vi /etc/nginx/nginx.conf
Adicione as linhas abaixo:
# Pagespeed main settings pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; # 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" { } Modelo de arquivo de configuração do NGIX com o NGX PageSpeed configurado
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid /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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; # Pagespeed main settings pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; # 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 / { 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; # } #} } Verificando configuração
$ sudo nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful Testando o PageSpeed O módulo do PageSpeed adiciona uma flag no header do pacote TCP informando que ele está presente. Para saber execute:
$ curl -I -p http://localhostPara saber mais sobre o projeto do PageSpeed acess: https://github.com/pagespeed/ngx_pagespeed