【问题标题】:Not able to run django with lighttpd无法使用 lighttpd 运行 django
【发布时间】:2020-10-26 06:38:44
【问题描述】:

我正在学习 Django 并将 Lighttpd 配置为 Web 服务器。

我的运行 Django 的 Lighttpd 服务器配置如下所示

server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_redirect",
        "mod_scgi",
        "mod_proxy",
)

server.document-root        = "/var/www/html"
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"
server.pid-file             = "/var/run/lighttpd.pid"
server.username             = "www-data"
server.groupname            = "www-data"
server.port                 = 8899


index-file.names            = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

# default listening port for IPv6 falls back to the IPv4 port
## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

scgi.protocol = "uwsgi"
scgi.server = (
  "/" => (( "host" => "127.0.0.1", "port" => 9000, "check-local" => "disable" )),
)

我创建了 Django 应用程序投票(作为 Django 文档中提到的 Django 教程的一部分) 第一部分是在 polls/view.py 下创建一个简单的视图

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

我按照文档中的说明进行操作,并且可以通过执行以下 uwsgi 命令来获取上述页面

uwsgi --http 127.0.0.1:9000 --chdir ~/new_project/mysite --wsgi-file mysite/wsgi.py 

我得到了预期的网页

但是,当我想从 Lighttpd 网络服务器打开时,问题就出现了 我根据需要将 uwsgi 命令从 uwsgi --http ... 更改为 uwsgi --uwsgi-socket ...

以下是我从浏览器收到的内容

不确定如何继续使其工作?任何帮助将不胜感激

【问题讨论】:

    标签: python django sockets uwsgi lighttpd


    【解决方案1】:

    您上面显示的错误页面来自 Django,因此 lighttpd 成功连接到运行 uwsgi --uwsgi-socket 的后端。

    使用 uwsgi 配置 lighttpd 的“Hello World”示例可以在以下位置找到: https://redmine.lighttpd.net/projects/lighttpd/wiki/HowToPythonWSGI

    您发布的 Python 代码导入 HttpResponse 并返回一个 HttpResponse。

    from django.http import HttpResponse
    
    def index(request):
        return HttpResponse("Hello, world. You're at the polls index.")
    

    HTTP 协议不同于 UWSGI 协议。一个简单的“Hello World”应用程序:

    def application(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/plain;charset=utf-8')])
        return ['Hello World!\n']
    

    【讨论】:

    • 你说得对,lighttpd 已成功连接到运行 uwsgi 的后端,但它没有得到我在使用 uwsgi --http 命令运行时成功收到的投票/网页......我想得到民意调查/或就此而言来自 lighttpd 的任何网页
    • 请重新阅读我的回答,它告诉您 lighttpd 工作正常,并且您的代码不正确。我建议从一个简单的“Hello World”示例开始,并给了你一个链接。
    【解决方案2】:

    您的 Django 报告清楚地表明 polls/ 正在丢失,如果 Django 期望它在 PATH_INFO 中,它是否在 SCRIPT_NAME 中着陆是可能的(并且值得检查)。

    我在 lighttpd 下运行一个 Django 站点,但遇到了 SCRIPT_NAME 和 PATH_INFO 的问题,并编写了一些中间件来解决这个问题。

    你运行 uwsgi 与我有点不同,我在 unix 套接字而不是 http 端口上运行它,我将 lighttp 指向那个套接字。这很可能会有所作为,我不知道。

    但是你可以看看我在这里做了什么:

    https://github.com/bernd-wechner/CoGs/blob/master/django_lighttpd_middleware/__init__.py

    它可能有用,也可能没用。

    【讨论】:

      【解决方案3】:

      我想,我遇到了一个非常相似的问题。修复是微不足道的。但是由于 Django + uWSGI + Lighttpd 的组合似乎有点不寻常(特别是 lighttpd 是这里不常见的部分),所以弄得太费劲了。但是,有一个 nice repo 展示了此设置。特别是,lighttpd.conf 很有帮助。在相关部分 (scgi.server),我发现了这个:

      "fix-root-scriptname" => "enable"
      

      docs 表示(scgi.server 与 fastcgi.server 共享大部分参数):

      "fix-root-scriptname": 将此用于扩展名为 "/" 的后端(以及 check-local 被禁用)。

      由于 django 网址以 / 结尾,这解决了我的问题。我不明白为什么默认情况下没有打开它,我不确定启用它是否有任何缺点。到目前为止,我没有找到任何东西。如果有人可以就这解决问题的原因添加一些见解,我将不胜感激。

      【讨论】:

        猜你喜欢
        • 2019-02-11
        • 2018-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-29
        • 2013-04-04
        • 2012-10-21
        • 2023-02-12
        相关资源
        最近更新 更多