kampta
By:
kampta

Setting up a sub-domain to point to a flask application on same droplet (with uwsgi and nginx on Ubuntu 15.10)

January 17, 2016 1.1k views
Python Nginx Deployment Python Frameworks Applications Ubuntu

I have a domain called abc.com which points to a static html page, in /var/www/abc.com/public_html.

I want to host a Flask application on same droplet (different port maybe?), which is accessible using a subdomain demo.abc.com.

I followed instructions here, and here, but they seem not to be working for me right now.

Here is a how my Zone file looks like currently -

$ORIGIN abc.com.
$TTL 1800
abc.com. IN SOA ns1.digitalocean.com. hostmaster.abc.com. 1451918078 10800 3600 604800 1800
abc.com. 1800 IN NS ns1.digitalocean.com.
abc.com. 1800 IN NS ns2.digitalocean.com.
abc.com. 1800 IN NS ns3.digitalocean.com.
abc.com. 1800 IN A 111.222.333.44
*.abc.com. 1800 IN CNAME abc.com.

I have created an app.py in /var/www/demo.abc.com/ which has

from flask import Flask
application = Flask(__name__)

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

if __name__ == "__main__":
    application.run(host='0.0.0.0')

I have 2 files in /etc/nginx/sites-available
abc.com
demo.abc.com

Here is what demo.abc.com looks like:

server {
    listen 80;
    server_tokens off;
    server_name demo.abc.com;

     location / {
         include uwsgi_params;
         uwsgi_pass unix:/tmp/abc.sock;
     }

     location /static {
         alias /var/www/demo.abc.com/static;
     }

     ## Only requests to our Host are allowed
     if ($host !~ ^(demo.abc.com.com|www.demo.abc.com)$ ) {
        return 444;
     }
}

Here is what /etc/uwsgi/apps-available/demo.abc.com.ini looks like

[uwsgi]
vhost = true
socket = /tmp/demo.sock
venv = /var/www/demo.abc.com/.env
chdir = /var/www/demo.abc.com
module = app
callable = application

master = true
processes = 5

chmod-socket = 660
vacuum = true

die-on-term = true

abc.com works fine, while demo.abc.com is not running.
I don't get any error while starting service nginx and uwsgi. I am however beginner in both of them. Please guide me on what I am doing wrong

1 Answer

When finding problems en debugging them its always usefull to use logs to see what is going on. Define your logs and check what nginx tells you once you access the website :

        error_log /var/log/demo.abc_error.log;
        access_log /var/log/demo.abc_access.log combined;
  • My nginx error log file was empty (/var/www/nginx/error.log)

    Attaching the full uwsgi log, and nginx access.log (The second one is 88 MB). I am not sure, how to get only the failed requests parts of the log. To my understanding, they look like this -

    uwsgi.log

    Sun Jan 17 07:54:58 2016 - *** Starting uWSGI 2.0.7-debian (64bit) on [Sun Jan 17 07:54:58 2016] ***
    Sun Jan 17 07:54:58 2016 - compiled with version: 5.2.1 20150903 on 08 September 2015 19:33:31
    Sun Jan 17 07:54:58 2016 - os: Linux-4.2.0-16-generic #19-Ubuntu SMP Thu Oct 8 15:35:06 UTC 2015
    Sun Jan 17 07:54:58 2016 - nodename: Poolka.com
    Sun Jan 17 07:54:58 2016 - machine: x86_64
    Sun Jan 17 07:54:58 2016 - clock source: unix
    Sun Jan 17 07:54:58 2016 - pcre jit disabled
    Sun Jan 17 07:54:58 2016 - detected number of CPU cores: 1
    Sun Jan 17 07:54:58 2016 - current working directory: /
    Sun Jan 17 07:54:58 2016 - writing pidfile to /run/uwsgi/app/demo.poolka.com/pid
    Sun Jan 17 07:54:58 2016 - detected binary path: /usr/bin/uwsgi-core
    Sun Jan 17 07:54:58 2016 - setgid() to 33
    Sun Jan 17 07:54:58 2016 - setuid() to 33
    Sun Jan 17 07:54:58 2016 - your processes number limit is 1833
    Sun Jan 17 07:54:58 2016 - your memory page size is 4096 bytes
    Sun Jan 17 07:54:58 2016 - detected max file descriptor number: 1024
    Sun Jan 17 07:54:58 2016 - VirtualHosting mode enabled.
    Sun Jan 17 07:54:58 2016 - lock engine: pthread robust mutexes
    Sun Jan 17 07:54:58 2016 - thunder lock: disabled (you can enable it with --thunder-lock)
    Sun Jan 17 07:54:58 2016 - uwsgi socket 0 bound to UNIX address /run/uwsgi/app/demo.poolka.com/socket fd 3
    Sun Jan 17 07:54:58 2016 - uwsgi socket 1 bound to UNIX address /tmp/demo.sock fd 5
    Sun Jan 17 07:54:58 2016 - Python version: 2.7.10 (default, Oct 14 2015, 16:09:02)  [GCC 5.2.1 20151010]
    Sun Jan 17 07:54:58 2016 - Set PythonHome to /var/www/demo.poolka.com/.env
    Sun Jan 17 07:54:58 2016 - *** Python threads support is disabled. You can enable it with --enable-threads ***
    Sun Jan 17 07:54:58 2016 - Python main interpreter initialized at 0x1f08d00
    Sun Jan 17 07:54:58 2016 - your server socket listen backlog is limited to 100 connections
    Sun Jan 17 07:54:58 2016 - your mercy for graceful operations on workers is 60 seconds
    Sun Jan 17 07:54:58 2016 - mapped 436608 bytes (426 KB) for 5 cores
    Sun Jan 17 07:54:58 2016 - *** Operational MODE: preforking ***
    Sun Jan 17 07:54:58 2016 - WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1f08d00 pid: 17624 (default app)
    Sun Jan 17 07:54:58 2016 - *** uWSGI is running in multiple interpreter mode ***
    Sun Jan 17 07:54:58 2016 - spawned uWSGI master process (pid: 17624)
    Sun Jan 17 07:54:58 2016 - spawned uWSGI worker 1 (pid: 17632, cores: 1)
    Sun Jan 17 07:54:58 2016 - spawned uWSGI worker 2 (pid: 17633, cores: 1)
    Sun Jan 17 07:54:58 2016 - spawned uWSGI worker 3 (pid: 17634, cores: 1)
    Sun Jan 17 07:54:58 2016 - spawned uWSGI worker 4 (pid: 17635, cores: 1)
    Sun Jan 17 07:54:58 2016 - spawned uWSGI worker 5 (pid: 17636, cores: 1)
    

    access.log

    14.183.60.220 - - [17/Jan/2016:13:38:52 -0500] "GET /items?c=37008&ts=1426138782918&m=20&T= HTTP/1.1" 404 177 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"
    86.129.146.109 - - [17/Jan/2016:13:38:52 -0500] "GET /items?c=6006&ts=1427056285299&m=30&T= HTTP/1.1" 404 177 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"
    86.129.146.109 - - [17/Jan/2016:13:38:52 -0500] "GET /items?c=48005&ts=1397342348934&m=30&T= HTTP/1.1" 404 177 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"
    14.183.60.220 - - [17/Jan/2016:13:38:52 -0500] "GET /items?c=28006&ts=1409495783294&m=20&T= HTTP/1.1" 404 177 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"
    14.183.60.220 - - [17/Jan/2016:13:38:52 -0500] "GET /items?c=20008&ts=1426240752454&m=20&T= HTTP/1.1" 404 177 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"
    31.18.246.156 - - [17/Jan/2016:13:38:52 -0500] "GET /items?c=6008&ts=1427052397699&m=10&T= HTTP/1.1" 404 177 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"
    31.18.246.156 - - [17/Jan/2016:13:38:52 -0500] "GET /items?c=41008&ts=1423783986283&m=10&T= HTTP/1.1" 404 177 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"
    31.18.246.156 - - [17/Jan/2016:13:38:52 -0500] "GET /items?c=23008&ts=1427067411049&m=10&T= HTTP/1.1" 404 177 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"
    14.183.60.220 - - [17/Jan/2016:13:38:52 -0500] "GET /items?c=37009&ts=1426108634237&m=20&T= HTTP/1.1" 404 177 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"
    14.183.60.220 - - [17/Jan/2016:13:38:52 -0500] "GET /items?c=46001&ts=1409495784000&m=20&T= HTTP/1.1" 404 177 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"
    14.183.60.220 - - [17/Jan/2016:13:38:52 -0500] "GET /items?c=20009&ts=1426224478835&m=20&T= HTTP/1.1" 404 177 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"
    14.183.60.220 - - [17/Jan/2016:13:38:52 -0500] "GET /items?c=11001&ts=1426240749118&m=20&T= HTTP/1.1" 404 177 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"
    
Have another answer? Share your knowledge.