Nginx - 502 bad gateway

I'm working through How To Serve Django Applications with uWSGI and Nginx on Ubuntu 16.04.

I have completed the tut and restarted both nginx and uwsgi. I notice that if I go to the IP of my server, I see my site (making me think that uwsgi is working properly). However if I go to www.mysite.com, I get the 502 error. My nginx config file in the /etc/nginx/sites-available is mysite contains:

server {
listen 80;
server_name mysite.com www.mysite.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /home/deploy/mysite;
}

location / {
    include         uwsgi_params;
    uwsgi_pass      unix:/run/uwsgi/mysite.sock;
}

What am I doing wrong?

$ systemctl status uwsgi
Failed to connect to bus: No such file or directory

I'm confused, I know I have been working on a number of uwsgi files and

$ sudo systemctl start uwsgi 

does not cause an error. Also the website is still being served at the IP address. I thought uwsgi was doing that.

$ netstat -a|grep uwsgi
unix  2      [ ACC ]     STREAM     LISTENING     2373238691 /run/uwsgi/mysite.sock

Solution 1:

The 502 Bad Gateway error means server is not getting proper response from another server. In your case it is uWSGI server. It is either not running or stopped due to error.

Check the status of uWSGI server by using sudo systemctl status uwsgi.

Solution 2:

In php7 www-conf is in: /etc/php/7.0/fpm/pool.d

In this file I find this:

; Note: This value is mandatory.
listen = /run/php/php7.0-fpm.sock

; Set listen(2) backlog.

I set a parameter in /etc/nginx/sites-available/[site]

upstream php-handler {
    #server 127.0.0.1:9000;
    server unix:/run/php/php7.0-fpm.sock;
}

And it's working :)