Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
¿Cómo servir parte de un archivo con Nginx?

Estoy usando X-Accel para servir contenido protegido, usando X-Accel-Redirect.

¿Es posible servir solo una parte del archivo? por ejemplo, el rango de bytes 0-x, o los primeros 5 minutos de un video (mi objetivo final)

Es importante hacerlo en el lado del servidor, para que el cliente no tenga acceso al resto del archivo.

Actualmente así es como envío el archivo completo:

 X-Accel-Cache-Control: no-store, no-cache, must-revalidate Pragma: no-cache Content-Type: application/octet-stream Content-Length: {file_size} Content-Disposition: attachment; filename="myfile.mp4" Accept-Ranges: bytes X-Accel-Buffering: yes X-Accel-Redirect: /protected/myfile.mp4

Conferencia Nginx:

 location /protected { internal; alias /dir/of/protected/files/; if_modified_since off; output_buffers 2 1m; open_file_cache max=50000 inactive=10m; open_file_cache_valid 15m; open_file_cache_min_uses 1; open_file_cache_errors off; }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

El truco masivo sería usar nginx como proxy a sí mismo con un encabezado de Range que limitaría la solicitud a un rango de bytes.

algo como esto (no probado, por lo que probablemente no funcione, pero la idea debería funcionar):

 { ... snip config ... server { listen 80 default_server; listen [::]:80 default_server; root /html; index index.html; location / { proxy_pass http://localhost/content; add_header Range btyes=0,100000; } location /content { try_files $uri $uri/ =404; } } }
over 4 years ago · Santiago Trujillo Report

0

No he probado Slice y X-Accel juntos. Si cada archivo puede tener un límite diferente definido por el backend, puede configurar Slice en la ubicación y enviar el límite con la URL X-Accel-Redirect como se muestra a continuación:

 X-Accel-Cache-Control: no-store, no-cache, must-revalidate Pragma: no-cache Content-Type: application/octet-stream Content-Length: {file_size} Content-Disposition: attachment; filename="myfile.mp4" Accept-Ranges: bytes X-Accel-Buffering: yes X-Accel-Redirect: /protected/myfile.mp4?s=0&e=$PHP_VAR

Nginx.conf

 location /protected { slice; # enable slicing slice_start_arg s; slice_end_arg e; internal; alias /dir/of/protected/files/; if_modified_since off; output_buffers 2 1m; open_file_cache max=50000 inactive=10m; open_file_cache_valid 15m; open_file_cache_min_uses 1; open_file_cache_errors off; }

Un límite de archivo global

Deberá redirigir la solicitud original, incluidos los parámetros de Slice, para truncar el archivo que se está sirviendo.

Conferencia Nginx:

 location /sample { slice; # enable slicing slice_start_arg s; slice_end_arg e; internal; alias /dir/of/protected/files/; if_modified_since off; output_buffers 2 1m; open_file_cache max=50000 inactive=10m; open_file_cache_valid 15m; open_file_cache_min_uses 1; open_file_cache_errors off; } location /protected { rewrite ^ /sample&s=0&e=1024; # replace for the desired file limit in bytes }

Si la directiva de rewrite anterior no funciona, sugiero la siguiente opción usando proxy_pass .

 location /protected { set $file_limit 1024 # replace for the desired file limit in bytes set $delimiter ""; if ($is_args) { set $delimiter "&"; } set $args $args${delimiter}s=0&e=$file_limit; proxy_pass $scheme://127.0.0.1/sample; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!