Nginx + fastcgi 截断问题

11 投票
6 回答
6125 浏览
提问于 2025-04-11 09:25

我正在用Django搭建一个网站,并通过fastcgi接口连接nginx。不过,有些页面显示不完整(也就是说,页面的源代码在某个地方就停止了,有时甚至是在一个标签的中间)。我该怎么解决这个问题呢?如果需要更多信息,请告诉我,我会补充上去。

具体情况:

我正在使用flup,并用以下命令启动fastcgi服务器:

python ./manage.py runfcgi umask=000 maxchildren=5 maxspare=1 minspare=0 method=prefork socket=/path/to/runfiles/django.sock pidfile=/path/to/runfiles/django.pid

nginx的配置如下:

# search and replace this: {project_location}
pid /path/to/runfiles/nginx.pid;
worker_processes  2;
error_log /path/to/runfiles/error_log;
events {
    worker_connections  1024;
    use epoll;
}
http {
    # default nginx location
    include        /etc/nginx/mime.types;
    default_type    application/octet-stream;
    log_format main
        '$remote_addr - $remote_user [$time_local] '
            '"$request" $status $bytes_sent '
        '"$http_referer" "$http_user_agent" '
        '"$gzip_ratio"';
    client_header_timeout  3m;
    client_body_timeout    3m;
    send_timeout           3m;
    connection_pool_size        256;
    client_header_buffer_size    1k;
    large_client_header_buffers    4 2k;
    request_pool_size        4k;
    output_buffers   4 32k;
    postpone_output  1460;
    sendfile        on;
    tcp_nopush             on;
    keepalive_timeout      75 20;
    tcp_nodelay            on;
    client_max_body_size       10m;
    client_body_buffer_size    256k;
    proxy_connect_timeout      90;
    proxy_send_timeout         90;
    proxy_read_timeout         90;
    client_body_temp_path      /path/to/runfiles/client_body_temp;
    proxy_temp_path            /path/to/runfiles/proxy_temp;
    fastcgi_temp_path            /path/to/runfiles/fastcgi_temp;
    gzip on;
    gzip_min_length  1100;
    gzip_buffers     4 32k;
    gzip_types       text/plain text/html application/x-javascript text/xml text/css;
    ignore_invalid_headers    on;
    server {
        listen 80;
        server_name alpha2.sonyalabs.com;
        index index.html;
        root   /path/to/django-root/static;
        # static resources
        location ~* ^/static/.*$
        {
        root   /path/to/django-root;
                expires 30d;
                break;
        }
        location / {
            # host and port to fastcgi server
            fastcgi_pass unix:/path/to/runfiles/django.sock;
            fastcgi_param PATH_INFO $fastcgi_script_name;
            fastcgi_param REQUEST_METHOD $request_method;
            fastcgi_param QUERY_STRING $query_string;
            fastcgi_param CONTENT_TYPE $content_type;
            fastcgi_param CONTENT_LENGTH $content_length;
            fastcgi_pass_header Authorization;
            fastcgi_intercept_errors off;
        }
        location /403.html {
                root   /usr/local/nginx;
                access_log   off;
        }
        location /401.html {
                root   /usr/local/nginx;
                access_log   off;
        }
        location /404.html {
                root   /usr/local/nginx;
                access_log   off;
        }
        location = /_.gif {
                    empty_gif;
                access_log   off;
        }
            access_log    /path/to/runfiles/localhost.access_log main;
            error_log    /path/to/runfiles/localhost.error_log;
        }
}

6 个回答

3

你在使用什么样的fastcgi接口?是flup吗?如果是的话,请把你启动服务器的方式和它是如何连接到nginx的步骤发过来。没有这些信息,我们只能猜测可能出错的地方。

可能出现的问题:

  • nginx可能有bug。至少lighttpd有很多糟糕的fastcgi bug,我不奇怪nginx也有一些问题 :)
  • Django在内部系统中出现了错误,导致fastcgi服务器关闭,而你在客户端看不到这个错误。在这种情况下,可以把fastcgi服务器的调用包裹起来,使用try/except来打印出异常信息。

不过,服务器的日志和配置文件会很有帮助。

6

查看你的错误日志,看看有没有“权限被拒绝”的错误,特别是在写入.../nginx/tmp/...文件时。Nginx在正常情况下是可以正常工作的,但如果需要临时空间,就可能会出问题,这种情况通常发生在32K的边界上。如果你发现了这些错误,确保tmp目录对运行Nginx的用户是可写的。

8

我在用nginx运行Nagios的时候也遇到了同样的问题。我在网上搜索解决办法时,看到你的问题,读到一些关于“权限被拒绝”的回答时,我突然想到了一些东西(也许对你有帮助):

  • nginx的错误日志里显示:

    2011/03/07 11:36:02 [严重] 30977#0: *225952 打开() "/var/lib/nginx/fastcgi/2/65/0000002652" 失败 (13: 权限被拒绝)

  • 所以我就执行了这个命令:# chown -R www-data:www-data /var/lib/nginx/fastcgi

  • 问题解决了!(也谢谢你间接的帮助)

撰写回答