1
votes

I cannot run Nginx because port 80 is already listening on a docker-proxy service.

tcp6     0     0 :::80          :::*           LISTEN      13110/docker-proxy

I would like to run Nginx on port 8800 instead of the default port 80.

As such, I've edited the default file as below;

sudo nano /etc/nginx/sites-available/default

listen 8800 default_server;
listen [::]:8800 default_server;
listen localhost;

However, I still cannot get it working as expected even after restart.

What am I doing wrong and how can I resolve it?

Below is the error I get;

● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2019-04-26 04:23:14 EDT; 13min ago
Docs: man:nginx(8)
Process: 16955 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)

Process: 16944 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 80941 (code=exited, status=0/SUCCESS)
Apr 26 04:23:11 ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 26 04:23:11 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use) Apr 26 04:23:12 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:12 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:13 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:13 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:14 ubuntu nginx[16955]: nginx: [emerg] still could not bind()
Apr 26 04:23:14 ubuntu systemd[1]: nginx.service: Control process exited, code=exited status=1
Apr 26 04:23:14 ubuntu systemd[1]: nginx.service: Failed with result 'exit-code'.
Apr 26 04:23:14 ubuntu systemd[1]: Failed to start A high performance web server and a reverse proxy server.

3
How you restarted nginx and what's in the error log?kworr
I have restarted the service. I have added the error log.Ganidu Ranasinghe

3 Answers

1
votes

It looks like the problem is in the line

        listen localhost;

The "listen" directive can take a port number, a host, or both. I believe in your case this line defaults the port to 80, which would cause the problems you are experiencing.

Also, beware if "default" is not the only file in hosts_available. Each of these (each server block) will default to using port 80 unless overridden there. Just editing "default" would not be enough in that case.

0
votes

You have to go to the /etc/nginx/sites-enabled/default

Edit that file and put (If you are willing to put "8800" as your port for nginx)

server { listen 8800; }

The start the server

sudo service nginx start

or

sudo service nginx restart if previously running.

Then access localhost:8800

Add a rule to iptables

 vi /etc/sysconfig/iptables 
 -A INPUT -m state --state NEW -m tcp -p tcp --dport 8800 -j ACCEPT

Restart IPtables

sudo service iptables restart;
0
votes

I found this answer on Server Fault helpful: https://serverfault.com/questions/655067/is-it-possible-to-make-nginx-listen-to-different-ports

You can add ports to the one server block to keep things simple:

server {
    listen 80;
    listen 8000;
    server_name example.org;
    root /var/www/;
}