通过 apache 提供静态文件 [英] Serving static files through apache

查看:36
本文介绍了通过 apache 提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是整个 mod_wsgi 的新手,并通过 apache 提供文件.我对烧瓶真的很满意,但这是我无法理解的.我做了hello-world程序,成功显示hello world!现在我想显示一个图像文件.所以我将我的 hello-world.py 更新为:

I am new to the whole mod_wsgi and serving files through apache. I am really comfortable with flask but this is something i can't get my head around. I did the hello-world program and successfully displayed hello world! Now i wanted to display a image file. So I updated my hello-world.py to:

from flask import *
yourflaskapp = Flask(__name__)

@yourflaskapp.route("/")
def hello():
    file="203.jpg"
    return render_template("hello.html",file=file)
#   return"HEY"
if __name__ == "__main__":
    yourflaskapp.run()

我的目录结构类似于:/var/www/hello-world

my directory structure is something like:/var/www/hello-world

/hello-world
    test.py
    yourflaskapp.wsgi
    /static
        -203.jpg
    /templates
        -hello.html

我的模板很简单:

<!DOCTYPE html>
<html><head><title>hi</title></head>
<body>
<img src="{{url_for('static',filename=file)}}"/>
</body></html>

我的 apache conf 文件是:

and my apache conf file is:

<VirtualHost *:80>
     WSGIDaemonProcess yourflaskapp
     WSGIScriptAlias / /var/www/hello-world/yourflaskapp.wsgi
     Alias /static/ /var/www/hello-world/static
     Alias /templates/ /var/www/hello-world/templates
     <Directory /var/www/hello-world>
            WSGIProcessGroup yourflaskapp
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
     </Directory>
        <Directory /var/www/hello-world/static>
            Order allow,deny
            Allow from all
        </Directory>
        <Directory /var/www/hello-world/templates>
            Order allow,deny
            Allow from all
        </Directory>
     ErrorLog ${APACHE_LOG_DIR}/error.log
     LogLevel warn
     CustomLog ${APACHE_LOG_DIR}/access.log combined
 </VirtualHost>

虽然当我打开浏览器并转到我的 ip 时,它没有显示图像文件.我究竟做错了什么?我应该遵循其他任何方法吗?如果有人可以推荐任何好的链接,我可以从中了解与flask+mod_wsgi+apache2一起使用

Though when i open the browser and head to my ip, it doesn't show the image file. What am i doing wrong? Is there any other approach i should follow? and if anyone could recommend any good links from where i can understand working with flask+mod_wsgi+apache2

推荐答案

在子 URL 上挂载静态文件时,平衡尾部斜杠通常总是一个好主意.所以,而不是:

It is generally always a good idea to have trailing slash balanced when mounting static files at a sub URL. So instead of:

Alias /static/ /var/www/hello-world/static

使用:

Alias /static /var/www/hello-world/static

这篇关于通过 apache 提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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