简体   繁体   中英

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 .

How do I proxy the page so that I can access the same app under http://localhost/jqueryexample ?

I added this to my Apache VirtualHost entry thinking it will do the trick:

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

But the new URL gives the 404 error:

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)? Or, how to change the app or Apache configuration in order to get this jQuery example running for both URLs?


BTW, here's how you download and run the vanilla Flask jQuery example in question:

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. Guidelines on how to do this are well documented here . In fact, for production, the dev server is not at all recommended (see here .)

As for deploying the jQuery Flask example itself, here's what you do (assuming your DocumentRoot is /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:

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

Then restart httpd. Now check out the running app at 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

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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