Python Django+Nginx+uwsgi 502 Bad Gateway - python

Centos7,when I connect to my website, shows 502 Bad Gateway,
I test my website with command
uwsgi --ini
systemctl start nginx
And I cant figure out what's happened,please help me!
here's nginx.conf
upstream django {
server 127.0.0.1:8000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com;
charset utf-8;
include /etc/nginx/default.d/*.conf;
location / {
include uwsgi_params;
uwsgi_pass django;
}
location /static/ {
alias /usr/local/etc/dmp/static/;
}
}
and uwsgi setting
[uwsgi]
chdir = /usr/local/etc/dmp
module = DMP_python.wsgi
plugins = python3
socket = :8000
chmod-socket = 666
master = true
processes = 2
vacuum = true

You're using the wrong setting to tell uwsgi to use an HTTP port. You need http-socket rather than socket.

There can be multiple reasons for which an upstream might return invalid or even do not return any response
Verify if upstream uwsgi is actually running locally in the centos instance and can handle incoming requests
for this to verify run it as http-socket = :8000 in uwsgi.ini and then run uwsgi --ini uwsgi.ini
if uwsgi running fine on localhost, then change config back to socket = :8000
On centos 7.x SELinux package is enabled and runs in enforcing mode. So it won't allow nginx to write/connect to a socket.
verify if SELinux has the policy for nginx to write to sockets
check if read/connect/write to socket is allowed grep nginx /var/log/audit/audit.log | audit2allow -m nginx
do so by grep nginx /var/log/audit/audit.log | audit2allow -M nginx
and finally semodule -i nginx.pp
permission to connect to socket issue should be resolved by now
verify if nginx can do network connection with upstream. Check nginx error.log or getsebool -a | grep httpd
to allow it run setsebool httpd_can_network_connect on -P

Related

Flask: Not able to access requests using nginx and directly I am able to access

I am setting up Flask, uwsgi, and nginx
Below is the config setup for uwsgi
[uwsgi]
module = wsgi:app
master = true
processes = 5
protocol = http
socket = 0.0.0.0:8443
buffer-size=32768
die-on-term = true
enable-threads = true
vacuum = true
When I try to Get Request , it is working fine
# curl --location --request GET 'http://10.1.1.10:8443/info?id=1&subid=2'
I have created ubuntu (docker container)
# access from nginx docker container with same curl working fine
# curl --location --request GET 'http://10.1.1.10:8443/info?id=1&subid=2'
Next, I have installed nginx, configured like below
cat /etc/nginx/sites-available/flaskconfig
server {
# the port your site will be served on
listen 80;
# the IP Address your site will be served on
server_name 10.1.1.10;
# Proxy connections to application server
location / {
include uwsgi_params;
uwsgi_pass 10.1.1.10:8443;
}
}
Created Linked file
# mkdir /etc/nginx/sites-enabled
# ln -s /etc/nginx/sites-available/flaskconfig /etc/nginx/sites-enabled/mysite.com
Restarted nginx service and service is running
I also Cross verified nginx with "nginx -t" command, config is good
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
It is not working, when I tried to access from nginx proxy and below is curl command
# curl --location --request GET 'http://10.1.1.10:8080/info?id=1&subid=2'
Note: why 8080 port is because nginx host container is started from -p 8080:80 and also tried with port 80 in above same curl , no luck
I guess, It is something wrong with nginx config/setup and I am unable to figure it out, struck from almost 2 days, Any help is really appreciated
message /var/log/nginx/error.log
2020/12/04 17:18:17 [error] 1195#1195: *3 upstream prematurely closed connection while reading response header from upstream, client: 172.10.1.16, server: 10.1.1.10, request: "GET /info?id=1&subid=2 HTTP/1.1", upstream: "uwsgi://10.1.1.10:8443", host: "10.1.1.10:8080"
Another Note:
Only Ngnix is running in Docker and both will accessing host and docker from outside is 10.1.1.10 IP, if we need to access ngnix i need pass 10.1.1.10:8080, if i need access uwsgi then 10.1.1.10:8443

Need to run django application in a domain name without specific port

I am newbie to Django recently I created a Django app and I uploaded to the server. I assigned a domain name to it. each time I run the server I need to type xyz.com:8000 to see my website. Is there any way to resolve this issue? Also, I have doubt. Do I need to type python manage.py runserver 0:8000 to launch the website or it's just run automatically like PHP.
You should set up a web server!
The web server is required for any site to work. Currently the most popular are Apache and NGINX. It is the web server that responds to user requests. We need to ensure the interaction of the web server and the python application. Most popular solutions:
uwsgi
gunicorn
Consider an example with Nginx and Gunicorn:
Let's start by installing the Gunicorn module in a virtual environment:
pip install gunicorn
Configure the gunicorn service settings for our project:
sudo nano /etc/systemd/system/gunicorn.service
/etc/systemd/system/gunicorn.service:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=my_user
Group=www-data
WorkingDirectory=/home/project_dir/project
ExecStart=/home/project_dir/project/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/project_dir/project/project.sock project.wsgi
[Install]
WantedBy=multi-user.target
We enable and run the gunicorn service, check its status:
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
sudo systemctl status gunicorn
If all is well, install the nginx web server:
sudo apt install nginx
Configure the project site parameters:
sudo nano /etc/nginx/sites-available/project
/etc/nginx/sites-available/project:
server {
listen 80;
server_name <server IP or domain name>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/project_dir/project;
}
location /media/ {
root /home/project_dir/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/project_dir/project/project.sock;
}
}
We use Nginx as a proxy for the gunicorn Python server:
proxy_pass http://unix:/home/project_dir/project/project.sock;
Create a link in the allowed sites folder “/etc/nginx/sites-enabled”:
sudo ln -s /etc/nginx/sites-available/project/etc/nginx/sites-enabled
We restart the Nginx service and add permissions to the firewall:
sudo systemctl restart nginx
sudo ufw allow 'Nginx Full'
Done! You can check the operation of our site by typing the IP address of the server in the browser.
P.S. Sorry for my english! If you see an error in the text or code, please edit me.

Forwarding Nginx port to gunicorn instance

I was following this tutorial to setup my flask server.
https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04#step-6-%E2%80%94-securing-the-application
When I got to step 6 I see that they are setting flask for the whole url but I would like to point it to a specific port.
This is the code I have for my nginx which points. This currenly produces a 404.
server {
listen 5000;
server_name site.com;
location / {
include proxy_params;
proxy_pass http://unix:/home/user/project/project.sock;
}
}
All the other files are the same as the tutorial. I have tried to modify the .sock file but it seems like it was generated automatically and it can't be modified. In addition I need to find a way for nginx to handle this before I worry about handling it from gunicorn.
My end goal is to have nginx foward requests to flask running when a request is sent to 0.0.0.0:5000 and have all other requests 0.0.0.0 , 0.0.0.0/* be handled by nginx.
Any help to undestand all this is really appreciated got lost at this point.
EDIT
my nginx configuration in sites-available
server {
server_name domain www.domain;
location / {
include proxy_params;
proxy_pass http://127.0.0.1:8080/;
}
}
If you want flask to be open to a port instead of a file you should override
[service] to
[service]
...
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind 127.0.0.1:8000 -m 007 wsgi:app
And change your nginx config to proxy_pass http://127.0.0.1:8000/;
This way you can have access to port 8000 for checking how are working gunicorn and flask. Remember to be careful with firewall rules to secure port 8000. For a good discussion on which one is better you can try: gunicorn + nginx: Server via socket or proxy?

nginx django uwsgi page not found error

I am trying to setup uwsgi and Django on Nginx but showing page not found error and error logs are empty.
I cannot identify the error because the error logs are empty.
Error log /var/log/nginx/error.log:
-rw-r--r-- 1 www-data root 0 Feb 26 12:31 error.log
uswgi is running properly because I tested this on following method:
uwsgi --http :8080 --home /home/flybegins/python/django/venv/ --chdir
/home/flybegins/python/django/sample -w sample.wsgi
virtual host
server {
listen 80;
server_name test.aaaaaaa.com;
error_log /var/log/nginx/error.log
location /static/ {
root /home/flybegins/python/django/sample/
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/flybegins/python/django/sample/sample.sock;
} }
Virtual host permission:
-rw-r--r-- 1 root root 333 Feb 27 08:54 test.aaaa.com
Thanks in advance!
You need to install python plugin for uwsgi
sudo apt-get install uwsgi-plugin-python
or for python 3
sudo apt-get install uwsgi-plugin-python3
You are running your project using port 8080 with this code:
uwsgi --http :8080 --home /home/flybegins/python/django/venv/ --chdir /home/flybegins/python/django/sample -w sample.wsgi
And you are trying to bind NGINX to a socket file that doesn't exist using this configuration:
location / {
include uwsgi_params;
uwsgi_pass unix:/home/flybegins/python/django/sample/sample.sock;
}
That why it doesn't work.
I did two mistakes One is nginx virtual host configuration and another one is socket permission error
uWSGI Configuration
[uwsgi]
project = prd
base = /home/flybegins/python/django
chdir = %(base)/%(project)
home = %(base)/venv
module = %(project).wsgi:application
master = true
processes = 5
gid = www-data
uid = www-data
socket = /var/uwsgi/%(project).sock
chmod-socket = 664
vacuum = true
To create the space for the socket to exist, you just have to pick a persistent directory (e.g. not /run or /tmp) and make www-data (the user nginx runs as) the owner of it, as such:
$ sudo mkdir /var/uwsgi
$ sudo chown www-data:www-data /var/uwsgi
My nginx virtual host configuration
server {
listen 80;
server_name testserver1.com;
access_log /home/flybegins/log/python/testserver1.com/access.log;
error_log /home/flybegins/log/python/testserver1.com/error.log error;
location /static {
alias /home/flybegins/python/django/prd/static_files/;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/var/uwsgi/prd.sock;
}
}

UWSGI + NGINX 502 Bad Gateway

I got a Web.py app and wanted to push it into production.
As recommended by the Web.py Community I decided to use uWSGI and Nginx for this.
My App uses Memcached for Session Storing and MySQL for other storing tasks. The app works fine on my MacBook.
I configured the uWSGI + Nginx setup previously which worked fine. But know I receive a 502 Bad Gateway when I try to access the Index Page on my Ubuntu Server.
BUT: when entering another page I receive all the content I wanted.
In general the app works fine in the Ubuntu environment, as I tested it by typing python app.py 8080. I was able to enter the page.tld:8080/ and receive all content.
My uWSGI config:
[uwsgi]
gid = www-data
uid = www-data
vhost = true
plugins = python
logdate
#socket = /tmp/uwsgi_vhosts.sock
socket = 127.0.0.1:3031
master = true
processes = 1
harakiri = 120
limit-as = 128
memory-report
no-orphans
The Nginx config:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
# Make site accessible from http://localhost/
server_name page.tld;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3031;
# This is the absolute path to the folder containing your application
uwsgi_param UWSGI_CHDIR /var/www/page.tld/apps;
# This is actually not necessary for our simple application,
# but you may need this in future
uwsgi_param UWSGI_PYHOME /var/www/page.tld/apps;
# This is the name of your application file, minus the '.py' extension
uwsgi_param UWSGI_SCRIPT test;
}
I keep getting this lines in the vhosts.log of uWSGI:
libgcc_s.so.1 must be installed for pthread_cancel to work
- DAMN ! worker 1 (pid: 1281) died, killed by signal 6 :( trying respawn ...
- Respawned uWSGI worker 1 (new pid: 1330)
Please let me know if you need to see other parts of the configuration.
And these lines in the error.log of nginx:
[error] 1233#0: *1 upstream prematurely closed connection while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: page.tld, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:3031", host: "page.tld"
Let me know if any other logs are needed to solve this.
Update: It seems that I get 502 Bad Gateway, when I want to access a page that has to load things from the MySQL Database. But as it is working without uWSGI & NGINX I guess that nginx kills the uwsgi instance for some reason when it tries to load things from the Database.
I recently fixed this problem by setting a higher memory limit within the uwsgi. You will need to restart the uwsgi. I am running uwsgi emperor at start up. So, in my case I rebooted.
[uwsgi]
...
limit-as = 512
System:
Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-43-generic x86_64)
mysqlclient==1.3.6

Categories