Para o Apache ao menos existe os pacotes prontos para instalar, mas no Nginx você precisa compilar e instalar manualmente. Tive que fazer isso por aqui e então decidi escrever esse artigo para compartilhar o processo que escolhi seguir.
No site do módulo do PageSpeed (https://www.modpagespeed.com/doc/build_ngx_pagespeed_from_source), você vai perceber que sugere compilar tanto o módulo, quanto o próprio Nginx. Mas no meu caso eu decidi manter a versão padrão do Nginx dos repositórios do Ubuntu 18.04 LTS que é a 1.14.0 e apenas instalar e configurar o módulo do PageSpeed. Eu não testei em outras distribuições ou versões do Ubuntu e do Nginx, mas acredito que vale a pena testar.
Primeiro vamos nos certificar sobre a versão do Nginx instalada com:
# nginx -v
Para exibir algo como:
nginx version: nginx/1.14.0 (Ubuntu)
Marque essa versão. Agora vamos executar o comando que vai baixar o código-fonte do PageSpeed e do Nginx. No parâmetro nginx-version troque pela versão do Nginx instalada.
# bash <(curl -f -L -sS https://ngxpagespeed.com/install) --nginx-version 1.14.0
Uma hora será perguntado sobre parâmetro adicionais para ./configure:
About to build nginx. Do you have any additional ./configure arguments you would like to set? For example, if you would like to build nginx with https support give --with-http_ssl_module If you don't have any, just press enter. >
Aqui você pressiona CTRL+C para cancelar porque iremos personalizar mais os parâmetros de ./configure.
Na sua pasta atual você vai perceber que foi adicionado duas pastas: incubator-pagespeed-ngx-latest-stable e nginx-1.14.0.
Para garantir o funcionamento do módulo com a versão já instalada do Nginx iremos precisar copiar os mesmos parâmetros usados na compilação dele:
# nginx -V
Vai exibir algo como:
nginx version: nginx/1.14.0 (Ubuntu) built with OpenSSL 1.1.1 11 Sep 2018 (running with OpenSSL 1.1.1d 10 Sep 2019) TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-GkiujU/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --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 --modules-path=/usr/lib/nginx/modules --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-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
Copie absolutamente tudo depois configure arguments:. Agora vamos entrar na pasta do Nginx com o código-fonte e executar ./configure –add-dynamic-module=/caminho-absoluto-para/incubator-pagespeed-ngx-latest-stable e colar os parâmetros em seguida.
# cd nginx-1.14.0/ # ./configure --add-dynamic-module=/root/incubator-pagespeed-ngx-latest-stable --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-GkiujU/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --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 --modules-path=/usr/lib/nginx/modules --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-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
Haverá a seguinte questão:
You have set --with-debug for building nginx, but precompiled Debug binaries for PSOL, which ngx_pagespeed depends on, aren't available. If you're trying to debug PSOL you need to build it from source. If you just want to run nginx with debug-level logging you can use the Release binaries. Use the available Release binaries? [Y/n]
Responda n e pressione Enter.
Esse processo termina com algo como:
Configuration summary + using threads + using system PCRE library + using system OpenSSL library + using system zlib library nginx path prefix: "/usr/share/nginx" nginx binary file: "/usr/share/nginx/sbin/nginx" nginx modules path: "/usr/lib/nginx/modules" nginx configuration prefix: "/etc/nginx" nginx configuration file: "/etc/nginx/nginx.conf" nginx pid file: "/run/nginx.pid" nginx error log file: "/var/log/nginx/error.log" nginx http access log file: "/var/log/nginx/access.log" nginx http client request body temporary files: "/var/lib/nginx/body" nginx http proxy temporary files: "/var/lib/nginx/proxy" nginx http fastcgi temporary files: "/var/lib/nginx/fastcgi" nginx http uwsgi temporary files: "/var/lib/nginx/uwsgi" nginx http scgi temporary files: "/var/lib/nginx/scgi"
Agora execute o comando para compilar os módulos:
# make modules
Se tudo estiver ocorrido corretamente você vai encontrar o módulo compilado na pasta objs com o nome ngx_pagespeed.so. Use esse comando para dar uma espiada na pasta:
# ls objs/
Copie o módulo para a pasta de módulos do Nginx instalado no sistema (talvez essa pasta seja diferente dependendo da distribuição, de como o Nginx foi configurado, etc):
# cp objs/ngx_pagespeed.so /usr/lib/nginx/modules/
Agora vamos adicionar o PageSpeed na configuração do Nginx usando o editor de texto nano:
# nano /etc/nginx/nginx.conf
Dentro do nano adicione o seguinte código depois de include /etc/nginx/modules-enabled/*.conf;:
load_module "modules/ngx_pagespeed.so";
Use CTRL+O e depois ENTER para salvar o conteúdo. E CTRL+X para sair do nano.
Vamos abrir novamente o nano, já criando uma configuração para o PageSpeed:
# nano /etc/nginx/conf.d/pagespeed.conf
Adicionamos algumas configurações:
pagespeed FetchWithGzip on; pagespeed FileCachePath /run/shm/pagespeed_cache; pagespeed RewriteLevel CoreFilters;
Use CTRL+O e depois ENTER para salvar o conteúdo. E CTRL+X para sair do nano.
Pronto! Vamos reiniciar o Nginx com:
# service nginx restart
Para verificar se o PageSpeed está funcionando abra o site onde o Nginx está rodando com o comando Inspecionar já aberto na aba Network. Clicando na chamada do www.site.com.br, verifique o Response Headers onde deve aparecer algo como x-page-speed: 1.13.35.2-0:

Fonte: https://www.modpagespeed.com/doc/build_ngx_pagespeed_from_source