【问题标题】:nginx file upload freezesnginx文件上传冻结
【发布时间】:2018-06-19 12:25:36
【问题描述】:

我正在测试一个 django 网站部署。当我直接连接到我的 gunicorn localhost 并在调试模式下运行它(以便 django 自己处理文件上传)时,该站点可以正常工作。当我通过 nginx 关闭调试模式访问站点时(它绑定到同一个 gunicorn localhost),除了文件上传之外,一切正常。每当我尝试上传大于 1MB 的文件时,上传都会冻结(对于 1.3MB 的文件,我的浏览器会冻结在 70%)。

我已将 nginx 安装到 conda 虚拟环境 (conda install --no-update-dependencies -c anacoda nginx) 中。这是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;
}

这是我的服务器配置文件的原始版本 (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;
}

我找到了几个相关的帖子:

他们让我介绍了一些修改

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;
}

我也尝试在我的配置文件中设置sendfile off,因为这是推荐用于 Free BSD(Mac OS X 基于 Free BSD),但无济于事。我错过了什么吗?

【问题讨论】:

    标签: django macos nginx


    【解决方案1】:

    看来,我已经想通了。我不得不更改临时目录(我不完全确定原因,因为没有与权限相关的问题)并设置/增加 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"';
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      • 2017-02-03
      • 2014-12-05
      相关资源
      最近更新 更多