Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Nginx static Resources

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the use of Nginx static resources in detail for everyone, how Nginx static resources compress files and add anti-theft chains, zero basis can also refer to this article, interested small partners can refer to it.

1) Static resource types

Nginx as a static resource web server deployment configuration, transmission is very efficient, often used for static resource processing, requests, static separation!

Non-server dynamic run generated files belong to static resources!

Type Browser-side rendering HTML, CSS, JS image JPEG, GIF, PNG video FLV, MP4 file TXT, arbitrary download file 2) Static resource scene

Static resource transmission delay minimized!

As shown in the figure:

3) Static resource configuration syntax 1) Efficient file reading-->sendfileSyntax: sendfile on| off ;Default: sendfile off ;Context: http, server, location, if in location ;2) Improve network transmission efficiency-->nopushSyntax: tcp_nopush on| off ;Default: tcp_nopush off ;Context: http, server, location ; Role: sendfile is enabled, improve the packet's 'transmission efficiency'; 3) Configuration corresponding to tcp_nopush tcp_nodelaySyntax: tcp_nodelay on| off ;Default: tcp_nodelay on ;Context:http, server, location ; action: under keepalive connection, provide network transmission 'real-time'; 4) static resource file compression

Nginx can enable compression before sending the response to the client, which can effectively save bandwidth and improve the speed of the response to the client.

1) gzip Compression Configuration Syntax: gzip on | off ;Default: gzip off ;Context: http, server, location, if in location ; action: transmission compression;2) gzip compression ratio configuration syntax: gzip_comp_level ;Default: gzip_comp_level 1 ;Context: http, server, location ; action: compression itself costs more server performance;3) gzip compression protocol version Syntax: gzip_http_version 1.0 | 1.1 ;Default: gzip_http_version 1.1 ;Context: http, server, location ; action: compression use http which protocol, mainstream version 1.1 ;4) expansion compression module Syntax: gzip_static on| off | always ;Default: gzip_static off ;Context: http, server, location ; action: pre-read gzip function;5) image compression case [root@nginx ~]# mkdir -p /soft/code/images[root@nginx ~]# vim /etc/nginx/conf.d/static_server.confserver { listen 80; server_name 192.168.1.10; 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 app lication/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png; root /soft/code/images; }}[root@nginx ~]# nginx -t[root@nginx ~]# nginx -s reload

Because the compression effect of the picture is not too good, so here only attached configuration!

6) File compression case [root@nginx ~]# mkdir -p /soft/code/doc[root@nginx ~]# vim /etc/nginx/conf.d/static_server.confserver { listen 80; server_name 192.168.1.10; sendfile on; access_log /var/log/nginx/static_access.log main; location ~ .*\. (txt|xml)$ { gzip on; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/json application/x-javascript app lication/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png; root /soft/code/doc; }}[root@nginx ~]# nginx -t[root@nginx ~]# nginx -s reload 1) gzip file compression is not enabled

2) Enable gzip file compression

5) Static Resource Browser Cache

HTTP protocol defined caching mechanism (e.g. Expires; Cache-control, etc.)

1) No cache in browser

Browser Request--> No Cache--> Request Web Server--> Request Response--> Rendering

2) Browser cache

browser request-> cache-> check expiration-> update-> render check Expires HTTP1.0, Cache-Control(max-age) HTTP1.1 protocol Etag header check Etag ()Last-Modified header check Last-Modified (specific time)1) cache configuration syntax expires Syntax: expires [modified] time ;Default: expires off ;Context: http, server, location, if in location ; action: add Cache-Control Expires header;2) configure static resource cache [root@nginx conf.d]# vim static_server.conf server { listen 80; server_name 192.168.1.10; sendfile on; access_log /var/log/nginx/static_access.log main; location ~ .*\. (txt|js|css|html)$ { root /soft/code/doc; expires 1h; } location ~ .*\. (jpg|gif|png)$ { root /soft/code/images; expires 7d; }}[root@nginx conf.d]# nginx -t[root@nginx conf.d]# nginx -s reload

Browser test effect:

3) Configuration static files are not cached [root@nginx conf.d]# vim static_server.confserver { listen 80; server_name 192.168.1.10; sendfile on; access_log /var/log/nginx/static_access.log main; location ~ .*\. (txt|js|css|html)$ { root /soft/code/doc; add_header Cache-Control no-store; add_header Pragma no-cache; } location ~ .*\. (jpg|gif|png)$ { root /soft/code/images; expires 7d; }}[root@nginx conf.d]# nginx -t[root@nginx conf.d]# nginx -s reload

Browser test effect:

6) Static resource anti-theft chain

Chain theft refers to displaying content that is not on your own server in your own world, obtaining the resource address of his server through technical segments, bypassing other resource display channels, and providing this content to users in your own world, thereby reducing the burden on your own server, because real space and traffic come from your own server;

Anti-theft chain setting ideas: distinguish which requests are normal user requests;

http_refer anti-theft chain configuration module:

Syntax: valid_referers none | blocked | server_names | string ...; Default: -Context: server, location

Another nginx server (initial case), write html file:

[root@nginx02 conf.d]# vim /usr/share/nginx/html/dl.html pachong

[root@nginx02 conf.d]# nginx -t [root@nginx02 conf.d]# nginx -s reload

First service normal access:

The second child server steals pictures of the first server:

2) Enable anti-theft chain [root@nginx conf.d]# vim static.confserver { listen 80; server_name 192.168.1.10; valid_referers none blocked 192.168.1.10; location ~ .*\. (jpg|gif|png)$ { if ($invalid_referer) { return 403; break; } root /soft/code/images; }}[root@nginx conf.d]# nginx -t[root@nginx conf.d]# nginx -s reload

Visit the second server again:

About Nginx static resource use method to share here, I hope the above content can have some help for everyone, you can learn more knowledge. If you like this post, share it with more people.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report