Flask app with Nginx and Gunicorn in Ubuntu - python

I have a vps flask application that processes incoming requests and returns a result in the format {result: OK} or {result: BAD}. There is also a bunch of nginx + gunicorn. When only gunicorn works for me, everything works as it should, but as soon as I connect nginx, the server returns me a response in the form of html code, which I will present below.
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.18.0 (Ubuntu)</center>
</body>
</html>
Here is my '/etc/nginx/sites-available/myproject' file:
server {
listen 80;
server_name 0.0.0.0;
location / {
include proxy_params;
proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
}
}
My configurations of myproject.service:
[Unit]
Description=Gunicorn instance to serve myproject
After=network.target
[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myproject
Environment="PATH=/home/sammy/myproject/myprojectenv/bin"
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target
my nginx and myporject status id "active" and have no errors
I can't figure out what I'm doing wrong, because I've read a lot of guides and documentation, the result is still unsuccessful
Once again, if I work without nginx, then everything works fine, but I need to connect it too

Related

nginx how to host both react and django

i have a react frontend imported inside a django backend. communication between the two is done through django-rest-framework. on the react's side, fetching is done through relative paths therefore in my package.json i have added the line:
"proxy": "http://127.0.0.1:8000",
django is hosting react-app locally without problems when i run: python3 manage.py runserver.
on the remote i am trying to use nginx with gunicorn to deploy this app on aws ubuntu instance and run into the problem:
first, i'm running python3 manage.py collectstatic
later, i'm pointing nginx to that static_files for the index.html
success! nginx serves react static files
use gunicorn myapp.wsgi -b 127.0.0.1:8000 to run django
problem! nginx served react files do not fetch anything. fetch does not call for this local path but instead calls public ip of aws instance. also, i cannot simulate get/post requests to the django backend because i think nginx "covers" django's gunicorn generated paths.
please tell how can i connect nginx-served react frontedn to gunicorn run django
my nginx sites-enabled/example
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/ubuntu/fandigger/frontend/build/;
server_name public_ip_without_port;
location / {
try_files $uri $uri/ =404;
}
}

Trying to set up flask with nginx and gunicorn

I have followed a simple guide, and trying to show my flask application, but it wont show. I am running everything through ssh on a ubuntu server.
Have my flask app and wsgi.py in /var/www/application/
My nginx config is:
server {
listen 80;
server_name application;
access_log /var/log/nginx/application.access.log;
error_log /var/log/nginx/appliation.error.log;
location / {
include proxy_params;
proxy_pass http://unix:/var/www/application/application.sock;
}
}
I have symlinked the file to /etc/nginx/sites-enabled/
Then here is my gunicorn application config
located in: /etc/systemd/system/application.service
[Unit]
Description=application.service - A Flask application run with Gunicorn.
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/application/
ExecStart=/usr/bin/gunicorn --workers 3 --bind unix:/var/www/application.sock wsgi:app
[Install]
WantedBy=multi-user.target
Have checked all processes are running, and systemd status ok, but when i try to curl
http://application
I get nothing?
My app.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def home():
return "<h1>Nginx & Gunicorn</h1>"
wsgi.py
from main import app
if __name__ == "__main__":
app.run(debug=True)
What am I missing here, how to get it working?
ExecStart=/usr/bin/gunicorn --workers 3 --bind unix:/var/www/application.sock wsgi:app
the problem is the path /var/www/application.sock, it should be: /var/www/application/application.sock
You can still view the status of the service with the command:
sudo systemctl status <service_name>
To understand if the command specified in the service file is correct, or if the application has problems starting, try to start it directly from the command line:
cd /var/www/application/ && /usr/bin/gunicorn --workers 3 --bind unix:/var/www/application/application.sock wsgi:app

Multiple Django app using nginx and gunicorn on Ubuntu 14.04 trusty server

I am new to server configuration. I do some google and config django app using gunicorn and nginx on ubuntu 14.04 trusty server. For the first django app I use port number 80 and my configfiles are :
/etc/init/gunicorn.conf :-
description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid
setgid www-data
chdir /home/myserver/my_virtualenv_path/myproject
exec /home/myserver/my_virtualenv_path/myproject/gunicorn --workers 2 --bind unix:/home/myserver/my_virtualenv_path/myproject/myproject.sock myproject.wsgi:application
My nginx configuration file for first django app:
/etc/nginx/site-available :-
server {
listen 80;
server_name myapp.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myserver/my_virtualenv_path/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/myserver/my_virtualenv_path/myproject/myproject.sock;
}
}
After that, i link site to site-enabled .
Next, i create a new django app inside the first django app virtualenv like:
FirstApp_Virtual_Env\first_djangoapp\app files
FirstApp_Virtual_Env\Second_djangoapp\app files
Now i configure gunicorn for second app like :
/etc/init/gunicorn_t :-
description "Gunicorn application server handling myproject2"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid
setgid www-data
chdir /home/myserver/my_virtualenv_path/myproject2
exec /home/myserver/my_virtualenv_path/myproject/gunicorn --workers 2 --bind unix:/home/myserver/my_virtualenv_path/myproject2/myproject2.sock myproject2.wsgi:application
My nginx configuration file for second django app:
/etc/nginx/site-available :-
server {
listen 8000;
server_name myapp2.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myserver/my_virtualenv_path/myproject2;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/myserver/my_virtualenv_path/myproject2/myproject2.sock;
}
}
After that i link site to site-enabled .
Now here is my problem: when i type myapp.com then my first django app is working fine but for second app when i type myapp2.com its showing nginx page and when i type myapp2.com:8000 it's working fine . I do some google for that but i am unable to find solution. I am newbie to this so please give me a hint for that how to correct my problem. Thanks for your time.
You configured nginx to serve myapp2.com on port 8000:
server {
listen 8000;
server_name myapp2.com;
# ...
}
so why would you expect nginx to serve it on port 80 ?
[edit] I thought the above was enough to make the problem clear but obviously not, so let's start again:
You configured nginx to serve myapp2.com on port 8000 (the listen 8000; line in your conf, so nginx do what you asked for: it serves myapp2.com on port 8000.
If you want nginx to serve myapp2.com on port 80 (which is the implied default port for http so you don't have to specify it explicitely in your url - IOW 'http://myapp2.com/' is a shortcut for 'http://myapp2.com:80/'), all you have to do is to configure nginx to serve it on port 80 just like you did for 'myapp.com': replace listen 8000; by listen 80;.
If you don't type in a port, your client will automatically use port 80.
Typing myapp2.com is the same as typing myapp2.com:80
But myapp2.com is not running on port 80, it's running on port 8000.
When you go into production it is possible to redirect myapp2.com to port 8000 without explicitly typing it. You register myapp2.com with a DNS name server and point it towards myapp2.com:8000

sock file is missing in running django app with nginx and gunicorn

I was following this tutorial and successfully ran the project manually. However, after setting up nginx and systemd, it says 502 Bad Gateway.
I have looked through other similar threads to no avail.
I see that my gunicorn workers are running by doing ps -ax | grep gunicorn.
my nginx configuration:
server {
listen 8000;
server_name 127.0.0.1;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/myproject/myproject.sock;
}
}
and the systemd file:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/myproject
ExecStart=/home/ubuntu/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
The contents of /var/log/nginx/error.log:
2017/02/18 17:57:51 [crit] 1977#1977: *6 connect() to unix:/home/ubuntu/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: 10.0.2.2, server: 172.30.1.5, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/myproject/myproject.sock:/", host: "localhost:8000"
Manually running /home/ubuntu/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/myproject/myproject.sock myproject.wsgi:application is also working. That is, sock file is created.
I feel like I missing something very simple.
What could I be doing wrong?
There could be an ordering thing here - nginx is probably starting before gunicorn, so the socket is not yet there to connect. You should add gunicorn.service to the After directive in the nginx systemd file.

Gunicorn + Nginx Internal server error while trying to apply code changes

I set up a VPS (django + gunicorn + nginx ) and it was working fine showing default django screen, but after I updated my django code and made all migrations I thought that now I need to restart gunicorn to apply changes.
So I did this:
sudo systemctl restart gunicorn
After this I've got Internal Server Error
Everything is set up like here
gunicorn.service :
[Unit]
Description=gunicorn daemon
After=network.targett
[Service]
User=thekotik
Group=www-data
WorkingDirectory=/home/thekotik/glboy
ExecStart=/home/thekotik/glboy/denv/bin/gunicorn --workers 3 --bind unix:/home/thekotik/glboy/closer.sock closer.wsgi:application
[Install]
WantedBy=multi-user.target
<class 'socket.error'>, [Errno 13] Permission denied: file: /usr/lib/python2.7/socket.py line: 228
unix socket files holds the same case unix file permissions; the file ( /home/thekotik/glboy/closer.sock) is currently being used or not owned by gunicorn process user that causes error 13.
I recommend using the TCP option --bind 127.0.0.1:8000.For tcp based changes,
gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.targett
[Service]
User=thekotik
Group=www-data
WorkingDirectory=/home/thekotik/glboy
ExecStart=/home/thekotik/glboy/denv/bin/gunicorn --workers 3 --bind 127.0.0.1:9090 closer.wsgi:application
[Install]
WantedBy=multi-user.target
use above systemd service file for gunicorn & change the proxy option in nginx to
proxy_pass http://127.0.0.1:9090$request_uri;
that will avoid unix socket file based permission problems.

Categories

Resources