504 Connection Error Flask Nginx uWSGI Ubuntu - python

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.

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;

Connection refused on socket.connect (Django app deployed on uWSGI + Nginx)

I have a Django application deployed on a VM using uWSGI & Nginx setup.
I would like to print some data, by passing the required information to a printer that is configured on the same network using a socket:
printer_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
printer_socket.connect(('10.125.125.2', 9001))
printer_socket.send(bytes(source='data', encoding='utf-8'))
Note: the IP address and the port are here for illustration purposes.
Problem: I get a Err 111 Connection refused error, however. The error is triggered at the printer_socket.connect step.
Additional information: The python code that initializes the socket, connects to the required IP address/Port and sends the data works fine when it's run from the python interactive shell.
Question: What do I need to configure in order to allow opening sockets from a django web application, deployed using uWSGI and Nginx?
Please keep in mind that the configuration of the project is out of the scope of this question. I don't have troubles configuring the app. The app works fine. I am specifically interested in how to allow opening sockets from a web app, served using uWSGI + Nginx setup
UPDATE
Here's the .ini configuration file for the uWSGI.
[uwsgi]
project = App
uid = user
base = /home/%(uid)
chdir = %(base)/%(project)
home = %(base)/Venv/%(project)
module = %(project).wsgi:application
# daemonize = %{base}/uwsgi/%{project}.log
logto = /home/user/logs/uwsgi/%{project}.log
master = true
processes = 5
socket = /run/uwsgi/%(project).sock
chown-socket = %(uid):www-data
chmod-socket = 777
vacuum = true
buffer-size=32768
Thank you.

uwsgi nginx connection to unix socket refused

I'm trying to migrate django app from ubuntu 14.04 to raspberry pi ( raspbian os)
for ubuntu i have done http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html and it worked.
in raspbian it's not so simple.
this is my bills_nginx.conf in /etc/nginx/sites-enabled
bills_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:/var/www/html/bills/bills/bills.sock; # for a file socket
#server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name 192.168.5.5; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /var/www/html/bills/bills/bills/media; # your Django project's media files - amend as required
}
location /static {
alias /var/www/html/bills/bills/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /var/www/html/bills/bills/uwsgi_params; # the uwsgi_params file you installed
}
}
and this is my UWSGI INI file:
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /var/www/html/bills/bills
# Django's wsgi file
module = bills.wsgi
# the virtualenv (full path)
home = /home/seb/.virtualenvs/bills3
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /var/www/html/bills/bills/bills.sock
# ... with appropriate permissions - may be needed
uid =www-data
gid=www-data
chown-socket=www-data:www-data
chmod-socket = 666
# clear environment on exit
vacuum = true
daemonize=/var/log/uwsgi/bills3.log
error_log=/var/log/nginx/bills3_error.log
in error.log I get:
2017/03/08 10:27:43 [error] 654#0: *1 connect() to unix:/var/www/html/bills/bills/bills.sock failed (111: Connection refused) while connecting to upstream, client: 192.168.5.2, server: 192.168.5.5, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/var/www/html/bills/bills/bills.sock:", host: "192.168.5.5:8000"
please help me get it working :)
chmod-socket, chown-socket, gid, uid, socket For uWSGI and nginx to communicate over a socket, you need to specify the permissions and the owner of the socket. 777 as chmod-socket is much too liberal for production. However, you may have to mess around with this number to get it correct, so everything necessary can communicate. If you don’t take care of your socket configurations, you will get errors such as:
So make sure the permission of the folder ..
I think better way
$ sudo mkdir /var/uwsgi
$ sudo chown www-data:www-data /var/uwsgi
And change the socket path
upstream django {
server unix:/var/uwsgi/bills.sock; # for a file socket
#server 127.0.0.1:8001; # for a web port socket (we'll use this first) }
More reference : Pls check A great article
http://monicalent.com/blog/2013/12/06/set-up-nginx-and-uwsgi/
Also I have the same issue before may you can check my configuration too
nginx django uwsgi page not found error

402 Bad Request using Python, Nginx, and flask on a Raspberry Pi

I am trying to get my Python application to run on port 80 so I can host my page to the Internet and see my temperature and all that remotely.
I get a 402 Bad Request error and I can't seem to figure out why. It seems it's having trouble writting my .sock file to a temp directory.
I am following this tutorial.
https://iotbytes.wordpress.com/python-flask-web-application-on-raspberry-pi-with-nginx-and-uwsgi/
/home/pi/sampleApp/sampleApp.py
from flask import Flask
first_app = Flask(__name__)
#first_app.route("/")
def first_function():
return "<html><body><h1 style='color:red'>I am hosted on Raspberry Pi !!!</h1></body></html>"
if __name__ == "__main__":
first_app.run(host='0.0.0.0')
/home/pi/sampleApp/uwsgi_config.ini
[uwsgi]
chdir = /home/pi/sampleApp
module = sample_app:first_app
master = true
processes = 1
threads = 2
uid = www-data
gid = www-data
socket = /tmp/sample_app.sock
chmod-socket = 664
vacuum = true
die-on-term = true
/etc/rc.local just before exit 0
/usr/local/bin/uwsgi --ini /home/pi/sampleApp/uwsgi_config.ini --uid www- data --gid www-data --daemonize /var/log/uwsgi.log
/etc/nginx/sites-available/sample_app_proxy and I verified this moved to sites-enabled after I linked it.
server {
listen 80;
server_name localhost;
location / { try_files $uri #app; }
location #app {
include uwsgi_params;
uwsgi_pass unix:/tmp/sample_app.sock;
}
}
I got all the way to the final step with 100 percent success. After I linked the sample_app_proxy file so it gets copied into /nginx/sites-enabled/ I do a service nginx restart. When I open my browser 'localhost' I get a 502 Bad Request.
I noticed in the nginx logs at the bottom that there was an error.
2017/01/29 14:49:08 [crit] 1883#0: *8 connect() to unix:///tmp/sample_app.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:///tmp/sample_app.sock:", host: "localhost", referrer: "http://localhost/"
My source code is exactly as you see it in the tutorial, I checked it over many times.
I looked at the /etc/logs/uwsgi.log and found this message at the bottom.
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7336
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
The -s/--socket option is missing and stdin is not a socket.
I am not sure what is going on and why it doesn't seem to write the .sock file to the /tmp/ directory. The test I did earlier in the tutorial worked fine and the sample_app.sock file showed up in /tmp/ But when I run the application it doesn't seem to work.
I did a lot of searching and I saw many posts saying to use "///" instead on "/" in the /etc/nginx/sites-available/sample_app_proxy file, but whether I use one or three, I still get the 502 error.
uwsgi_pass unix:///tmp/sample_app.sock;
Any help would be greatly appreciated as this is the last step I need to accomplish so I can do remote stuff to my home. Thanks!

running uwsgi as master won't handle requests

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

Categories

Resources