Enterprise Section - --- Web Page Compression under nginx

Experimental environment

We write index.html in/usr/local/nginx/html on a virtual machine with nginx configured. We can access the web successfully and the size of the web page is more than 200 k

configuration file

Nginx compresses web pages and needs to modify niginx's configuration file.Let's first look at what nginx's configuration file contains:

#1. Define the user and user group that Nginx runs as nginx, which means that the worker's work control group is the nginx user, in order to ensure the security of the system.
user  nginx nginx;

#2. Number of processes, which is the process of processing requests (known as the receptionist), can be initially set to the total number of cpu cores such as worker_processes 8;
worker_processes  2;

#3. Global error log definition type
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#4. Log the process number to a file to manage nginx processes
#pid        logs/nginx.pid;

#5.The maximum number of open files in Nginx's event model can be set to the result of ulimit-HSn after system optimization.
//That is, 1024 can be changed to 65535 and then modified in the / etc/security/limits.conf file, which is the default number of connections for nginx.
//Just modify it in this file
events {
    worker_connections  1024;      #Maximum number of connections for nginx = number of worker connections * number of worker processes
}

#6.Setup section of HTTP module
http {
    include       mime.types;    #File extension and file type mapping table
    default_type  application/octet-stream;   #Default file type

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '      #Define logging format for logs
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;    #Turn on efficient file transfer mode for kernel zero copy
    #tcp_nopush     on;       #Activating the tcp_nopush parameter allows httpresponseheader and file start to be published in one file, reducing the number of network message segments

    #keepalive_timeout  0;   #Connection timeout in seconds
    keepalive_timeout  65;

    #gzip  on;    #gzip compression is extremely important for site optimization

#7. Set up the domain name-based virtual host section
    server {
        listen       80;    #The port to listen on can also be in the form of 172.25.254.1:80
        server_name  localhost;   #domain name

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {      #location Tag Segment for Default Access
            root   html;   #Site root directory, which is the directory where the site program resides
            index  index.html index.htm;      #Sort First Page
        }

        #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$ {     #Requests that match the php extension are dispatched to the fcgi server
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;     #Port 9000 thrown to this machine (php fastcgi server)
        #    fastcgi_index  index.php;    #Set Dynamic Home Page
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;     #Set parameters to interact with fastcgi to include files
        #}

        # 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;
    #    }
    #}

#8.http Virtual Host
    # 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;
    #    }
    #}

}

Start Compression

Let's look at the web page to see what compression it supports.Page Type

I wrote a web page that supports gzip, so I modified the content of the gzip section in the configuration file

gzip on; #Turn on gzip compression 
gzip_min_length 1; #Minimum compressed file is 0.1k 
gzip_comp_level 3; #Compression Level 3
gzip_types text/plain appliaton/x-javascript text/css application/xml  text/javascripts  application/x-httpd-php  image/jpeg imgae/gif image/png; #Compressed file type

Then check if the syntax of the profile I wrote is correct

/usr/local/nginx/sbin/nginx    -t

When it shows is ok, success means there are no grammar errors, if there are any, it will tell you exactly where the errors are

Then restart the service

Both systemctl restart nginx and systemctl reload nginx can be restarted

Access on the Web

Here's a reminder: when you revisit a web page, you must clear the previous cache, or you won't see any effect.

Keywords: Operation & Maintenance Nginx PHP Apache SSL

Added by flash-genie on Tue, 30 Jul 2019 20:44:14 +0300