Home > Web Front-end > JS Tutorial > body text

How to download Nginx configuration file in sections

php中世界最好的语言
Release: 2018-04-11 14:15:08
Original
2849 people have browsed it

This time I will bring you the configuration file of Nginx. How to download the Nginx configuration file in parts? one time.

Html5 provides a new Range tag to implement segmented downloading of files. This tag can be configured in Node.JS

to implement segmented downloading of files.

Header Tag Request Request Header: Download the file content after 3744

range: bytes=3744-
Copy after login

Return Response Header: Total file length 15522643 bytes

accept-ranges': 'bytes'
content-range': 'bytes */15522643'
Copy after login

Nginx configuration First, you need to configure Nginx to support range tag return. It is very simple to add add_header Accept-Ranges bytes; this line.

server {
 listen 80;
 server_name adksdf.com;
 location ~ ^/(img/|js/|css/|upload/|font/|fonts/|res/|icon) {
  add_header Access-Control-Allow-Origin *;
  add_header Accept-Ranges bytes;
  root /var/www/...;
  access_log off;
  expires max;
 }
 ...
}
Copy after login

After enabling, if the node.js side sends

request information

containing range header, nginx will return range-related information: This is a complete Response Header. Note that

content-length

here is not the total length of the file, but the length of the current range. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">{ server: 'nginx',  date: 'Wed, 24 Jan 2018 02:43:20 GMT',  'content-type': 'application/zip',  'content-length': '12420187',  'last-modified': 'Tue, 16 Jan 2018 12:09:47 GMT',  connection: 'close',  etag: '&quot;5a5deb8b-ecdb53&quot;',  expires: 'Thu, 31 Dec 2037 23:55:55 GMT',  'cache-control': 'max-age=315360000',  'access-control-allow-origin': '*',  'accept-ranges': 'bytes',  'content-range': 'bytes 3102456-15522642/15522643' }</pre><div class="contentsignin">Copy after login</div></div> The total size of the file can be obtained based on the content-range in this header.

Node.JS implementation This example first detects half-downloaded files locally, then creates a file stream in 'r' read-write mode, and writes the response stream to the file.

Here, range support will be added to the statement file.

var reqOptions = { url: packageUrl, headers: {} }
var filepath  = '/path/to/your/part/file'
var fileOptions = {}
fs.stat(filepath, function(err, states) {
 if (states) {
  //Range: bytes=3744-
  reqOptions.headers['range'] = 'bytes=' + states.size + '-'
  fileOptions = { start: states.size, flags: 'r+' }
 }
 //创建 http 对象方法
 var reqUrl = reqOptions.url
 var urlObj = url.parse(reqUrl)
 var options = {
   hostname : urlObj.hostname
  , port   : urlObj.port
  , path   : urlObj.pathname
  , headers  : reqOptions.headers || {}
 }
 var req = http.request(options, function(res) {
  var receives  = []
  var err     = null
  var statusCode = res.statusCode
  var headers   = res.headers
  var ws = fs.createWriteStream(filepath, fileOptions)
  ws.on('error', function(e) {
   console.log('ws error', e)
  })
  res.on('data', function(chrunk) {
   ws.write(chrunk)
  })
  res.on('error', function(err) {
   ws.end()
  })
  res.on('end', function() {
   ws.end()
  })
 })
 req.on('error', function(e) {
  cb && cb(e, null, {})
 })
 req.end()
 ...
})
Copy after login

Return Header When requesting nginx, other status codes may be returned, such as 206 or 416, with the following meaning:

206 Partial Content

What is returned is part of the file content

416 Requested Range Not Satisfiable

The requested range exceeds the file size                                                            

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use the on-change attribute in IView

Detailed explanation of the steps for vue to process storejs data acquisition


The above is the detailed content of How to download Nginx configuration file in sections. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!