1. Static resource type
Nginx
as a static resource Web
server deployment configuration, transmission is very High efficiency, often used for static resource processing, request, dynamic and static separation
Files generated by non-server dynamic operation are static resources
Type | Type |
---|---|
Browser rendering
td> | HTML, CSS, JS |
Picture | JPEG, GIF, png |
Video | FLV, Mp4 |
File | TXT , Download any file |
2. Static resource scenario
Minimize the transmission delay of static resource
p>
3. Static resource configuration syntax
1. File reading is efficient sendfile
Syntax: sendfile on | off;
Default: sendfile off;
Context: http, server, location, if in location
2. Improve network transmission efficiencynopush
< /p>
Syntax: tcp_nopush on | off;
Default: tcp_nopush off;
Context: http, server, location
Function: Improve the network when sendfile is turned on The'transmission efficiency' of the package
2. and tcp_nop The corresponding configuration of ush
tcp_nodelay
Syntax: tcp_nodelay on | off;
Default: tcp_nodelay on;
Context: http, server, location
Function: Under the keepalive connection, improve the'real-time performance' of network transmission
4. Static resource file compression
< p>Nginx
can enable the compression function before sending the response message to the client, which can effectively save bandwidth and improve the speed of response to the client.
1.gzip
Compression configuration syntax
Syntax: gzip on | off;
Default: gzip off;
Context: http , server, location, if in location
Function: Transmission compression
2.gzip
Compression ratio configuration syntax
Syntax: gzip_comp_level level;
Default: gzip_comp_level 1;
Context: http, server, location
Function: Compression itself consumes server-side performance
3.< code>gzipCompression protocol version
Syntax: gzip_http_version 1.0 | 1.1;
Default: gzip_http_version 1.1;
Context: http, server, location
Function: Which http protocol is used for compression, mainstream version 1.1
4. Extended compression module
Syntax: gzip_static on | off | always;
Default: gzip_static off ;
Context: http, server, location
Function: Pre-read gzip function
5. Picture compression case
[[emailprotected] conf.d]# mkdir -p /soft/code/images
[[emailprotected] conf.d]# cat static_server.conf
server {
listen 80;
server_name 192.168.56.11;
sendfile on;
access_log /var/log/nginx/static_access.log main;
location ~ .*\.(jpg|gif|png)$ {
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application /json application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
root /soft/code/images;
}
}
gzip
image compression is not enabled
After enabling gzip
to compress images (due to The picture has been compressed before, so the compression ratio is not obvious)
6. File compression case
[[emailprotected] conf.d]# mkdir -p /soft/code/doc
[[email Protected] conf.d]# cat static_server.conf
server {
listen 80;
server_name 192.168.56.11;
sendfile on;
access_log /var/log/nginx/static_access.log main;
location ~ .*\.(txt|xml)$ {
gzip on;
gzip_http_version 1.1;
gzip_comp_level 1;
gzip_types text/plain application/json applicati on/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
root /soft/code/doc;
}
}
gzip
file compression is not enabled
gzip
compressed files is enabled
< h2 id="Static resource browser cache">5. Static resource browser cache
Cache mechanism defined by HTTP protocol (such as: Expires; Cache-control, etc.)
1. Browser without cache
Browser request->No cache->Request WEB server->Request response->Present
2. Browser has cache [ max-age) HTTP1.1
Etag header information verification in the protocol Etag ()
Last-Modified header information verification Last-Modified (specific time)
1. Cache configuration syntax expires
Syntax: expires [modified] time;
expires epoch | max | off;
Default: expires off;
Context: http, server, location, if in location
Function: Add Cache-Control Expires header
2. Configure static resource cache
location ~. *\.(js|css|html)$ {
root /soft/code/js;
expires 1h;
}
location ~ .*\.(jpg|gif|png)$ {
root /soft/code/images;
expires 7d;
}
3. Development When the code is not officially launched, I hope that static files will not be cached
//Cancel static file caching such as js, css, html, etc.
location ~ .*\.(css|js|swf|json|mp4| htm|html)$ {
add_header Cache-Control no-store;
add_header Pragma no-cache;
}
Alibaba Cloud Cache Strategy Help Manual
Nginx Static resource caching
6. Cross-domain access to static resources
Browser prohibits cross-domain access, which is mainly insecure and prone to CSRF
Attack
Nginx
Cross-domain access configuration
Syntax: add_header name value [always];Default: —Context: http, server, location, if in locationAccess-Control-Allow-Origin
1. Prepare html
files
//in www .xuliangwei.com website add cross-access files [[email protected] ~]# cat /soft/code/http_origin.htmlTest ajax and cross-domain access Test cross-domain access
2. Configure Nginx
cross-domain access
//Run www.xuliangwei.com domain name cross-domain access [[email Protected] conf.d]# cat origin.conf server {listen 80; server_name kt.xuliangwei.com; sendfile on; access_log /var/log/nginx/kuayue.log main; location ~ .*\.(html|htm)$ {add_header Access-Control-Allow -Origin https://www.xuliangwei.com; add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS; root /soft/code; })
Not started header
Header access
Start header
header access
7. Static resource anti-theft chain
h2>
Hotlinking refers to displaying content that is not on your own server on your own interface, obtaining the resource address of another person’s server through technical means, bypassing other people’s resource display page, and providing this content to users on your own page, thereby reducing The burden of your own server, because the real space and traffic come from other people’s servers
The idea of anti-leech setting: distinguish which requests are abnormal user requests
Based on http_refer
anti-leech configuration module
Syntax: valid_referers none | blocked | server_names | string ...;Default: —Context: server, location
1. Prepare html file
pachong
2. Start anti-theft link
//Support IP, domain name, regular location ~ .*\.(jpg|gif|png)$ {valid_referers none blocked www.xuliangwei.com ; if ($invalid_referer) {return 403; }root /soft/code/images;}
3. Verification
//Fake protocol header access [[ email protected] ~]# curl -e "http://www.baidu.com" -I /wp-content/uploadshttp:/192.168.69.113/test.jpgHTTP/1.1 403 ForbiddenServer: nginx/1.12.2Date: Tue, 17 Apr 2018 04:55:18 GMTContent-Type: text/htmlContent-Length: 169Connection: keep-alive//Fake protocol header access [[emailprotected] ~]# curl -e "https://www.xuliangwei.com "-I /wp-content/uploadshttp:/192.16 8.69.113/test.jpgHTTP/1.1 200 OKServer: nginx/1.12.2Date: Tue, 17 Apr 2018 04:55:27 GMTContent-Type: image/jpegContent-Length: 174315Last-Modified: Wed, 29 Nov 2017 03:16 :08 GMTConnection: keep-aliveETag: "5a1e2678-2a8eb"Expires: Tue, 17 Apr 2018 16:55:27 GMTCache-Control: max-age=43200Accept-Ranges: bytes
< /p>