如何在Flask webapp上获取apache来提供静态文件

我在尝试让Apache提供静态文件时遇到500内部错误.

该应用程序将在本地托管(而不是面向www).没有DNS可以解析“www.domain.com”名称.我想通过在该网络上输入服务器的IP地址来访问应用程序.

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

sgiScriptAlias / /var/www/testapp/service.wsgi

如果我将WsgiScriptAlias更改为WGSIScriptAlias / test /var/www/testapp/service.wsgi,那么当我输入IP时我可以查看我的静态文件,但我仍然无法从[IP] /访问service.py脚本测试.

在任何情况下,我希望能够使用service.py脚本为所有GET / POST请求提供服务,因此我希望我的别名从/开始,而不是从其他地方开始.

我的所有静态文件都在/ var / www / html中(Apache在我使用httpd.conf之前自动显示这些文件,现在我只是获得了500).

这是我的service.wsgi

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

这是我的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()

我是否还需要将我的.wsgi文件保存在/ var / www / html目录中?或者他们可以进入不同的文件夹?我可以看到我发送到服务器的消息(‘Hello’)和已经在/ var / www / html /目录中的静态文件之间可能存在一些冲突.这就是为什么我尝试将别名设置为/ test但这也不起作用.

我只是想让我的Flask应用程序为GET / POST请求提供服务,并希望apache能够提供所有静态文件.

最佳答案
修复500错误

您当前获得500个错误,因为您的处理程序是基本的Wsgi处理程序,但Flask处理程序不是Wsgi处理程序(Flask / Werkzeug为您提取所有这些内容).将处理程序更改为:

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

500错误应该消失.

使用Apache提供静态文件

当您的应用程序为域的根目录(/)提供服务时,可以使用以下技术,具体取决于您使用的是WsgiScriptAlias还是AddHandler.

使用WsgiScriptAlias时

使用WsgiScriptAlias在/上安装Wsgi应用程序时,您可以使用Apache Alias指令to ensure that certain sub-routes are not handled by WSGIScriptAlias(这是further documented in mod_wsgi‘s wiki as well):

Alias "/static/" "/path/to/app/static/"

如果您还想支持蓝图静态文件夹,您还需要使用AliasMatch指令:

AliasMatch "(?i)^/([^/]+)/static/(.*)$" "/path/to/app/blueprints-root/$1/static/$2"

另请参见:Directory指令.

使用AddHandler时

正如Graham Dumpleton在评论中指出的那样,you can use mod_rewrite将请求传递给Python,当且仅当DocumentRoot中不存在文件时.引用链接的文档:

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:

06003

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.

06004

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.

相关文章

Linux中的ARP防火墙主要用于防御ARP欺骗攻击,其效果取决于多...
insmod和modprobe加-f参数导致Invalid module format错误 这...
将ArchLinux安装到U盘 几个月前入门Arch的时候上网搜了不少安...
1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...