Ruan Bekker's Blog

From a Curious mind to Posts on Github

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 127.0.0.1:5000 and our nginx reverse proxy will listen on 0.0.0.0:80 which will proxy requests through to our flask upstream.

Our Backend Application

Our Flask application:

1
2
3
4
5
6
7
8
9
from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello'

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000)

Nginx

Install nginx:

1
$ apt install nginx -y

Our main nginx configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# /etc/nginx/conf.d/backend-flask.conf
upstream backend_flask {
    server 127.0.0.1:5000;
}

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:

1
2
$ 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:

1
2
$ curl http://nginx-public-ip:80/
Hello

Resoures

Comments