Eimantas
Eimantas

Reputation: 429

Nginx error with port 80

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 ?

Upvotes: 1

Views: 4255

Answers (1)

Louis
Louis

Reputation: 151401

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 [...].

Upvotes: 2

Related Questions