nxweb + Python Flask Configuration
20.09.2014Quick start
Here is our Flask application:
hello.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello Flask!"
Make sure there is no nxweb_config.json
file in current directory and launch nxweb:
nxweb -T python -W hello.app
In case you have Flask installed in virtualenv environment ./env
you can specify path to that environment like this:
nxweb -T python -W hello.app -V env
Now point your browser at http://localhost:8055/
Production deployment
In production we are going to use more functional configuration.
Directory layout
current working directory/ |- nxweb_config.json |- python/ |- |- hello.py |- env/ |- static/ |- cache/ |- logs/
python
directory shall contain Python files of our application.
env
directory contains Python virtual environment where we have Flask installed (this is optional).
static
directory shall contain static files like *.js
, *.css
, images, etc.
cache
directory will be used by nxweb to store cached files (you might need to periodically clean this directory by removing files of certain age).
logs
directory will contain nxweb log files.
Make sure file permissions allow nxweb to write into cache
and logs
directories.
nxweb_config.json
{
"modules":{
"python":{
"virtualenv_path":"env", // this is optional
"project_path":"python", // python module search root; relative to workdir
"wsgi_application":"hello.app" // full python name of WSGI entry point
},
},
"routing":[
{ // first check static files
"prefix":null, // "vhost":".mydomain.com",
"handler":"sendfile", "memcache":false,
"dir":"static",
"charset":"utf-8", // charset for text files
"index_file":"index.htm", // directory index
"filters":[
{"type":"gzip", "compression":4, "cache_dir":"cache/gzip"}
]
},
{ // then fallback to python
"prefix":null, // "vhost":".mydomain.com",
"handler":"python", // "uri":"/my/path/info",
"dir":"cache/upload_temp", "size":50000000 /* 50Mb max upload */,
"filters":[
{"type":"file_cache", "cache_dir":"cache/python"}
]
}
]
}
Launch nxweb
Make sure your current working directory is where nxweb_config.json
file is located.
nxweb -H :80 -S :443 -l logs/nxweb_error_log -a logs/nxweb_access_log
This must be run by a privileged user (root) in order to use port numbers below 1024. If you specify -u <user> -g <group>
options then nxweb will drop privileges after binding the ports.
Now you should be able to access your application under site's root directory. Static files will be checked first, then request will be routed to Python.
There is a filter applied to Python's output in this sample config:
-
file_cache
will cache responses (only when they containLast-Modified
header)
Comments