le téléchargement de fichier nginx se fige

Je teste un déploiement de site Web Django. Le site fonctionne sans aucun problème lorsque je me connecte directement à mon gunicorn localhost et l’exécute en mode débogage (pour que django gère lui-même le téléchargement des fichiers). Lorsque j’accède au site avec le mode débogage désactivé via nginx (il se lie au même localhost gunicorn), tout fonctionne aussi bien, à l’exception des téléchargements de fichiers. Chaque fois que j’essaie de télécharger un fichier> 1 Mo, le téléchargement se fige à un moment donné (avec un fichier de 1,3 Mo, mon navigateur se bloque à 70%).

J’ai installé nginx dans un environnement virtuel de conda ( conda install --no-update-dependencies -c anacoda nginx ). Voici le fichier etc/nginx.conf :

 # nginx Configuration File # https://www.nginx.com/resources/wiki/start/topics/examples/full/ # http://nginx.org/en/docs/dirindex.html # https://www.nginx.com/resources/wiki/start/ # Run as a unique, less privileged user for security. # user nginx www-data; ## Default: nobody # If using supervisord init system, do not run in deamon mode. # Bear in mind that non-stop upgrade is not an option with "daemon off". # daemon off; # Sets the worker threads to the number of CPU cores available in the system # for best performance. # Should be > the number of CPU cores. # Maximum number of connections = worker_processes * worker_connections worker_processes auto; ## Default: 1 # Maximum number of open files per worker process. # Should be > worker_connections. # http://blog.martinfjordvald.com/2011/04/optimizing-nginx-for-high-traffic-loads/ # http://stackoverflow.com/a/8217856/2127762 # Each connection needs a filehandle (or 2 if you are proxying). worker_rlimit_nofile 8192; events { # If you need more connections than this, you start optimizing your OS. # That's probably the point at which you hire people who are smarter than # you as this is *a lot* of requests. # Should be < worker_rlimit_nofile. worker_connections 8000; } # Log errors and warnings to this file # This is only used when you don't override it on a server{} level #error_log logs/error.log notice; #error_log logs/error.log info; error_log var/log/nginx/error.log warn; # The file storing the process ID of the main process pid var/run/nginx.pid; http { # Log access to this file # This is only used when you don't override it on a server{} level access_log var/log/nginx/access.log; # Hide nginx version information. server_tokens off; # Controls the maximum length of a virtual host entry (ie the length # of the domain name). server_names_hash_bucket_size 64; # Specify MIME types for files. include mime.types; default_type application/octet-stream; # How long to allow each connection to stay idle. # Longer values are better for each individual client, particularly for SSL, # but means that worker connections are tied up longer. keepalive_timeout 20s; # Speed up file transfers by using sendfile() to copy directly # between descriptors rather than using read()/write(). # For performance reasons, on FreeBSD systems w/ ZFS # this option should be disabled as ZFS's ARC caches # frequently used files in RAM by default. sendfile on; # Don't send out partial frames; this increases throughput # since TCP frames are filled up before being sent out. tcp_nopush on; # Enable gzip compression. gzip on; # Compression level (1-9). # 5 is a perfect compromise between size and CPU usage, offering about # 75% reduction for most ASCII files (almost identical to level 9). gzip_comp_level 5; # Don't compress anything that's already small and unlikely to shrink much # if at all (the default is 20 bytes, which is bad as that usually leads to # larger files after gzipping). gzip_min_length 256; # Compress data even for clients that are connecting to us via proxies, # identified by the "Via" header (required for CloudFront). gzip_proxied any; # Tell proxies to cache both the gzipped and regular version of a resource # whenever the client's Accept-Encoding capabilities header varies; # Avoids the issue where a non-gzip capable client (which is extremely rare # today) would display gibberish if their proxy gave them the gzipped version. gzip_vary on; # Compress all output labeled with one of the following MIME-types. gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy; # text/html is always compressed by gzip module # This should be turned on if you are going to have pre-compressed copies (.gz) of # static files available. If not it should be left off as it will cause extra I/O # for the check. It is best if you enable this in a location{} block for # a specific directory, or on an individual server{} level. # gzip_static on; include conf.d/*.conf; } 

Ceci est la version originale du fichier de configuration de mon serveur ( conf.d/test.conf ).

 server { server_name localhost; listen 8081; access_log on; client_max_body_size 32M; send_timeout 100s; location /static/ { alias /Users/user/static/; autoindex on; error_log /Users/user/.nginx/labsite.static.error.log warn; } location /media/ { alias /Users/user/media/; autoindex on; error_log /Users/user/.nginx/labsite.media.error.log warn; } location / { proxy_pass http://localhost:8001; proxy_set_header X-Forwarded-Host $server_name; proxy_set_header X-Real-IP $remote_addr; add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"'; } access_log /Users/user/.nginx/labsite.access.log combined; error_log /Users/user/.nginx/labsit.error.log warn; } 

J’ai trouvé plusieurs articles liés:

  • Nginx PHP échoue avec les téléchargements de fichiers volumineux (plus de 6 Go)
  • https://serverfault.com/questions/626817/nginx-file-upload-pauses-stalls-in-the-middle-uploads-only-258kb-and-stops
  • https://easyengine.io/tutorials/php/increase-file-upload-size-limit/

Ils m’ont amené à introduire des modifications

 server { server_name localhost; listen 8081; access_log on; client_max_body_size 32M; send_timeout 300s; gzip_static off; location /static/ { alias /Users/user/static/; autoindex on; error_log /Users/user/.nginx/labsite.static.error.log warn; } location /media/ { alias /Users/user/media/; client_body_temp_path /Users/user/media; autoindex on; error_log /Users/user/.nginx/labsite.media.error.log warn; } location / { proxy_pass http://localhost:8001; proxy_set_header X-Forwarded-Host $server_name; proxy_set_header X-Real-IP $remote_addr; add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"'; } access_log /Users/user/.nginx/labsite.access.log combined; error_log /Users/user/.nginx/labsit.error.log warn; } 

J’ai également essayé de sendfile off dans mon fichier de configuration, car cela est recommandé pour Free BSD (et Mac OS X est basé sur Free BSD), mais en vain. Est-ce que je manque quelque chose?

On dirait, j’ai compris ça. J’ai dû changer le répertoire temporaire (je ne sais pas trop pourquoi, car il n’y avait pas de problèmes liés aux permissions) et définir / augmenter le paramètre client_body_timeout .

 server { listen 8081; server_name localhost; client_max_body_size 32M; client_body_timeout 300s; send_timeout 300s; client_body_temp_path /Users/user/media; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /Users/user; } location /media/ { root /Users/user; } location / { proxy_pass http://localhost:8001; proxy_set_header X-Forwarded-Host $server_name; proxy_set_header X-Real-IP $remote_addr; add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"'; } }