Web.py Apache mod_wsgi url重写

时间:2013-12-18 08:49:34

标签: python apache web.py

我相信我已经使用mod_wsgi和Rewrite Engine正确设置了Apache。我正在使用web.py来提供内容。测试“Hello World”应用程序有效,但输出包含文件根目录。 看起来像这样:

Hello, /var/www/example.com/application/!

我已经包含了配置和代码。

这是apache配置:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName foodcost.mynetwork.inside
    ServerAlias foodcost.mynetwork.inside
    DocumentRoot /var/www/example.com/public_html/
    ErrorLog /var/www/example.com/logs/error.log
    CustomLog /var/www/example.com/logs/access.log combined
    WSGIScriptAlias / /var/www/example.com/application/

    Alias /static /var/www/example.com/public_html

    <Directory /var/www/example.com/application>
      SetHandler wsgi-script
      Options ExecCGI
      Options +FollowSymLinks
    </Directory>

    AddType text/html .py

    <Location />
      RewriteEngine on
      RewriteBase /
      RewriteCond %{REQUEST_URI} !^/static
      RewriteCond %{REQUEST_URI} !^(/.*)+code.py/
      RewriteRule ^(.*)$ code.py/$1 [PT]
    </Location>
</VirtualHost>

Python代码:

import web

urls = (
    '(.*)', 'hello'
)

app = web.application(urls, globals(), autoreload=False)
application = app.wsgifunc()

class hello:
    def GET(self, name):
        if not name:
            name = 'World'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

更新:在使用Rewrite后,我发现问题出在RewriteRule ^(.*)$ code.py/$1 [PT]上。 $1(参数)传递运行python脚本的根目录以及URL的其余部分来自根URL。

所以这个例子就是URL:

http://{rootURL}/tom

输出:

Hello, /var/www/example.com/application/tom!

我无法弄清楚为什么要传入python脚本的目录位置。

1 个答案:

答案 0 :(得分:2)

按照http://webpy.org/cookbook/mod_wsgi-apache的教程,我相信你真的不需要重写网址。那么你的apache配置是:

<VirtualHost *:80>

    ServerAdmin admin@example.com
    ServerName foodcost.mynetwork.inside
    ServerAlias foodcost.mynetwork.inside
    DocumentRoot /var/www/example.com/public_html/
    ErrorLog /var/www/example.com/logs/error.log
    CustomLog /var/www/example.com/logs/access.log combined

    WSGIScriptAlias / /var/www/example.com/application/code.py/
    Alias /static /var/www/example.com/public_html

    <Directory /var/www/example.com/application>
      SetHandler wsgi-script
      Options ExecCGI
      Options +FollowSymLinks
    </Directory>

    AddType text/html .py

</VirtualHost>

(除了其他更改之外,请注意code.py/指令中添加了WSGIScriptAlias

文件/var/www/example.com/application/code.py是:

import web

urls = (
    '/(.*)', 'hello'
)

app = web.application(urls, globals(), autoreload=False)
application = app.wsgifunc()

class hello:
    def GET(self, name):
        if not name:
            name = 'World'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

(注意开头的urls元组中添加的斜杠。

这样就没有“code.py”出现了:

enter image description here