navigationGo.pngQuick Navigation
allprojects32.pngAll projects
hardware32.pngHardware
links32.pngLinks

favoriteStar32.pngTop projects
Alan numitron clock
Clapclap 2313/1386
SNES Pi Webserver
USB Volume/USB toys
Smokey amp
Laser cutter
WordClock
ardReveil v3
SNES Arcade cabinet
Game boy projects
cameleon
Home Presence Detector

github32.pngGitHub
AlanFromJapan

navigationMail.pngContact me

alanfjmail.png
3flags.pngWho's Alan?


Akizukidenshi
Elec-lab
Rand Nerd Tut
EEVblog
SpritesMods
AvrFreaks
Gameboy Dev
FLOZz' blog
Switch-science
Sparkfun
Suzusho
Datasheet Lib
Reddit Elec
Ermicro
Carnet du maker (fr)

setup flask behind nginx proxy

Last update: Sun Jul 28 14:14:33 2024
Because 1 server 2 apps is sometime what you want.

Presentation

Principle

  • Nginx is the proxy listening to port 80 and your 2 sites AAA and BBB will live behind the proxy in folders aa and bb
  • Ie: http://my-super-site.dot.com/aa => calls the site running on http://my-super-site.dot.com:12345 (that is app AAA)

    To do that, setup NginX in front of both sites on the same server as a reverse proxy.

    Implementation

    Step 1 : Nginx

    Do like RTFM 1 : https://flask.palletsprojects.com/en/2.2.x/deploying/nginx/

    EXCEPT that it's maybe better to :
  • create an new file in /etc/nginx/sites-available
  • link it in /etc/nginx/sites-enabled
  • Check it would work sudo nginx -t
  • Enable with a manly sudo service nginx restart
  • Step 2 : Flask

    So though it works without telling Flask that it's behind a proxy, the makers of Flask say to do it so RTFM 2 : https://flask.palletsprojects.com/en/2.3.x/deploying/proxy_fix/

    ADDITIONALLY :
  • your intra-site URL must all be RELATIVES meaning in your site navigate like link WITHOUT a leading / on the target URL or you'll end up at the root of your http://my-super-site.dot.com/ and not in the /aa "folder" (the proxy redirection to your site)


  • One other option is to use flask URL processor to edit he URL dynamically : link.

    Step 3 : block direct access to apps

    Most likely you will not want to have your app accessible on http://my-super-site.dot.com:12345, so you need to block access from outside but not from the proxy.
    WORK IN PROGRESS!
    Not the answer alas: ufw deny in on eth0 to any port 12345 proto tcp

    Links

    Helpful sources

  • RTFM 1 : https://flask.palletsprojects.com/en/2.2.x/deploying/nginx/
  • RTFM 2 : https://flask.palletsprojects.com/en/2.2.x/deploying/proxy_fix/
  • All content on this site is shared under the MIT licence (do what u want, don't sue me, hat tip appreciated)
    electrogeek.tokyo ~ Formerly known as Kalshagar.wikispaces.com and electrogeek.cc (AlanFromJapan [2009 - 2024])