Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
This comment has been deleted
Justin, excellent article. Followed what was being done (at least conceptually), and made it right up to the end before having any kind of issue (working without errors before “Configure Nginx to Proxy to uWSGI”, but after no errors on starting processes, but I am getting 502 bad gateway error. Pretty much brand spankin’ new at Nginx/uWSGI although I gather from the message that my Nginx proxy is not communicating successfully to uWSGI. Any guidance/troubleshooting would be appreciated.
Here is what I added to my nginx.conf in “Configure Nginx to Proxy to uWSGI” step:
# Digital Ocean uwsgi/nginx/python on CentOS 7 example.
server {
listen 80;
server_name 172.20.1.42;
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/myapp.sock;
}
}
This was added just before the default server block. Don’t know if this will help, but here is output of systemctl status command for both nginx and uwsgi:
● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2016-10-18 18:56:49 EDT; 4s ago Process: 19786 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 19781 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 19779 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 19787 (nginx) CGroup: /system.slice/nginx.service ├─19787 nginx: master process /usr/sbin/nginx ├─19788 nginx: worker process ├─19789 nginx: worker process ├─19790 nginx: worker process ├─19791 nginx: worker process ├─19792 nginx: worker process └─19793 nginx: worker process
Oct 18 18:56:49 ayoder3 systemd[1]: Starting The nginx HTTP and reverse proxy server… Oct 18 18:56:49 ayoder3 nginx[19781]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok Oct 18 18:56:49 ayoder3 nginx[19781]: nginx: configuration file /etc/nginx/nginx.conf test is successful Oct 18 18:56:49 ayoder3 systemd[1]: Started The nginx HTTP and reverse proxy server.
● uwsgi.service - uWSGI instance to server myapp Loaded: loaded (/etc/systemd/system/uwsgi.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2016-10-18 18:50:13 EDT; 26min ago Process: 19753 ExecStartPre=/usr/bin/bash -c mkdir -p /run/uwsgi; chown ayoder:nginx /run/uwsgi (code=exited, status=0/SUCCESS) Main PID: 19757 (bash) CGroup: /system.slice/uwsgi.service ├─19757 /usr/bin/bash -c cd /home/ayoder/myapp; source myappenv/bin/activate; uwsgi --ini myapp.ini ├─19761 uwsgi --ini myapp.ini ├─19762 uwsgi --ini myapp.ini ├─19763 uwsgi --ini myapp.ini ├─19764 uwsgi --ini myapp.ini ├─19765 uwsgi --ini myapp.ini └─19766 uwsgi --ini myapp.ini
Oct 18 18:50:13 ayoder3 bash[19757]: mapped 436560 bytes (426 KB) for 5 cores Oct 18 18:50:13 ayoder3 bash[19757]: *** Operational MODE: preforking *** Oct 18 18:50:13 ayoder3 bash[19757]: WSGI app 0 (mountpoint=‘’) ready in 0 seconds on interpreter 0x77ed30 pid: 19761 (default app) Oct 18 18:50:13 ayoder3 bash[19757]: *** uWSGI is running in multiple interpreter mode *** Oct 18 18:50:13 ayoder3 bash[19757]: spawned uWSGI master process (pid: 19761) Oct 18 18:50:13 ayoder3 bash[19757]: spawned uWSGI worker 1 (pid: 19762, cores: 1) Oct 18 18:50:13 ayoder3 bash[19757]: spawned uWSGI worker 2 (pid: 19763, cores: 1) Oct 18 18:50:13 ayoder3 bash[19757]: spawned uWSGI worker 3 (pid: 19764, cores: 1) Oct 18 18:50:13 ayoder3 bash[19757]: spawned uWSGI worker 4 (pid: 19765, cores: 1) Oct 18 18:50:13 ayoder3 bash[19757]: spawned uWSGI worker 5 (pid: 19766, cores: 1)
For posterity, I was getting a 502 error, as well. It turns out it was SELinux rather than nginx or uwsgi. The solution is fairly simple:
sudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
sudo semodule -i mynginx.pp
This allows nginx access to the appropriate port.
See https://stackoverflow.com/questions/23948527/13-permission-denied-while-connecting-to-upstreamnginx for more details.
This article is great!! I could get everything to work fine mimicking this example. I did receive the 502 bad gateway error mentioned in these comments, but after fixing it with the audit2allow command, it all worked.
====================================================
However, then I went through the exact same steps with my actual Flask app, just changing the wsgi.py script to
from mymodule import app
if __name__ == "__main__":
app.run()
as you can see here: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-14-04
====================================================
But then it did not work. More specifically, I could properly do the following:
Get everything to work with just the uwsgi CLI command: uwsgi --socket 0.0.0.0:8080 --protocol=http -w wsgi
Get the uwsgi services to work fine, like by using: sudo systemctl start uwsgi and validating with sudo systemctl status uwsgi
Get nginx to work OK, validating via sudo nginx -t, sudo systemctl start nginx, and sudo systemctl status nginx
====================================================
But then I had the 502 bad gateway error. However, at this point I could not fix the problem with the audit2allow command. More interestingly, I could not find any denials in the logs. That is,
sudo cat /var/log/audit/audit.log | grep nginx | grep denied
came back empty. So I do not understand why I am getting the bad gateway.
My app had some POST and GET methods, neither of them would work.
Help!!
Hi, Many thanks for writing up this article. This is very useful article which is good starting point for people who want to have uwsgi, Nginx and Flask. There are few very important thing which are missing from above article. Create group and user in CentOS first.
Here you can check the error in Nginx: #sudo tail -30 /var/log/nginx/error.log
I have successfully setup **Nginx, uwsgi and Flask in virtual environment. **
Let me know if anyone want to have any help. I spend a lot of time to understand this. I hope that I am now comfortable to help others.
Thanks
● uwsgi.service - uWSGI instance to serve myapp Loaded: loaded (/etc/systemd/system/uwsgi.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Tue 2019-02-26 22:08:41 MST; 6s ago Process: 24067 ExecStart=/usr/bin/bash -c cd /root/myapp; source root/avinash/avi/bin/activate; uwsgi --ini myapp.ini (code=exited, status=127) Process: 24064 ExecStartPre=/usr/bin/bash -c mkdir -p /run/uwsgi; chown user:root /run/uwsgi (code=exited, status=1/FAILURE) Main PID: 24067 (code=exited, status=127)
im facing problem please anyone help me out