Getting "502 Bad Gateway" with nginx, uwsgi python-flask on ubuntu 16.04

I follow this to deploy a flash application (simple hello world) on Ubuntu 16-04. digital ocean tutorial

Everything is working fine until uWSGI service testing . After that, I performed the described step and, when I finally reached the bottom level and checked the IP address of the server, I got:

502 Bad Gateway 

Good. I searched and checked my error log, I got the following: -

 2017/01/16 05:29:27 [crit] 20714#20714: *2 connect() to unix:/home/sajjan/project/project.sock failed (2: No such file or directory) while connecting to upstream, client: xx.9.xxx.xxx, server: 138.xxx.xx.xxx, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/home/sajjan/project/project.sock:", host: "xx.xx.xx.xx" 

So, after registering the error log, I created the project.sock file manually. Go to the server IP again and then the same error "502 Bad Gateway"

I checked the error log again and found this

 2017/01/16 06:07:11 [crit] 20874#20874: *1 connect() to unix:/home/sajjan/project/project.sock failed (13: Permission denied) while connecting to upstream, client: 47.9.237.113, server: XX.XX.XX.XX, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/home/sajjan/project/project.sock:", host: " XX.XX.XX.XX " 

I found out the permission question and changed the resolution using the following command

 sudo chmod 666 project.sock 

Now I checked the resolution (using ls -l filename)

 -rw-rw-rw- 1 root root 0 Jan 16 05:31 project.sock 

Now I am returning to check the IP address of the server, but found the same "502 Bad Gateway". Again I checked the error log and found this:

 017/01/16 06:13:31 [error] 20897#20897: *6 connect() to unix:/home/sajjan/project/project.sock failed (111: Connection refused) while connecting to upstream, client: 47.9.237.113, server: XX.XX.XX.XX, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/home/sajjan/project/project.sock:", host: " XX.XX.XX.XX ", referrer: "http:// XX.XX.XX.XX /" 

Over the past two days I have been looking for errors higher than the last two days, but nothing seemed to me. I checked these answers, but stackanswer-1 stackanswer-2 did not help, and with them I checked the entire digital community stream, but nothing works.

I am completely starting servers and know little about ubuntu. If you can help me figure out what I'm doing wrong, or suggest some of the best tutorials / ways to deploy my flash drive application, then I would really appreciate it.

These are my files.

  • hello.py

     from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "<h1 style='color:blue'>Hello There!</h1>" if __name__ == "__main__": app.run(host='0.0.0.0') 
  • project.ini

     [uwsgi] module = wsgi:app master = true processes = 5 socket = /home/sajjan/project/project.sock chmod-socket = 660 vacuum = true die-on-term = true 
  • wsgi.py

     from hello import app if __name__ == "__main__": app.run() 
  • Below is the file: / etc / nginx / sites-available / project

     server { listen 80; server_name 138.197.28.107; location / { include uwsgi_params; uwsgi_pass unix:/home/sajjan/project/project.sock; } } 

When I run the command:

  sudo service uwsgi restart 

output:

  Failed to restart wsgi.service: Unit wsgi.service not found. 

and conclusion

  sudo service nginx status/restart 

then it shows that nginx is working.

Help me, if there is anything else you want to know, let me know. thanks

EDIT:

I created a project.service file and its code:

  [Unit] Description=uWSGI instance to serve project After=network.target [Service] User=sajjan Group=www-data WorkingDirectory=/home/sajjan/project Environment="PATH=/home/sajjan/project/venv/bin" ExecStart=/home/sajjan/project/venv/bin/uwsgi --ini project.ini [Install] WantedBy=multi-user.target 

I realized that I need to execute the command below:

  sudo systemctl start project 

Output:

  Warning: project.service changed on disk. Run 'systemctl daemon-reload' to reload units. 

and when i started

  sudo systemcl reload project 

then print:

  Failed to reload project.service: Job type reload is not applicable for unit project.service. See system logs and 'systemctl status project.service' for details. 

and when I check "systemctl status project.service"

  โ— project.service - uWSGI instance to serve project Loaded: loaded (/etc/systemd/system/project.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Mon 2017-01-16 17:49:29 UTC; 6min ago Main PID: 27157 (code=exited, status=203/EXEC) Jan 16 17:49:29 learningwithpython systemd[1]: Started uWSGI instance to serve project. Jan 16 17:49:29 learningwithpython systemd[1]: project.service: Main process exited, code=exited, status=203/EXEC Jan 16 17:49:29 learningwithpython systemd[1]: project.service: Unit entered failed state. Jan 16 17:49:29 learningwithpython systemd[1]: project.service: Failed with result 'exit-code'. 
+5
source share
4 answers

How are you? Do you allow it? I was with your mistake. It was a flask that was broken. Read this How do I include uWSGI logging in a file? and check the log. It works well in my computer flask, but it crashes on the server. Bye!

0
source

Nginx is not allowed to write to the socket. Providing the appropriate mode with the command below helped me.

 chmod 0755 /to/project 
0
source

I saw your comment on https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04

try running sudo /etc/init.d/nginx start then try opening http: // server_domain_or_IP

If this works, enter which uwsgi to find the correct uwsgi path and change "/etc/systemd/system/myproject.service"

change

Environment="PATH=/home/sammy/myproject/myprojectenv/bin" ExecStart=/home/sammy/myproject/myprojectenv/bin/uwsgi --ini myproject.ini in the real path instead of the env path.

I will find out this problem with this command.

0
source

I had the same problem using the manual. As far as I read; 502 bad gateway is a nginx symptom that cannot connect to uwsgi correctly. Changing socket permissions solves the problem for me.

 sudo chmod 777 /home/sajjan/project/project.sock sudo systemctl restart nginx 

777 is too much time, but its a quick and dirty way to check if it really is a permission issue

0
source

Source: https://habr.com/ru/post/1262861/


All Articles