fastcgi 不提供 css 和图像的 lighttpd 上的烧瓶

flask on lighttpd with fastcgi not serving css and images

我正在尝试使用 lighttpd 和 Fast-CGI 部署 flask 应用程序。在启动 lig​​httpd 服务器时,HTML 页面在浏览器上呈现,但相应的 CSS 和图像未由 lighttpd 提供。

以下是最小的设置代码片段:-

烧瓶(文件名:- example.py)

#!/usr/bin/env python
from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route("/")
def hello():
    return render_template('topic.html')

HTML(文件名: - topic.html)

<!DOCTYPE html>
<body>
<img alt="logo" src="{{ url_for('static', filename='img/example.png') }}">
</body>
</html>

启用fcgi WSGI(文件名:-test.fcgi)

#!/usr/bin/env python
from flup.server.fcgi import WSGIServer
from example import app

if __name__ == '__main__':
    WSGIServer(app, debug = True).run()

lighttpd 配置文件

server.modules   += ( "mod_fastcgi" )
server.modules   += ( "mod_rewrite" )
server.modules   += ( "mod_alias" )
server.modules   += ( "mod_accesslog" )

server.document-root        = "/var/www/testing/"
server.port = 5000
server.modules += ( "mod_cgi" )

mimetype.assign = (
".html" => "text/html", 
".txt" => "text/plain",
".jpg" => "image/jpeg",
 ".png" => "image/png",
".js" => "text/javascript",
".css" => "text/css"
 )

cgi.assign                 = ( ".pl"  => "/usr/bin/perl",
                           ".cgi" => "/usr/bin/perl",
                           ".rb"  => "/usr/bin/ruby",
                           ".erb" => "/usr/bin/eruby",
                           ".py"  => "/usr/bin/python",
                           ".php" => "/usr/bin/php-cgi" )

index-file.names           += ( "index.pl",   "default.pl",
                           "index.rb",   "default.rb",
                           "index.erb",  "default.erb",
                           "index.py",   "default.py",
                           "index.php",  "default.php" )

server.errorlog    = "/var/log/lighttpd/error.log"
accesslog.filename = "/var/log/lighttpd/access.log"

$HTTP["url"] =~ "^/" {
   fastcgi.server = ("" =>
                    ((
    "socket" => "/tmp/test-fcgi.sock",
    "bin-path" => "/var/www/testing/test.fcgi",
    "check-local" => "disable",
    "max-procs" => 1
))
)
}

Flask 文件夹结构

/testing/
    /example.py  
    /test.fcgi  
    /static/
        /img/
           /example.png
    /template/
        /topic.html

使用 Flask 开发服务器一切正常。但是使用 lighttpd 部署时,css 和图像无法正常提供。使用 lighttpd 和 Fast-CGI 部署 flask 的正确方法是什么?

有了这个更新的 lighttpd 配置文件,css 图像可以从 lighttpd 正确地提供。

server.modules   += ( "mod_fastcgi" )
server.modules   += ( "mod_rewrite" )
server.modules   += ( "mod_alias" )
server.modules   += ( "mod_accesslog" )

server.document-root        = "/var/www/testing/"
server.port = 5000
server.modules += ( "mod_cgi" )

mimetype.assign = (
".html" => "text/html", 
".txt" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png",
".js" => "text/javascript",
".css" => "text/css"
)

cgi.assign                 = ( ".pl"  => "/usr/bin/perl",
                           ".cgi" => "/usr/bin/perl",
                           ".rb"  => "/usr/bin/ruby",
                           ".erb" => "/usr/bin/eruby",
                           ".py"  => "/usr/bin/python",
                           ".php" => "/usr/bin/php-cgi" )

index-file.names           += ( "index.pl",   "default.pl",
                           "index.rb",   "default.rb",
                           "index.erb",  "default.erb",
                           "index.py",   "default.py",
                           "index.php",  "default.php" )

server.errorlog    = "/var/log/lighttpd/error.log"
accesslog.filename = "/var/log/lighttpd/access.log"

fastcgi.server = ("/test.fcgi" =>
((
    "socket" => "/tmp/test-fcgi.sock",
    "bin-path" => "/var/www/testing/test.fcgi",
    "check-local" => "disable",
    "max-procs" => 1
))
)

alias.url = ( 
"/static" => "/var/www/testing/static"
)

url.rewrite-once = (
"^(/static($|/.*))$" => "",
"^(/.*)$" => "/test.fcgi"
)