Nginx as reverse proxy

This article explains about, using nginx as reverse proxy with an example configuration

Hello all. Today we will see how to use nginx as reverse proxy. I have explained about nginx in my previous articles , please refer that for installation here

What is reverse proxy?


Let's say X -> Client Y -> Server

      X -----------------> Y (X sends request to Y)

Y responses to the Client X.

This is normal proxy server.

Reverse Proxy Server


X ------------------> Z -----------------------> Y


Y -------------------> Z --------------------------> X

 

X -> sends requests to Y , It's received by Z and proxy passed to Y and then the response from Y is send to X .

In Real life People are not aware of reverse proxy server. Since they can't see, they consider that as only one server response.

What we gonna do?

  we are gonna run flask application with nginx and uwsgi. In short flask app is gonna run by uwsgi . All the requests are proxy passed to uwsgi by nginx.


Install commands for flask and uwsgi

  sudo apt-get install uwsgi

  sudo pip install flask

These above two commands will install flask and uwsgi in your machine.


Step 1. simple hello world in flask looks like this


from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
return "Hello World!"

if __name__ == "__main__":
app.run()

save this as hello.py

Test run with python

  python hello.py

It runs this app on your machine on port  5000

Open your favourite browser http://127.0.0.1:5000/ . It displays

 Hello World!

Make one directory put these file inside that

mkdir test

cd test

touch __init__.py

Put these lines inside  __init__.py . Make sure hello.py inside test directory

from .hello import *

Step2. write uwsgi config in /etc/uwsgi/apps-enabled/

[uwsgi]
base = /home/bala/
venv = /home/bala/Envs/env2
basesock = /tmp/hello.sock
plugins = python
socket = %(basesock)
chdir = %(base)
module = test:app
threads = 1
master = 1
chmod-socket = 777

save this as  hello.ini inside  /etc/uwsgi/apps-enabled/

Note: if you are not using  virtualenv comment  venu line and change  base -> path-to-your-directory.


Restart the uwsgi service

   sudo service uwsgi restart

You can check  /var/log/uwsgi/apps/hello.log file whether it started successfully or not.

It will look like this

Wed Dec 28 12:01:23 2016 - thunder lock: disabled (you can enable it with --thunder-lock)
Wed Dec 28 12:01:23 2016 - uwsgi socket 0 bound to UNIX address /run/uwsgi/app/hello/socket fd 3
Wed Dec 28 12:01:23 2016 - uwsgi socket 1 bound to UNIX address /tmp/hello.sock fd 5
Wed Dec 28 12:01:23 2016 - Python version: 2.7.6 (default, Oct 26 2016, 20:33:43) [GCC 4.8.4]
Wed Dec 28 12:01:23 2016 - Set PythonHome to /home/bala/Envs/env2
Wed Dec 28 12:01:23 2016 - Python main interpreter initialized at 0x93b260
Wed Dec 28 12:01:23 2016 - python threads support enabled
Wed Dec 28 12:01:23 2016 - your server socket listen backlog is limited to 100 connections
Wed Dec 28 12:01:23 2016 - your mercy for graceful operations on workers is 60 seconds
Wed Dec 28 12:01:23 2016 - mapped 218376 bytes (213 KB) for 2 cores
Wed Dec 28 12:01:23 2016 - *** Operational MODE: preforking ***
Wed Dec 28 12:01:23 2016 - WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x93b260 pid: 31908 (default app)
Wed Dec 28 12:01:23 2016 - *** uWSGI is running in multiple interpreter mode ***
Wed Dec 28 12:01:23 2016 - spawned uWSGI master process (pid: 31908)
Wed Dec 28 12:01:23 2016 - spawned uWSGI worker 1 (pid: 31919, cores: 1)
Wed Dec 28 12:01:23 2016 - spawned uWSGI worker 2 (pid: 31920, cores: 1)

See WSGI app ready. It means no error and our app is running successfully.


Step3: Write Nginx config to act as reverse proxy inside  /etc/nginx/sites-enabled/


server {
listen *:8884;
location / {
uwsgi_pass unix://tmp/hello.sock;
include uwsgi_params;
}
}

save this as  hello.conf inside  /etc/nginx/sites-enabled/

Restart Nginx by

 sudo service nginx restart

 Conclusion:
            That's it . Now open Your favourite browser and type http://localhost:8884/

You will see

 Hello World!

These codes can be found https://github.com/fossbalaji/hello-flak-nginx-uwsgi

Thanks for reading my blog. Happy Coding !!!

Leave a comment

(Note: Comments are moderated)