running uwsgi as master won't handle requests - python

I have the following set up
src
|
|--flask_app
|--|--controllers.py
|--|--provider.py
|--|--__init__.py
|--config.py
|--wsgi.py
|--myproject.ini
|--myproject.sock
init.py creates the flask application
from flask import Flask, g
from flask_app.controllers import api_module
from flask_app.provider import CassandraDbProvider
# Define WSGI application object
app = Flask(__name__)
# Configure it
app.config.from_object('config')
cassandra = CassandraDbProvider(**app.config['DATABASE_CONNECT_OPTIONS'])
#app.before_request
def before_request():
g.db = cassandra
app.register_blueprint(api_module)
controler.py has the views that run on endpoints and also creates the blueprint
Finally wsgi.py has the following code
from cassandra_flask_app import app as application
if __name__ == "__main__":
application.run(host="0.0.0.0", port=5000)
myproject.ini
[uwsgi]
module = wsgi
logto = /var/log/uwsgi/uwsgi.log
threads = 10
socket = myproject.sock
chmod-socket = 664
vacum = true
die-on-term = true
Upstart script
description "uWSGI server instance configured to serve myproject"
start on runlevel [2345]
stop on runlevel [!2345]
setuid myuser
setgid www-data
env PATH=/home/myuser/myproject/myprojectenv/bin
chdir /home/myuser/myproject/src
exec uwsgi --ini myproject.ini
and nginx
server {
listen 80;
server_name myIpHere;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/myuser/myproject/src/myproject.sock;
}
}
controller.py
api_module = Blueprint('my_api', __name__, prefix='/api') # this might be wrong now because I don't have code infront of me
#myvmip/api/ works fine when uwsgi is master
#api_module.route('/', methods=['GET']):
def test_url():
return 'c'
#myvmip/api/normal_view/?query1=some_value" doesn't work when in master with no error. Only connection timeout error in nginx error.log.
#api_module.route('/nomral_view/', methods=['GET'])
def normal_view():
get_parameter = request.args.get('query1')
#uses g.db to connect to database and fetch some results
#database is cassandra db
return jsonify(**json)
It runs good on my developement machine. On my vm I load my flask application using nginx and uwsgi. I have set up uwsgi as described on many tutorial on the internet. The problem is that If I run uwsgi as master then It won't access some of my urls. Disabling it works properly. What do you think it could be? Does it matter uwsgi isn't loaded as master?

You need to add the following to your myproject.ini file
chdir = /path/to/project
callable = application
http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html#deploying-flask
Also if you have a virtualenv you should add
venv=/path/to/venv

Related

Can not reach Flask app served with uWSGI and Nginx

I have a Flask back end that is functional without using uwsgi and nginx.
I'm trying to deploy it on an EC2 instance with its front-end.
No matter what I do, I can't reach the back-end. I opened all the ports for testing purposes but that does not help.
Here's my uwsgi ini file:
[uwsgi]
module = main
callable = app
master = true
processes = 1
socket = 0.0.0.0:5000
vacuum = true
die-on-term = true
Then I use this command to start the app:
uwsgi --ini uwsgi.ini
The message returned is
WSGI app 0 (mountpoint='') ready in 9 seconds.
spawned uWSGI worker 1 (and the only) PID: xxxx
Then here is my Nginx conf file:
server {
server_name my_name.com www.ny_name.com
location / {
root /home/ubuntu/front_end/dist/;
}
location /gan {
proxy_pass https:localhost:5000/gan;
}
## below https conf by certbot
}
If my understanding is correct, whenever a request reaches "my_name.com/gan..." it will be redirected to the localhost on the port 5000 where the back-end is started by uwsgi.
But I can't reach it. I'm trying to simply do a get request on "my_name.com/gan" on my browser (it should return a random image) but I get a 502 by nginx.
Important to note, the front-end works fine and I can access it on browser.
My guess is that url is not in proper form
Try
proxy_pass http://0.0.0.0:5000;

504 Connection Error Flask Nginx uWSGI Ubuntu

Hello all I was hoping I could receive some guidance on this matter.
I have a flask application that is setup on an ubuntu server. It uses ssh to create a tunnel to a Centos 7 Server that has it's mysql database. Upon running this application with python on the Ubuntu Server I'm able to perfectly login to my application and view data from database from domain ip. Now upon trying to run the application on nginx and uWSGI I can actually get to the login page from my domain name. But upon entering my credentials and trying to login, the page loads for around a minute and the I receive the 504 Connection Time Out Error
Would I be receiving this because my application is trying to reach out to another server while processing data from me. I'm not sure and nothing has been a help yet. Here are my files
server block
server {
listen 80;
server_name itinareport.tk www.itinareport.tk;
location / {
uwsgi_read_timeout 600;
include uwsgi_params;
uwsgi_pass unix:/home/pinchrep2/itinarep/itinarep.sock;
}
}
ini file
[uwsgi]
module = wsgi:app
master = true
processes = 5
socket = itinarep.sock
chmod-socket = 660
vacuum = true
die-on-term=true
wsgi.py
from main import app
if __name__ == "__main__":
app.run()
service file
[Unit]
Description=uWSGI instance to serve itinarep
After=network.target
[Service]
User=pinchrep2
Group=www-data
WorkingDirectory=/home/pinchrep2/itinarep
Environment="PATH=/home/pinchrep2/itinarep/it_venv/bin"
ExecStart=/home/pinchrep2/itinarep/it_venv/bin/uwsgi --ini itinarep.ini
[Install]
WantedBy=multi-user.target
Here is where I ssh from main py file
main.py
sshforward = SSHTunnelForwarder(
("public ip", 22),
ssh_username = 'user',
ssh_password = 'pass',
remote_bind_address = ('127.0.0.1', 3306)
)
sshforward.start()
local_port = sshforward.local_bind_port
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+pymysql://root#localhost:{local_port}/asteriskcdrdb"
if __name__ == "__main__":
app.run(host='0.0.0.0')
Again I just need this to be deployed. Please point in the right direction configuration wise. I can get to application but as soon as logging in I receive this issue.
When your database connection url references "localhost", its really connecting via a unix socket.
You can connnect using a local_bind_address containing a unix socket adding ?unix_socket=/path/to/mysql.sock to the SQLALCHEMY_DATABASE_URI like this answer.
Seems connecting to a remote unix socket is waiting for this upstream issue to be implemented.

502 bad gateway error on nginx server when using flask restplus

This error occurs for me solely when using flask restplus with an nginx server. The server works correctly when just flask is used and the flask-restplus api works fine when running the app locally or even with:
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:application
on my EC2 instance.
I have tried using gunicorn along side nginx (rather than uwsgi) but I get the same error in my /var/log/nginx/error.log file:
2017/07/03 20:30:02 [crit] 957#957: *1 connect() to unix:/home/jamie/my_app/my_app.sock failed
(2: No such file or directory) while connecting to upstream,
client: #.#.#.#, server: #.#.#.#, request: "GET /favicon.ico HTTP/1.1", upstream:
"uwsgi://unix:/home/jamie/my_app/my_app.sock:", host: "#.#.#.#", referrer: "http://#.#.#.#/"
The app is envoked through a wsgi.py that looks like this:
from main import application
if __name__ == "__main__":
application.run('0.0.0.0')
And the main.py file contains the following:
from boto3 import resource
from flask import json, request, abort
from flask import Flask
from flask_restplus import Api, Resource, fields
application = Flask(__name__)
api = Api(application)
#api.route('/users', methods=['GET', 'POST'])
class Users(Resource):
def get(self):
return users_get()
#api.expect(resource_fields)
def post(self):
return user_login()
#api.route('/users/<string:Username>', methods=['GET', 'PUT', 'DELETE'])
class User(Resource):
def get(self, Username):
return user_get(Username)
def put(self, Username):
return create_user(Username)
def delete(self, Username):
return delete_user(Username)
In order to configure the server I followed this digitalocean tutorial and my config is essentially the same. If anyone wants to see it I can provide it.
My uwsgi ini file is as follows:
[uwsgi]
module = wsgi:application
master = true
processes = 5
socket = my_app.sock
chmod-socket = 666
vacuum = true
die-on-term = true
My nginx sever config is at etc/nginx/sites-available/my_app
like so:
server {
listen 80;
server_name #.#.#.#;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/jamie/my_app/my_app.sock;
}
}
And finally my /etc/systemd/system/fitness_test_app.service file is like so:
[Unit]
Description=uWSGI instance to serve my_app
After=network.target
[Service]
User=jamie
Group=www-data
WorkingDirectory=/home/jamie/my_app
Environment="PATH=/home/jamie/my_app/myprojectenv/bin"
ExecStart=/home/jamie/my_app/myprojectenv/bin/uwsgi --ini my_app.ini
[Install]
WantedBy=multi-user.target
If anyone has any idea what could cause this problem an answer would be greatly appreciated as I have been unable to solve this issue for some time.

Running a Flask app on nginx

I don't know exactly what I am doing but I am experimenting with running Flask on nginx. I am boiling it down to the simple bit of code below. First I have a test app in Flask like this:
from flask import Flask, render_template
app = Flask(__name__, static_folder='client', template_folder='client/html')
def show_home_page():
return render_template("home.html")
#app.route('/')
def server():
return show_home_page()
if __name__ == '__main__':
app.run(threaded=True)
If I run python app.py I can go to http://localhost:5000 and see the "Hello World". Next I read that I need to run uwsgi but its not clear what params I need to pass to it. I tried different things like:
uwsgi -s /tmp/app.sock --manage-script-name --mount ./=app:app
I also noted that I need to set my nginx conf file to match, but I am stuck on that as well (I just get a welcome from nginx on port 5000) and it doesnt seem to link with my Flask app. I googled around a bit but nothing has clicked yet.
server {
listen 5000;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
You can use refer this for Flask with Nginx and uWSGI:
Python flask with Nginx and uWSGI

Flask with gevent multicore

What is the clear way to run flask application with gevent backend server and utilize all processor cores? I have idea to run multiple copies of flask application where gevent WSGIServer listen one port in diapason 5000..5003 (for 4 processes) and nginx as load balancer.
But I'm not sure that this way is the best and may be there are some other ways to do it. For example, master process listen one port and workers process incoming connections.
I'll take a shot!
Nginx!
server section:
location / {
include proxy_params;
proxy_pass http://127.0.0.1:5000;
}
Flask App
This is a simple flask app that i will be using for this example.
myapp.py:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
uWSGI
Okay so I know that you said that you wanted to use gevent, but if you are willing to compromise on that a little bit I think you would be very happy with this setup.
[uwsgi]
master = true
plugin = python
http-socket = 127.0.0.1:5000
workers = 4
wsgi-file = myapp.py
callable = app
Gunicorn
If you must have gevent you might like this little setup
config.py:
import multiprocessing
workers = multiprocessing.cpu_count()
bind = "127.0.0.1:5000"
worker_class = 'gevent'
worker_connections = 30
Then you can run:
gunicorn -c config.py myapp:app
Thats right you have a worker for each cpu and 30 connections per worker.
See if that works for you.
If you are really sold on using nginx as a load balancer try something like this in your http section
upstream backend {
server 127.0.0.1:5000;
server 127.0.0.1:5002;
server 127.0.0.1:5003;
server 127.0.0.1:5004;
}
then one of these in the server section
location / {
include proxy_params;
proxy_pass http://backend;
}
Good Luck buddy!

Categories

Resources