Nginx as a static resource Web service and static resource compression

Source: Internet
Author: User
This article introduces to you about Nginx as a static resource Web service and static resource compression, there is a certain reference value, the need for friends can refer to, I hope to help you.

I. Static resource Web Service

1. Static resource types

type File Type
Browser-Side rendering HTML, CSS, JS
Image JEPG, GIF, PNG
Video FLV, MPEG
File TXT and other download files

2. Static Resource Service Scenario-CDN

Second, the static resource core configuration

1. File Read Sendfile

Sendfile is a highly efficient mode for transferring files.
The Sendfile set to on indicates the mode of initiating efficient transfer of files. Sendfile allows Nginx to transfer data directly between the disk and the TCP socket when transferring files. If this parameter is not turned on, it will first request a buffer in user space (Nginx process space), read the data from disk to the cache with the Read function, and then read from the cache to the buffer of the user space, The Write function is then used to write data from the buffer of the user space to the kernel buffer, and finally to the TCP socket. This parameter allows the data to be used without user buffer.


Grammar

Syntax:        sendfile on | off;default:    sendfile off; Context:    http, server, location, if

2. Tcp_nopush

In the case of sendfile, the transmission efficiency of network packets is improved.
The Tcp_nopush directive enables tcp_cork under the Linux system when connecting sockets. This option tells the TCP stack to append packets and send them when they are full or when the application sends packets by explicitly removing the tcp_cork indication. This makes the data packets sent is the optimal amount, and therefore improves the transmission efficiency of the network packet.
That is tcp_nopush=on, the result is that the packet will not be sent out immediately, wait until the maximum packet, a one-time transmission out, which helps to solve the network congestion, although there is a little delay.

Grammar

Syntax:        Tcp_nopush on | off;default:    tcp_nopush off; Context:    http, server, location

3. Tcp_nodelay

In the KeepAlive connection, the real-time transmission of network packets is improved.
The Tcp_nodelay option is the opposite of Tcp_nopush, and the packet does not wait to be sent to the user in real time.

Grammar

Syntax:        tcp_nodelay on | off;default:    tcp_nodelay off; Context:    Server, location

4. Compression

Turn on compression to speed up resource response while saving network bandwidth resources.

Ngx_http_gzip_module

Grammar

Turn off compression on

Syntax:        gzip on | off;default:    gzip off; Context:    http, server, location, if

Compression level configuration (the higher the compression level, the more server resources are consumed)

Syntax:    gzip_comp_level level;default:    gzip_comp_level 1; Context:    http, server, location

Gzip Protocol version Configuration

Syntax:    gzip_http_version 1.0 | 1.1;default:    gzip_http_version 1.1; Context:    http, server, location

Compression Extension Module

Pre-read gzip feature Ngx_http_gzip_static_module

Syntax:    gzip_static on | off | always;default:    gzip_static off; Context:    http, server, location

The application supports Gunzip compression mode Ngx_http_gunzip_module

Syntax:    gunzip on | off;default:    gunzip off; Context:    http, server, Locationsyntax:    gunzip_buffers number size;default:    gunzip_buffers 4k|16 8k; Context:    http, server, location

Third, static resource compression instance

1. vim/etc/nginx/conf.d/static.conf

server {    #开启sendfile, improve network packet transmission efficiency    sendfile on;    #配置txt |xml Resource path location    ~. *\. Txt|xml) $ {        #开启压缩        gzip on;        Gzip_http_version 1.1;        Gzip_comp_level 1;        Gzip_types Text/plain Application/xml;        Root/vagrant/doc;}    }

2. Nginx-s Reload re-loading the Nginx configuration file

3. Create a/vagrant/doc/a.txt file and view the file size

[root/etc/nginx]# Curl http://www.sina.com.cn/>/vagrant/doc/a.txt  % total    % Received% xferd  Average Speed Time Time-                                 dload  Upload Total   spent    left  Speed100  557k  557k    0     0   488k      0  0:00:01  0:00:01--:--:--  488k[root/etc/nginx]# ll/vagrant/doc/a.txt-rwxrwxrwx 1 vagrant vagrant 558K July  10:57/vagrant/doc/a.txt*

Visible, a.txt file size is 558K

4. Access 192.168.33.88/a.txt via Curl to view HTTP response header information

[root/etc/nginx]# curl-i 192.168.33.88/a.txt-h accept-encoding:gzip,defaltehttp/1.1 okserver:nginx/1.14.0date: Wed, 2018 11:01:43 gmtcontent-type:text/plainlast-modified:wed, Jul 2018 10:57:22 Gmtconnection:keep-aliveeta g:w/"5b45e292-8b47f" Content-encoding:gzip

From the response header information, you can see that the server uses GZIP compression

5. Access the 192.168.33.88/a.txt through the browser and use the developer tools to see the size of the request file

Visible, gzip compressed, request file from 558K compressed to 148K, compression ratio is very high.

6. You can also view the size of the transfer file via the Nginx access.log log.

[root/etc/nginx]# tail/var/log/nginx/access.log192.168.33.1--[11/jul/2018:11:02:46 +0000] "Get/a.txt HTTP/1.1" 200 1 51549 "-" "chrome/67.0.3396.99" "-"

The

can see that the transfer file size is 151549, the unit is B, and the conversion to KB is about 148KB.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.