Published on

Setup a Reverse Proxy on Nginx for your Backend Applications


Nginx is a great product! And today we will use nginx to setup a http reverse proxy to access our backend applications.

Our Setup

We will have a flask backend application listening on and our nginx reverse proxy will listen on which will proxy requests through to our flask upstream.

Our Backend Application

Our Flask application:

from flask import Flask
app = Flask(__name__)

def index():
    return 'Hello'

if __name__ == '__main__':'', port=5000)


Install nginx:

$ apt install nginx -y

Our main nginx configuration:

user www-data;
worker_processes auto;
pid /run/;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_names_hash_bucket_size 64;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/backend-*.conf;

Our application's configuration:

upstream backend_flask {

server {
    listen 80 default_server;
    listen [::]:80;
    server_name _;
    location / {
        include proxy_params;
        proxy_http_version 1.1;
        proxy_read_timeout 90;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://backend_flask;
        proxy_buffering off;

Restart nginx and enable nginx on boot:

$ systemctl restart nginx
$ systemctl enable nginx

Test your Application:

Access your server on port 80 and you should receive the response from your flask application:

$ curl http://nginx-public-ip:80/


Thank You

Thanks for reading, if you like my content, feel free to check out my website, and subscrube to my newsletter or follow me at @ruanbekker on Twitter.

Buy Me A Coffee