网站搜索

在 CentOS 7 上使用 Ngx_Pagespeed 提高 Nginx 性能


Nginx [engine x] 是一款免费开源、跨平台、功能强大的用 C 语言编写的 Web 服务器和反向代理软件。它也是标准的 IMAP/POP3 和 TCP/UDP 代理服务器,并且可以用作负载平衡器。

Nginx 是一个众所周知的 HTTP 服务器(与 Apache HTTP 服务器相当),为网络上的众多站点提供支持;它以其高性能和稳定性而广受欢迎。

当您想要改善网站上的用户体验时,需要考虑各种重要因素,包括网站速度和页面速度(也称为页面加载时间)。如果您的网站由 Nginx 提供支持,那么您可以使用 ngx_pagespeed 来实现此目的。

建议阅读: 安装 Mod_Pagespeed 以提高 Apache 服务器性能

Ngx_pagespeed 是一个免费开源的 Nginx 模块,用于提高网站速度并缩短页面加载时间。它显着减少了用户查看网站内容并与之交互所需的时间。

ngx_pagespeed 特点:

  • 支持HTTPS和URL控制。
  • 图像优化:剥离元数据、动态调整大小、重新压缩。
  • CSS 和 JavaScript 缩小、串联、内联和大纲。
  • 小资源内联。
  • 推迟图像和 JavaScript 加载。
  • HTML 重写。
  • 缓存生命周期延长。
  • 允许配置多个服务器和许多其他服务器。

在本教程中,我们将展示如何使用 ngx_pagespeed 安装 Nginx 并在 RHEL/CentOS 7 上测试整个安装。

注意:Debian和Ubuntu用户可以按照本指南安装Ngx_Pagespeed以提高Nginx性能。

第 1 步:从源安装 Nginx

1.要使用ngx_pagespeed安装Nginx需要您从源代码编译它。首先像这样从源代码编译 Nginx 所需的所有软件包。

yum install wget gcc cmake unzip gcc-c++ pcre-devel zlib-devel
OR
yum group install "Development Tools"

2. 接下来,使用 wget 命令获取最新版本 Nginx(在撰写本文时为 1.13.2)的源文件,并解压下载的 tar 球,如下所示。

mkdir ~/downloads
cd ~/downloads
wget -c https://nginx.org/download/nginx-1.13.2.tar.gz
tar -xzvf nginx-1.13.2.tar.gz

3.接下来,下载ngx_pagespeed源文件并解压压缩文件。

wget -c https://github.com/pagespeed/ngx_pagespeed/archive/v1.12.34.2-stable.zip
unzip v1.12.34.2-stable.zip

4. 现在进入解压后的 ngx_pagespeed 目录并获取 PageSpeed 优化库来编译 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

步骤 2:使用 Ngx_Pagespeed 配置并编译 Nginx

5. 现在进入 nginx-1.13.2 目录,并通过运行以下命令配置 Nginx 源。

cd ~/downloads/nginx-1.13.2
./configure --add-module=$HOME/downloads/ngx_pagespeed-1.12.34.2-stable/ --user=nobody --group=nobody --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid ${PS_NGX_EXTRA_FLAGS}

6.接下来,编译并安装Nginx,如下所示。

make
make install

7. 安装过程完成后,为 Nginx 创建所有必需的符号链接。

ln -s /usr/local/nginx/conf/ /etc/nginx/
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

步骤3:为SystemD创建Nginx单元文件

8.接下来,由于systemdCentOS 7中的init系统,因此需要手动创建Nginx单元为其归档。

首先,创建文件 /lib/systemd/system/nginx.service,然后获取 NGINX systemd 服务文件,将单元文件配置粘贴到以下文件中。

vi /lib/systemd/system/nginx.service

记下您在配置和编译 Nginx 时设置的 PIDFileNGINX 二进制文件的位置,您将在单元文件中的适当变量中设置它们,如下所示:

[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

保存文件并关闭它。

9. 此时,同时启动 nginx 服务,并通过运行以下命令使其在系统启动时启动。

systemctl start nginx
systemctl enable nginx
systemctl status nginx

步骤 4:使用 Pagespeed 模块配置 Nginx

10.安装了Nginx后,接下来,您需要启用Ngx_pagespeed模块。首先创建一个目录,模块将在其中缓存您网站的文件,并使用以下命令在此目录上设置适当的权限。

mkdir -p /var/ngx_pagespeed_cache
chown -R nobody:nobody /var/ngx_pagespeed_cache

11.现在是时候启用Ngx_pagespeed模块了,打开Nginx配置文件并添加以下行。

重要:如果您在服务器上配置了任何 nginx 虚拟主机,请将上述 pagespeed 指令添加到每个服务器块,以在每个站点上启用 Ngx_pagespeed。

vi /etc/nginx/nginx.conf

在服务器块中添加以下 Ngx_pagespeed 配置行。

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" { }

以下是在默认虚拟主机中启用 Ngx_pagespeed 的 Nginx 配置文件的工作示例。

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/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;
    #    }
    #}

}

12. 然后,通过运行以下命令确保 Nginx 配置文件没有错误,如果一切顺利,您将看到以下输出。

nginx -t

13.最后,重新启动Nginx服务器以使更改生效。

systemctl restart nginx

第 5 步:使用 Ngx_pagespeed 测试 Nginx

14. 要了解 Ngx_pagespeed 现在是否与 Nginx 结合使用,它必须出现在 X-Page-Speed 标头中。

curl -I -p http://localhost

如果您没有看到上述标头,请返回到步骤 11,并仔细按照说明在后续步骤中启用 Ngx-pagespeed。

NGX-pagespeed Github存储库:https://github.com/pagespeed/ngx_pagespeed

就这些!在本教程中,我们展示了如何使用 Ngx_pagespeed 模块安装和配置 Nginx,以优化和提高站点性能并减少页面加载时间。

要保护 Nginx Web 服务器的安全,请阅读本文 – Nginx Web 服务器安全、强化和提高性能的终极指南。

与往常一样,如果您对本教程有任何疑问或想法,请随时告诉我们。