Nginx、FastCGI 和 Django 连接拒绝错误

发布于 2024-09-27 18:31:59 字数 5081 浏览 3 评论 0原文

我已经设置了 nginx、fastcgi 和 django。通过 fastcgi 运行我的 django 项目,使用:

python26 manage.py runfcgi host=127.0.0.1 port=8080 --settings=settings

当我访问 http://127.0.0.1 时,我可以看到正常的 django 404除了管理员之外,我还没有在 django 项目上设置所有内容。所以我尝试访问 http://127.0.0.1/admin ,它返回 502 bad gateway 错误。

我检查了日志,这就是我发现的内容。有谁知道导致此错误的原因是什么?

    2010/10/15 23:09:34 [error] 4796#0: *1 FastCGI sent in stderr: "" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /admin/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:8080", host: "localhost"
2010/10/15 23:09:34 [error] 4796#0: *1 FastCGI sent in stderr: "Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/flup/server/fcgi_base.py", line 574, in run
    protocolStatus, appStatus = self.server.handler(self)
  File "/usr/lib/python2.6/site-packages/flup/server/fcgi_base.py", line 1159, in handler
    result = self.application(environ, start_response)
  File "/usr/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 245, in __call__
    response = middleware_method(request, response)
  File "/usr/lib/python2.6/site-packages/django/contrib/sessions/middleware.py", line 36, in process_response
    request.session.save()
  File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/db.py", line 56, in save
    session_key = self.session_key,
  File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/base.py", line 152, in _get_session_key
    self._session_key = self._get_new_session_key()
  File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/base.py", line 144, in _get_new_session_key
    if not self.exists(session_key):
  File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/db.py", line 29, in exists
    Session.objects.get(session_key=session_key)
  File "/usr/lib/python2.6/site-packages/django/db/models/manager.py", line 132, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 336, in get
    num = len(clone)
  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 81, in __len__
    self._result_cache = list(self.iterator())
  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 269, in iterator
    for row in compiler.results_iter():
  File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 672, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 726, in execute_sql
    cursor =

顺便说一句,我正在运行 nginx07 和 django1.2。

更新:

这是我的 nginx 配置:

    user  apache apache;

worker_processes  4;

error_log /var/log/nginx/error_log info;

events {
    worker_connections  1024;
    use epoll;
}

http {
    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    10m;
    client_body_timeout        10m;
    send_timeout            10m;

    connection_pool_size        256;
    client_header_buffer_size    1k;
    large_client_header_buffers    4 2k;
    request_pool_size            4k;

    gzip on;
    gzip_min_length    1100;
    gzip_buffers    4 8k;
    gzip_types        text/plain;

    output_buffers    1 32k;
    postpone_output    1460;

    sendfile    on;
    tcp_nopush    on;
    tcp_nodelay    on;

    keepalive_timeout    75 20;

    ignore_invalid_headers    on;
    index index.html;

    server {
        listen 80;
        server_name localhost;
        location /site_media  {
            root /media/; # Notice this is the /media folder that we create above
        }
        location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
            access_log   off;
            expires      30d; 
        }
        location / {
            # host and port to fastcgi server
            fastcgi_pass 127.0.0.1:8080;
            fastcgi_param  CONTENT_LENGTH   $content_length;
            fastcgi_param  CONTENT_TYPE     $content_type;
            fastcgi_param  PATH_INFO        $fastcgi_script_name;
            fastcgi_param  QUERY_STRING     $query_string;
            fastcgi_param  REQUEST_METHOD   $request_method;
            fastcgi_param  SERVER_NAME      $server_name;
            fastcgi_param  SERVER_PORT      $server_port;
            fastcgi_param  SERVER_PROTOCOL  $server_protocol;

            fastcgi_pass_header Authorization;
            fastcgi_intercept_errors off;
        }
        access_log    /var/log/nginx/localhost.access_log main;
        error_log    /var/log/nginx/localhost.error_log;
    }
}

I've setup nginx, fastcgi and django. The run my django project through fastcgi using:

python26 manage.py runfcgi host=127.0.0.1 port=8080 --settings=settings

When I visit http://127.0.0.1 I can see the normal django 404 since I haven't setup everything on my django project aside from the admin. So I tried visinting http://127.0.0.1/admin and it returns a 502 bad gateway error.

I checked the logs and here's what I've found. Anybody who knows what's causing this error?

    2010/10/15 23:09:34 [error] 4796#0: *1 FastCGI sent in stderr: "" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /admin/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:8080", host: "localhost"
2010/10/15 23:09:34 [error] 4796#0: *1 FastCGI sent in stderr: "Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/flup/server/fcgi_base.py", line 574, in run
    protocolStatus, appStatus = self.server.handler(self)
  File "/usr/lib/python2.6/site-packages/flup/server/fcgi_base.py", line 1159, in handler
    result = self.application(environ, start_response)
  File "/usr/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 245, in __call__
    response = middleware_method(request, response)
  File "/usr/lib/python2.6/site-packages/django/contrib/sessions/middleware.py", line 36, in process_response
    request.session.save()
  File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/db.py", line 56, in save
    session_key = self.session_key,
  File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/base.py", line 152, in _get_session_key
    self._session_key = self._get_new_session_key()
  File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/base.py", line 144, in _get_new_session_key
    if not self.exists(session_key):
  File "/usr/lib/python2.6/site-packages/django/contrib/sessions/backends/db.py", line 29, in exists
    Session.objects.get(session_key=session_key)
  File "/usr/lib/python2.6/site-packages/django/db/models/manager.py", line 132, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 336, in get
    num = len(clone)
  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 81, in __len__
    self._result_cache = list(self.iterator())
  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 269, in iterator
    for row in compiler.results_iter():
  File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 672, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 726, in execute_sql
    cursor =

BTW I'm running nginx07 and django1.2.

UPDATE:

Here's my nginx configs:

    user  apache apache;

worker_processes  4;

error_log /var/log/nginx/error_log info;

events {
    worker_connections  1024;
    use epoll;
}

http {
    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    10m;
    client_body_timeout        10m;
    send_timeout            10m;

    connection_pool_size        256;
    client_header_buffer_size    1k;
    large_client_header_buffers    4 2k;
    request_pool_size            4k;

    gzip on;
    gzip_min_length    1100;
    gzip_buffers    4 8k;
    gzip_types        text/plain;

    output_buffers    1 32k;
    postpone_output    1460;

    sendfile    on;
    tcp_nopush    on;
    tcp_nodelay    on;

    keepalive_timeout    75 20;

    ignore_invalid_headers    on;
    index index.html;

    server {
        listen 80;
        server_name localhost;
        location /site_media  {
            root /media/; # Notice this is the /media folder that we create above
        }
        location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
            access_log   off;
            expires      30d; 
        }
        location / {
            # host and port to fastcgi server
            fastcgi_pass 127.0.0.1:8080;
            fastcgi_param  CONTENT_LENGTH   $content_length;
            fastcgi_param  CONTENT_TYPE     $content_type;
            fastcgi_param  PATH_INFO        $fastcgi_script_name;
            fastcgi_param  QUERY_STRING     $query_string;
            fastcgi_param  REQUEST_METHOD   $request_method;
            fastcgi_param  SERVER_NAME      $server_name;
            fastcgi_param  SERVER_PORT      $server_port;
            fastcgi_param  SERVER_PROTOCOL  $server_protocol;

            fastcgi_pass_header Authorization;
            fastcgi_intercept_errors off;
        }
        access_log    /var/log/nginx/localhost.access_log main;
        error_log    /var/log/nginx/localhost.error_log;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携余温的黄昏 2024-10-04 18:31:59

我从你的日志中可以看到,即使是“/”的连接也被拒绝,所以这与管理员无关。

尝试使用 telnet 连接到正确端口上的本地主机,看看是否可以打开连接。我猜想守护进程没有运行。

您还删除了 python 回溯,这对于确定进程可能崩溃的原因非常有帮助。

I can see from your logs that even connections for '/' is refused, so this has nothing to do with the admin.

Try using telnet to connect to localhost on the correct port and see if you can open a connection. I would guess that the daemon is not running.

You also removed the python traceback, which would be very helpful in determining why the process might have crashed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文