Nginx、FastCGI 和 Django 连接被拒绝错误
Posted
技术标签:
【中文标题】Nginx、FastCGI 和 Django 连接被拒绝错误【英文标题】:Nginx, FastCGI and Django connection refused error 【发布时间】:2011-04-25 22:14:59 【问题描述】:我已经设置了 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;
【问题讨论】:
显示应用程序日志而不是 nginx 日志。粘贴你的 nginx 配置。 应用日志是什么意思?抱歉,我对这些东西还是很陌生。我更新了我的帖子以包含我的 nginx 配置。 【参考方案1】:我可以从你的日志中看到,连“/”的连接都被拒绝了,所以这与管理员无关。
尝试使用 telnet 在正确的端口上连接到 localhost,看看是否可以打开连接。我猜这个守护进程没有运行。
您还删除了 python 回溯,这对于确定进程可能崩溃的原因非常有帮助。
【讨论】:
嗨,我添加了整个回溯。没错,当我执行 telnet 时,它返回的连接被拒绝。但我不知道为什么。我的 nginx 配置有问题吗? 不,如果您无法使用 telnet 连接,则没有任何东西可以接受连接。检查您的应用程序是否真的在该端口上运行并侦听。此外,如果应用正在侦听 127.0.0.1,它将只接受来自 localhost 的连接,而不接受来自任何外部主机的连接。 我又查看了回溯,它似乎在尝试将会话保存到数据库时崩溃了。你在用sqlite吗?运行该进程的用户是否有权读写sqlite文件以及在父目录上读取和执行?以上是关于Nginx、FastCGI 和 Django 连接被拒绝错误的主要内容,如果未能解决你的问题,请参考以下文章
更改代码时出现 Django、Nginx、FastCGI 缓存问题
Django @login_required 使用 nginx 和 fastcgi 时导致重定向循环
使用 Nginx/Fastcgi 在子 URL 下部署 django
用于 django 应用程序的 nginx + FastCGI --- 运行两个网络服务器还是一个?