如何获得Apache供职于瓶的web应用的静态文件 [英] How to get apache to serve static files on Flask webapp

查看:135
本文介绍了如何获得Apache供职于瓶的web应用的静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个500内部错误,让Apache来为我的静态文件。

该应用程序将在当地举办的(而不是面向WWW)。不会有任何DNS来解析www.domain.com的名字。我希望能够通过输入服务器的IP地址时,我访问网络上的应用程序

这是我的httpd.conf文件(我在RHEL):

 <目录/ var / WWW / testapp>
  为了允许,拒绝
  所有允许
< /目录>WSGIScriptAlias​​ / /var/www/testapp/service.wsgi

如果我改变WSGIScriptAlias​​为 WGSIScriptAlias​​ /测试/var/www/testapp/service.wsgi 然后当我在IP类型,我可以查看我的静态文件,但我仍然无法从[IP] /测试访问service.py脚本。

在任何情况下,我希望能够以服务的service.py剧本,所以我想我的别名在 / 启动所有的GET / POST请求,而不是一些其他的地方。

我所有的静态文件是在/ var / www / html等(之前我在httpd.conf搞砸阿帕奇自动显示这些文件,现在我刚开500)。

这是我的service.wsgi:

 进口SYS
sys.path.insert(0,'在/ var / WWW / testapp)
从服务导入应用程序作为应用程序

这是我的service.py:

 从瓶中瓶进口
应用=瓶(__ name__)@ app.route(/)
高清你好(ENVIRON,start_response):
    状态=200 OK
    输出=你好
    response_headers = [('内容类型','text / plain的'),(内容长度,STR(LEN(输出)))]
    start_response(状态,response_headers)
    返回输出如果__name __ =='__ main__
    app.run()

我需要让我的.wsgi文件在/ var / www / html等目录呢?或者,他们可以在不同的文件夹去了?我可以看到,有可能是我发送到服务器(你好),并已在/ var / www / html等/目录下的静态文件信息之间的一些冲突。这就是为什么我试图别名设置为 /测试但是这也不能工作。

我只希望我的瓶的应用程序服务GET / POST请求,并希望Apache以服务所有静态文件。


解决方案

修复500错误

您目前收到500错误,因为你的处理程序是一个基本的WSGI处理程序,但瓶处理程序不是WSGI处理程序(瓶/ WERKZEUG抽象所有你)。您的处理程序更改为:

  @ app.route(/)
高清你好():
    回归你好

和500错误应该走了。

服务与Apache静态文件

下面的技巧可以在您的应用程序服务于域的根使用( / ),这取决于您是否使用 WSGIScriptAlias​​ 的AddHandler

当使用 WSGIScriptAlias​​

在使用 WSGIScriptAlias​​ 安装在 / A WSGI应用程序,您可以使用Apache的别名指令的,以确保某些子路由是的的被处理 WSGIScriptAlias​​ (这是进一步记录在的mod_wsgi 的维基以及):

 别名/静态//路径/要/应用/静态/
<目录/路径/要/应用/静态/>
  为了允许,拒绝
  所有允许
< /目录>

如果你也想支持静态蓝图的文件夹,以及你的的需要使用的 Alias​​Match 指令:

  Alias​​Match(我)^ /([^ /] +)/静态/(.*)$/路径/要/应用/蓝图根/ $ 1 /静态/ $ 2
<目录〜/path/to/app/blueprints-root/[^/]+/static/.*\">
  为了允许,拒绝
  所有允许
< /目录>

参见:目录指令。

当使用的AddHandler

由于格雷厄姆邓普尔顿已经在评论中指出,可以使用的mod_rewrite 通关要求对Python当且仅当一个文件没有在的DocumentRoot 存在。从链接的文档引用:


  

在使用的AddHandler指令,由脚本文件的扩展名确定的WSGI应用程序,只有这样,才能使WSGI应用程序显示为服务器的根是用在URL内部到Apache的飞重写执行mod_rewrite的。 mod_rewrite的所需的规则,以确保WSGI应用程序,通过在虚拟主机的根目录下的脚本文件site.wsgi'实现的,显示为安装在虚拟主机的根目录为:

  RewriteEngine叙述在
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^(。*)$ /site.wsgi/$1 [QSA,PT,L]


  
  

确实注意到然而,当WSGI应用程序的请求执行'SCRIPT_NAME'变量,指示哪些应用程序的安装点是将/site.wsgi。这将意味着,当一个应用程序WSGI构建了基于SCRIPT_NAME一个绝对的URL,这将包括,而不是它被隐藏的URLsite.wsgi。由于这很可能是不可取的,很多web框架提供了一个选项覆盖哪些挂载点的值。如果这样的配置选项不可用,这也很容易在site.wsgi'脚本文件本身调整SCRIPT_NAME'的值。

 从your.app导入应用程序#您的水壶的应用进口posixpath高清应用(ENVIRON,start_response):
    #包装设置SCRIPT_NAME实际安装点。
    ENVIRON ['SCRIPT_NAME'] = posixpath.dirname(ENVIRON ['SCRIPT_NAME'])
    如果ENVIRON ['SCRIPT_NAME'] =='/':
        ENVIRON ['SCRIPT_NAME'] =''
    返回应用程序(ENVIRON,start_response)

这包装将确保site.wsgi永远不会出现在URL中,只要它没有被列入首位,并且通过访问该网站,而不是根始终。


I'm getting a 500 internal error while trying to get Apache to serve my static files.

The application will be locally hosted (not www facing). There will be no DNS to resolve a 'www.domain.com' name. I want to be able to access the application by entering the IP address of the server when I'm on that network.

This is my httpd.conf file (I'm on RHEL):

<Directory /var/www/testapp>
  Order allow,deny
  Allow from all
</Directory>

WSGIScriptAlias / /var/www/testapp/service.wsgi

If I change the WSGIScriptAlias to WGSIScriptAlias /test /var/www/testapp/service.wsgi then I can view my static files when I type in the IP, but I still can't access the service.py script from [IP]/test.

In any case, I want to be able to service all GET/POST requests with the service.py script so I want my alias to start at /, not some other place.

All my static files are in /var/www/html (Apache was automatically displaying these files before I messed with the httpd.conf, now I'm just getting a 500).

This is my service.wsgi:

import sys
sys.path.insert(0, '/var/www/testapp')
from service import app as application

This is my service.py:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello(environ, start_response):
    status = '200 OK'
    output = "Hello"
    response_headers = [('Content-type', 'text/plain'), ('Content-length', str(len(output)))]
    start_response(status, response_headers)
    return output

if __name__=='__main__'
    app.run()

Do I need keep my .wsgi files in the /var/www/html directory as well? Or can they go in a different folder? I can see that there might be some conflict between the message I am sending to the server ('Hello') and the static files that are already in the /var/www/html/ directory. That's why I tried setting the alias to /test but that didn't work either.

I just want my Flask application to service GET/POST requests and want apache to serve all the static files.

解决方案

Fixing the 500 errors

You are currently getting 500 errors because your handler is a basic WSGI handler, but Flask handlers are not WSGI handlers (Flask / Werkzeug abstracts all that for you). Change your handler to:

@app.route("/")
def hello():
    return "Hello"

and the 500 errors should go away.

Serving static files with Apache

The following techniques can be used when your application is serving the root of the domain (/), depending on whether you are using WSGIScriptAlias or AddHandler.

When using WSGIScriptAlias

When using the WSGIScriptAlias to mount a WSGI application at / you can use an Apache Alias directive to ensure that certain sub-routes are not handled by WSGIScriptAlias (this is further documented in mod_wsgi's wiki as well):

Alias "/static/" "/path/to/app/static/"
<Directory "/path/to/app/static/">
  Order allow,deny
  Allow from all
</Directory>

If you also want to support blueprint static folders as well you'll also need to use the AliasMatch directive:

AliasMatch "(?i)^/([^/]+)/static/(.*)$" "/path/to/app/blueprints-root/$1/static/$2"
<Directory ~ "/path/to/app/blueprints-root/[^/]+/static/.*">
  Order allow,deny
  Allow from all
</Directory>

See also: The Directory directive.

When using AddHandler

As Graham Dumpleton has pointed out in the comments, you can use mod_rewrite to pass requests off to Python if and only if a file does not exist in DocumentRoot. Quoting from the linked docs:

When using the AddHandler directive, with WSGI applications identified by the extension of the script file, the only way to make the WSGI application appear as the root of the server is to perform on the fly rewriting of the URL internal to Apache using mod_rewrite. The required rules for mod_rewrite to ensure that a WSGI application, implemented by the script file 'site.wsgi' in the root directory of the virtual host, appears as being mounted on the root of the virtual host would be:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /site.wsgi/$1 [QSA,PT,L]

Do note however that when the WSGI application is executed for a request the 'SCRIPT_NAME' variable indicating what the mount point of the application was will be '/site.wsgi'. This will mean that when a WSGI application constructs an absolute URL based on 'SCRIPT_NAME', it will include 'site.wsgi' in the URL rather than it being hidden. As this would probably be undesirable, many web frameworks provide an option to override what the value for the mount point is. If such a configuration option isn't available, it is just as easy to adjust the value of 'SCRIPT_NAME' in the 'site.wsgi' script file itself.

from your.app import app  # Your Flask app

import posixpath

def application(environ, start_response):
    # Wrapper to set SCRIPT_NAME to actual mount point.
    environ['SCRIPT_NAME'] = posixpath.dirname(environ['SCRIPT_NAME'])
    if environ['SCRIPT_NAME'] == '/':
        environ['SCRIPT_NAME'] = ''
    return app(environ, start_response)

This wrapper will ensure that 'site.wsgi' never appears in the URL as long as it wasn't included in the first place and that access was always via the root of the web site instead.

这篇关于如何获得Apache供职于瓶的web应用的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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