带有FastCGI的Django在浏览器中提供404,但可在命令行中使用 [英] Django with FastCGI gives 404 in browser but works on command line

查看:73
本文介绍了带有FastCGI的Django在浏览器中提供404,但可在命令行中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 1.6
Python 2.6
Apache 2.2.27
PostgreSQL 8.4.20
psycopg2
flup

Django 1.6 Python 2.6 Apache 2.2.27 PostgreSQL 8.4.20 psycopg2 flup

通过命令行查看Django站点的FastCGI包装器页面似乎正常,但是我总是得到 404:页面通过浏览器查看我的网站时找不到。我究竟做错了什么?我怀疑这与我的Apache设置有关。

Viewing the FastCGI wrapper page for my Django site via the command line seems to work fine, but I always get a 404: Page Not Found when viewing my site via a browser. What am I doing wrong? I suspect it has to do with my Apache setup.

Django 1.8文档说fastcgi自Django 1.7起不支持该功能,并且我不能使用 mod_wsgi ,因此我正在使用Django 1.6。 Python 2.7和更高版本不可用,因此我正在使用Python 2.6。而且托管公司不会为我安装Python软件包,但我设法自己安装了flup和psycopg2。 (请注意,我必须在Linux64计算机上编译psycopg2并将其上载到服务器。)

The Django 1.8 documentation says fastcgi support is deprecated as of Django 1.7, and I can't use mod_wsgi, so I am using Django 1.6. Python 2.7 and later are unavailable, so I am using Python 2.6. And the hosting company won't install Python packages for me, but I managed to install flup and psycopg2 myself. (Note that I had to compile psycopg2 on my Linux64 machine and upload it to the server.)

Django,flup和psycopg2安装在中/ home / account / public_html / sitename / site-packages / 。我的Django网站位于 / home / account / public_html / sitename / sitename / 。发行 set | grep -e PYTHON -e DJANGO 在命令提示符下不返回任何结果(即,未设置PYTHON或DJANGO环境变量)。

Django, flup, and psycopg2 are installed in /home/account/public_html/sitename/site-packages/. My Django site is located in /home/account/public_html/sitename/sitename/. Issuing set | grep -e PYTHON -e DJANGO at the command prompt returns no results (i.e., no PYTHON or DJANGO environment variables are set).

我正在尝试在共享主机上设置Django。而且在任何人提出建议之前:不幸的是, mod_wsgi 没问题

I am trying to set up Django on shared hosting. And before anyone suggests it: unfortunately, mod_wsgi is out of the question.

我按照这里的指示进行操作: https:// docs。 djangoproject.com/zh-CN/1.6/howto/deployment/fastcgi/#apache-shared-hosting 并以 /home/account/public_html/.htaccess :

I followed the directions here: https://docs.djangoproject.com/en/1.6/howto/deployment/fastcgi/#apache-shared-hosting and ended up with this /home/account/public_html/.htaccess:

AddHandler fastcgi-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ sitename/site.fcgi/$1 [QSA,L]

和这个 /home/account/public_html/sitename/site.fcgi

#!/usr/bin/python
import sys, os

# Add a custom Python path.
sys.path.insert(0, "/home/account/public_html/sitename")
sys.path.insert(0, "/home/account/public_html/sitename/site-packages")

# Switch to the directory of your project. (Optional.)
os.chdir("/home/account/public_html/sitename")

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "sitename.settings"

# From Django
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="prefork", daemonize="false", debug="true")

当我 cd / home / account / public_html / sitename /; ./site.fcgi ,我得到:

WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Status: 200 OK
X-Frame-Options: SAMEORIGIN
Content-Type: text/html; charset=utf-8

<!DOCTYPE html>
<html>
<head>
...etc...

这是我的Django网站的正确索引页。 因此行得通。(我暂时忽略了WSGIServer错误,因为从命令提示符处调用 site.fcgi 显然是在WSGI之外。

which is the correct index page for my Django site. So that works. (I am ignoring the WSGIServer errors for now, because calling site.fcgi from the command prompt is, obviously, outside the WSGI environment.)

但是,当我将浏览器导航到 http://example.com/ 时, 404:找不到页面错误,并且服务器错误日志仅显示:

However, when I navigate my browser to http://example.com/, I get a 404: Page Not Found error, and the server error log shows only:

[error] File does not exist: /home/account/public_html/sitename/site.fcgi/

导航到 http://example.com/valid-page 还会在服务器上生成 404 错误:

Navigating to http://example.com/valid-page also produces a 404, with the server error:

[error] File does not exist: /home/account/public_html/sitename/site.fcgi/valid-page

我在做什么错?还是WSGI如何工作,所以我可以更好地调试它?

What am I doing wrong? Or how does WSGI work, so I can debug this better?


  1. 使用HostGator方法执行 site.fcgi 代替:

# From Django
#from django.core.servers.fastcgi import runfastcgi
#runfastcgi(method="prefork", daemonize="false", debug="true")

# From HostGator
from flup.server.fcgi import WSGIServer
from django.core.wsgi import get_wsgi_application
WSGIServer(get_wsgi_application()).run()

相同的结果: 404 。这是有道理的,因为手动遍历代码表明 From Django 代码只是代码的包装器。

Same result: 404. This makes sense, because manually walking the code shows that the From Django code is just a wrapper for the From HostGator code.

.htaccess fcgid-script 处理程序。 c $ c>而不是 fastcgi-script

Use the fcgid-script handler in .htaccess instead of fastcgi-script:

#AddHandler fastcgi-script .fcgi
AddHandler fcgid-script .fcgi


  • 使用<$我在另一个教程中找到的 .htaccess 中的c $ c> RewriteCond

  • Use a RewriteCond in .htaccess that I found in another tutorial:

    #RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(sitename/site.fcgi)
    RewriteRule ^(.*)$ sitename/site.fcgi/$1 [QSA,L]
    


  • 以命令提示符方式传递WSGI URI参数(注意空格而不是 / ):

    #RewriteRule ^(.*)$ sitename/site.fcgi/$1 [QSA,L]
    RewriteRule ^(.*)$ sitename/site.fcgi $1 [QSA,L]
    


  • 确保 R设置了EQUEST_URI 环境变量:

    SetEnv REQUEST_URI %{REQUEST_URI}
    


  • 即使将 site.fcgi 用作CGI脚本,也只是看到如果我可以从命令提示符下重现运行 site.fcgi 的成功(可能不是永久解决方案):

  • Even make site.fcgi a CGI script, just to see if I could reproduce the success of running site.fcgi from the command prompt (probably not a permanent solution):

    #AddHandler fastcgi-script .fcgi
    #RewriteEngine On
    #RewriteCond %{REQUEST_FILENAME} !-f
    #RewriteRule ^(.*)$ sitename/site.fcgi/$1 [QSA,L]
    Options -Indexes +ExecCGI
    AddHandler cgi-script .fcgi
    

    ,然后导航到 http://example.com/sitename/site.fcgi

    以上任何一种变体均无用,其中的几种变体也不起作用。他们中的许多人返回了 404:找不到页面,还有一些返回了更明显的错误,而其他人只是显示了 site.fcgi

    None of the above variations worked, nor did many combinations of several of them. Many of them returned 404: Page Not Found, and some returned more glaring errors, while others just displayed the source of site.fcgi.


    1. 我也得到了相同的结果/与Arvixe和LunarPages共享托管的类似结果。上面的描述只使用了我的Arvixe帐户,因为我的LunarPages帐户没有SSH访问权限,因此更难以调试。

    1. I am getting the same/similar results for shared hosting with both Arvixe and LunarPages. The above description is using only my Arvixe account, because my LunarPages account does not have SSH access and is therefore harder to debug.

    我什至愿意低调一点成本的替代托管计划(例如,其他托管公司),可使该计划有效。这只是一个个人网站,不会产生任何收入,所以我不想花费Arvixe和LunarPages要求专门托管的〜$ 20 / month,以便拥有 mod_wsgi 访问。

    I'd even be willing to entertain low-cost alternative hosting plans (e.g., other hosting companies) that would enable this to work. This is just a personal website, not generating any money, so I don't want to spend the ~$20/month that Arvixe and LunarPages are asking to have dedicated hosting in order to have mod_wsgi access.

    在此先感谢您的帮助或建议!

    推荐答案

    我遇到了同样的问题(发布在此),最终确定这是由于对我通过bluehost订阅的共享托管帐户。我切换了dreamhost共享托管计划,并且立即启动并运行。

    I ran into the same problem (posted here) and ended up determining that it was due to restrictions on the shared hosting account I was subscribed to with bluehost. I switched a dreamhost shared hosting plan and was up and running in no time.

    这篇关于带有FastCGI的Django在浏览器中提供404,但可在命令行中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆