简体   繁体   English

在Apache代理下运行Flask jQuery示例

[英]running Flask jQuery example under Apache proxy

I can successfully run the Flask jQuery example (as referred near bottom of Flask's "AJAX with jQuery" page.) It runs on the flask development server, and is accessible at http://localhost:5000 . 我可以成功运行Flask jQuery示例 (在Flask的“带有jQuery的AJAX”页面的底部附近提到。)它在flask开发服务器上运行,可以从http://localhost:5000

How do I proxy the page so that I can access the same app under http://localhost/jqueryexample ? 如何代理页面,以便可以在http://localhost/jqueryexample下访问同一应用程序?

I added this to my Apache VirtualHost entry thinking it will do the trick: 我将此添加到我的Apache VirtualHost条目中,认为它可以解决问题:

ProxyPass /jqueryexample http://localhost:5000/
ProxyPassReverse /jqueryexample http://localhost:5000/

But the new URL gives the 404 error: 但是新的网址显示404错误:

GET http://localhost/_add_numbers?a=6&b=2 404 (Not Found)

How can I get the example to run correctly under "canonical URL" (not sure if that's the right terminology)? 我如何才能使该示例在“规范URL”下正确运行(不确定这是否是正确的术语)? Or, how to change the app or Apache configuration in order to get this jQuery example running for both URLs? 或者,如何更改应用程序或Apache配置,以使这两个URL都运行此jQuery示例?


BTW, here's how you download and run the vanilla Flask jQuery example in question: 顺便说一句,这是您下载和运行有问题的香草Flask jQuery示例的方法:

git clone http://github.com/mitsuhiko/flask 
cd flask/examples/jqueryexample/ 
python jqueryexample.py

Okay, after looking into this further, I think I answered my own question: 好吧,在进一步研究之后,我想我回答了我自己的问题:

Apparently, instead of running the flask development server and trying to proxy it through Apache httpd, it's best to deploy the app directly to Apache using mod_wsgi. 显然,与其运行烧瓶开发服务器并尝试通过Apache httpd代理它,不如最好使用mod_wsgi将应用程序直接部署到Apache。 Guidelines on how to do this are well documented here . 有关如何执行此操作的指南已在此处详细记录。 In fact, for production, the dev server is not at all recommended (see here .) 实际上,对于生产而言,完全不建议使用dev服务器(请参阅此处)

As for deploying the jQuery Flask example itself, here's what you do (assuming your DocumentRoot is /var/www/html ): 至于部署jQuery Flask示例本身,这是您要做的(假设您的DocumentRoot是/var/www/html ):

# Get the example code.
git clone http://github.com/mitsuhiko/flask 
cd flask/examples/jqueryexample/

# Create WSGI file.
echo "\
import sys\
sys.path.insert(0, '/var/www/html/jqueryexample')\
from jqueryexample import app as application\
" > jqueryexample.wsgi

# Deploy to httpd.
sudo mkdir /var/www/html/jqueryexample
sudo cp -r * /var/www/html/jqueryexample/

Now add this to your VirtualHost: 现在将其添加到您的VirtualHost中:

WSGIScriptAlias /jqueryexample /var/www/html/jqueryexample/jqueryexample.wsgi
<Location /var/www/html/jqueryexample>
    Allow from all
    Order allow,deny
</Location>

Then restart httpd. 然后重新启动httpd。 Now check out the running app at http://localhost/jqueryexample . 现在,通过http://localhost/jqueryexample正在运行的应用程序。 Voila! 瞧!

I don't have an Apache install in front of me but if you are proxying the app shouldn't you change line 6 of the index.html from 我前面没有安装Apache,但是如果您代理该应用程序,则不应该将index.html的第6行更改为

$.getJSON($SCRIPT_ROOT + '/_add_numbers', {

to

$.getJSON($SCRIPT_ROOT + '/jqueryexample/_add_numbers', {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM