1

I am trying to get my Django app running on a VPS and I did everything according to this tutorial, but I am getting a 502 error.

I assume that nginx is listening to port 80 (am I right?), because sudo netstat -nlp | grep 80 throws:

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      892/nginx
tcp6       0      0 :::80                   :::*                    LISTEN      892/nginx
unix  2      [ ACC ]     STREAM     LISTENING     8942     805/acpid           /var/run/acpid.socket

But when I enter sudo nginx it seems that Nginx is not listening to port 80 ...:

`nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()`

My Nginx configuration:

server {
server_name 95.85.34.87;

access_log off;

location /static/ {
    alias /opt/myenv/static/;
}

location / {
    proxy_pass http://127.0.0.1:8001;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
    add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}

Could anyone help me ?

Eimantas
  • 429
  • 1
  • 4
  • 22
  • Do you have other services running on port 80? How are you running gunicorn? – Yuval Adam Apr 18 '14 at 22:07
  • I do not think that I have any other services running on port 80, because I have pasted the whole info that `sudo netstat -nlp | grep 80` throws (I think it filters which services are using port 80). I did everything to the tutorial, so I am using Gunicorn like that: `gunicorn_django --bind 95.85.34.87:8001` as the article said. By the way, visiting http://95.85.34.87:8001/ works fine, but I think thats not the way it should work. – Eimantas Apr 18 '14 at 22:14
  • 1
    https://stackoverflow.com/questions/14972792/nginx-nginx-emerg-bind-to-80-failed-98-address-already-in-use – Yuval Adam Apr 18 '14 at 22:20
  • Thank you for the answer, but unfortunately I am getting the same error - nginx listens for port 80, but 'the addressis already in use'.. – Eimantas Apr 18 '14 at 22:54

1 Answers1

2

What I see in your netstat output is that nginx is already running on your system. On systems like Debian or Ubuntu, and probably other *nix systems, when you install nginx it is installed so that it starts when the system boots. Then when you try to run it from the command line you are running a second instance that tries to use the same port as the instance that starts at boot time. On Debian or Ubuntu systems you can stop nginx from starting by doing:

$ sudo service nginx stop
$ sudo rm /etc/nginx/sites-enabled/default

Removing the default prevents it from starting again. That default file is a symlink to /etc/nginx/sites-available/default so you can easily recreate it if needed.

Or if you want to keep the nginx that starts at boot running on its port, you could use a configuration for your nginx started from the command line that uses a different port, for instance:

server {
        listen 3333 default_server;
        listen [::]:3333 default_server ipv6only=on;

Additional note: If you put your site in /etc/nginx/sites-enabled/ then you must not start your instance of nginx from the command line. You should control nginx only through sudo service nginx [...].

Louis
  • 146,715
  • 28
  • 274
  • 320
  • Thank you very much for the detailed coment. I have `sudo -rm -rf`'ed the 'default' profile, but the 80 port is still listened by nginx... the sites-enabled directory only has my site now, but I am still getting the same error...What you are offering is that that the default port for my website would be 3333 (or something) instead of 80, right ? – Eimantas Apr 18 '14 at 22:48
  • I've edited my answer. See the last paragraph. Since you've decided to put your config in `/etc/nginx/sites-enabled/` the you must use the `service` command to start and stop your web site, and not start it with `sudo nginx` because you'll get two instances. – Louis Apr 18 '14 at 22:54
  • Thank you again and sorry for confusion, this is a lot of information for me for one day. So now Nginx started but I am getting a 502 bad gateway error. Could you please tell me how should I check Nginx and Gunicorn server logs to debug this error ? – Eimantas Apr 18 '14 at 22:59
  • When I run into errors like that, I just open the log files created in `/var/log/nginx/*`, and `/var/log/uwsgi/*`. I presume Gunicorn (which I don't use) does something similar to uwsgi. There's no trick really. I just read those logs until I see something I recognize I can fix. :) If there is nothing I see I can fix, then I hit Google. – Louis Apr 18 '14 at 23:02
  • Thank you, I will try to figure something out :) I will accept your answer as correct since you informed me about the "double nginx instance" and that appearently was true. – Eimantas Apr 18 '14 at 23:11