Now we want to install nginx to serve our application.
sudo apt-get install nginx # on debian/ubuntu
Then we create a configuration for our website
cd /etc/nginx/site-available # go to the configuration for available sites
# create a file flaskconfig with your favourite editor
flaskconfig
server {
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass unix:///tmp/flask.sock;
}
}
This tells nginx to listen on port 80 (default for http) and serve something at the root path (/
). There we tell nginx to simply act as a proxy and pass every request to a socket called flask.sock
located in /tmp/
.
Let's enable the site:
cd /etc/nginx/sites-enabled
sudo ln -s ../sites-available/flaskconfig .
You might want to remove the default configuration if it is enabled:
# inside /etc/sites-enabled
sudo rm default
Then restart nginx:
sudo service nginx restart
Point your browser to localhost
and you will see an error: 502 Bad Gateway
.
This means that nginx is up and working but the socket is missing. So lets create that.
Go back to your uwsgi.ini
file and open it. Then append these lines:
socket = /tmp/flask.sock
chmod-socket = 666
The first line tells uwsgi to create a socket at the given location. The socket will be used to receive requests and send back the responses. In the last line we allow other users (including nginx) to be able to read and write from that socket.
Start uwsgi again with uwsgi --ini uwsgi.ini
. Now point your browser again to localhost
and you will see the "Hello uWSGI" greeting again.
Note that you still can see the response on localhost:5000
because uWSGI now serves the application via http and the socket. So let's disable the http option in the ini file
http = :5000 # <-- remove this line and restart uwsgi
Now the app can only be accessed from nginx (or reading that socket directly :) ).