Jiri Ngx_Pagespeed rụọ ọrụ Nginx na CentOS 7


Nginx [engine x] bụ free na-emeghe-isi iyi, cross-platform, ike webụsaịtị na reverse proxy software nke edere na C. Ọ bụkwa ọkọlọtọ IMAP/POP3 na TCP/UDP proxy nkesa, na enwere ike iji ya dị ka ibu. onye nhazi.

Nginx bụ sava HTTP amaara nke ọma (tụnyere sava Apache HTTP) na-enye ike ọtụtụ saịtị na webụ; ọ na-ewu ewu maka ịrụ ọrụ dị elu na nkwụsi ike ya.

Enwere ihe dị iche iche dị mkpa ị ga-atụle mgbe ịchọrọ ịkwalite ahụmịhe onye ọrụ na saịtị gị, gụnyere ọsọ saịtị na ọsọ ibe (nke a makwaara dị ka oge ibu ibe). Ọ bụrụ na Nginx kwadoro saịtị gị, ị nwere ike iji ngx_pagespeed maka ebumnuche a.

Ngx_pagespeed bụ Nginx modul na mepere emepe ejiri maka ịkwalite ọsọ saịtị yana iwetulata oge ibu ibe. Ọ na-ebelata nke ọma oge ọ na-ewe maka ndị ọrụ ịhụ na imekọrịta ọdịnaya saịtị gị.

  • Nkwado HTTPS na njikwa URL.
  • Njikwaa onyonyo: iwepụ meta-data, ngbanwe dị ike, mkpakọ.
  • CSS na mbelata Javascript, njikọ, ntinye, na nkọwapụta.
  • Obere ntinye akụrụngwa.
  • Na-ebufe onyonyo na ntinye Javascript.
  • HTML na-edegharị.
  • Cache ndọtị ogologo ndụ.
  • Na-enye ohere ịhazi maka ọtụtụ sava yana ọtụtụ ndị ọzọ.

N'ime nkuzi a, anyị ga-egosi otu esi etinye Nginx na ngx_pagespeed wee nwalee nrụnye niile na RHEL/CentOS 7.

Ntị: Ndị ọrụ Debian na Ubuntu nwere ike soro ntuziaka a ka Wụnye Ngx_Pagespeed iji melite arụmọrụ Nginx.

Kwụpụ 1: Wụnye Nginx site na Isi mmalite

1. Iji tinye Nginx na ngx_pagespeed chọrọ ka ị chịkọta ya site na isi mmalite. Buru ụzọ wụnye ngwugwu niile achọrọ maka ịchịkọta Nginx site na isi mmalite dị ka nke a.

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

2. Na-esote, nweta faịlụ isi mmalite nke Nginx kachasị ọhụrụ (1.13.2 n'oge ederede) site na iji iwu wget wee wepụ bọọlụ tar ebudatara dị ka n'okpuru.

# 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. Ọzọ, budata faịlụ isi mmalite ngx_pagespeed wee bupụ faịlụ abịakọrọ.

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

4. Ugbu a banye n'ime akwụkwọ ndekọ aha ngx_pagespeed na-emepe emepe wee nweta ọba akwụkwọ nkwalite PageSpeed iji kpokọta Nginx dị ka ndị a.

# 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

Kwụpụ 2: Hazie wee chịkọta Nginx na Ngx_Pagespeed

5. Ugbu a banye nginx-1.13.2 ndekọ, ma hazie Nginx isi iyi site na-agba ọsọ iwu n'okpuru.

# 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. Ọzọ, chịkọta ma wụnye Nginx dị ka ndị a.

# make
# make install

7. Mgbe usoro nrụnye zuru ezu, mepụta symlinks niile dị mkpa maka Nginx.

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

Kwụpụ 3: Mepụta Nginx Unit File maka SystemD

8. Na-esote, ebe ọ bụ na systemd bụ usoro init na CentOS 7, ịkwesịrị iji aka mepụta faịlụ Nginx maka ya.

Fisrt, mepụta faịlụ /lib/systemd/system/nginx.service, wee jide faịlụ NGINX systemd faịlụ mado nhazi faịlụ otu n'ime faịlụ dị n'okpuru.

# vi /lib/systemd/system/nginx.service

Rịba ama ebe PIDFile na ọnụọgụ abụọ NGINX nke ị debere mgbe ị na-ahazi ma na-achịkọta Nginx, ị ga-edobe ha na mgbanwe kwesịrị ekwesị na faịlụ otu dị ka egosiri n'okpuru:

[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

Chekwaa faịlụ ma mechie ya.

9. N'ebe a, malite ọrụ nginx maka oge ọ bụla ma mee ka ọ malite na usoro buut site na ịme iwu ndị dị n'okpuru.

# systemctl start nginx
# systemctl enable nginx
# systemctl status nginx

Kwụpụ 4: Hazie Nginx na Module Ọsọ ọsọ

10. Na Nginx arụnyere, na-esote, ị ga-eme ka Ngx_pagespeed modul. Malite site na ịmepụta ndekọ ebe modul ga-echekwa faịlụ maka weebụsaịtị gị wee debe ikike kwesịrị ekwesị na ndekọ a na iwu dị n'okpuru.

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

11. Ugbu a ọ bụ oge iji mee ka Ngx_pagespeed modul, mepee faịlụ nhazi Nginx ma tinye ahịrị ndị dị n'okpuru.

Ihe dị mkpa: Ọ bụrụ na ị haziela nginx mebere ndị ọbịa na sava ahụ, gbakwunye ntuziaka ngwa ngwa dị n'elu na ngọngọ nkesa ọ bụla iji mee ka Ngx_pagespeed na saịtị ọ bụla.

# vi /etc/nginx/nginx.conf

Tinye usoro nhazi Ngx_pagespeed n'ime ngọngọ nkesa.

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

Ihe na-esonụ bụ ihe atụ na-arụ ọrụ nke faịlụ nhazi Nginx nwere Ngx_pagespeed enyere aka na onye nnabata nke ndabara.

#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. E mesịa, hụ na Nginx config faịlụ bụ njehie free site na-agba ọsọ iwu n'okpuru, ị ga-ahụ mmepụta n'okpuru ma ọ bụrụ na ihe niile dị mma.

# nginx -t

13. N'ikpeazụ, malitegharịa Nginx nkesa maka mgbanwe iji mee ihe.

# systemctl restart nginx

Kwụpụ 5: Jiri Ngx_pagespeed nwale Nginx

14. Iji mara ma Ngx_pagespeed na-arụ ọrụ ugbu a na njikọ Nginx, ọ ga-apụta na X-Page-Speed nkụnye eji isi mee.

# curl -I -p http://localhost

Ọ bụrụ na ịhụbeghị nkụnye eji isi mee dị n'elu, laghachi azụ na nzọụkwụ 11 wee soro ntuziaka a iji mee ka Ngx-pagespeed jiri usoro ndị na-esote.

Ngx-pagespeed Github ebe nchekwa: https://github.com/pagespeed/ngx_pagespeed

Nke ahụ bụ naanị! N'ime nkuzi a, anyị gosiri otu esi etinye na hazie Nginx na Ngx_pagespeed modul iji bulie ma melite arụmọrụ saịtị ma belata oge ibu ibe.

Iji chekwaa sava weebụ Nginx, gụọ akụkọ a - Ntuziaka kacha mma maka nchekwa, sie ike na kwalite arụmọrụ nke sava weebụ Nginx.

Dị ka oge niile, egbula oge ime ka anyị mata ajụjụ ọ bụla ma ọ bụ echiche gbasara nkuzi a.