Nginx failed to bind and redirection - nginx

I'm trying to configure my reverse proxy through nginx using this tutorial, but when I go to restart the nginx server, it throws the following error message:
nginx[12681]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
I checked out the port using netstat, and I have the following result
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN off (0.00/0/0)
What does this mean? When I go to my domain name, it redirects to the Centos page instead of an nginx page I should be getting. How can I configure the file to get it to redirect to nginx?
Edit:
Added httpd status
httpd.service disabled
mysql.service enabled
my_api.service enabled
nginx.service disabled
I get the above errors when I try to start nginx using systemctl

Running netstat -luntp as root showed that nginx had spawned a master process and a worker. Killing those processes solved the issue

Related

Nginx fails with port 80, but only when I am connected to the virtual server by SSH

I first opened a Linode.com droplet.
Then installed Nginx, and left the default configuration.
I pointed my domain to the droplet's IP address.
The command "systemctl status nginx" - shows an error, this one seems relevant:
"Jun 18 14:40:13 localhost nginx[25837]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)"
I searched and racked my brains out until I tried this:
change config file so that Nginx/website listens to port 8080
Working fine, I then changed settings back to port 80 (stopped working)
The process was not halted, so step 2 allowed me to run the command "Nginx -s reload" (the reload signal needs the process running, and I couldn't restart Nginx with systemctl due to the original error)
While the website was not loading, I exited the SSH connection (which I checked, was using port 22, not 80)
My website then started working!
Re-entered the droplet with SSH, and found Nginx listening on port 80 just fine.
I want to understand why this worked, as it makes no sense that port 80 should be unavailable to Nginx just because I have an SSH connection.
I don't think it's unique to Linode's configuration as I have a Digital Ocean droplet too, and this was failing to listen on port 80 as well, although I never tried the fix I discovered on Linode.

MAMP Pro nginx won't start

Last week I was able to use MAMP Pro on MacOS Big Sur 11.6 (Mackbook Pro M1) to run the nginx web server for multiple virtual hosts. Today, after using certbot locally in renewing my local LetsEncrypt SSL certificate that supports local dev https development, I cannot start nginx on any of the virtual hosts.
I get a warning in MAMP Pro that says "You configured nginx to use ports 80 and 443, but at least one of these seems to be in use." So I checked that MAMP is still configured to use port 80 for nginx. Then I used Activity Monitor to try to kill all httpd processes using port 80. Two of them, owned by root and _www, refused to die. So I used kill -9 with their respective PIDs. That killed them for a moment but they came back. After that, I tried to restart nginx again on port 80. It fails to start with message "Nginx couldn't be started. Please check the log file for more information."
The log file from that shows
2021/12/28 16:04:22 [emerg] 93920#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2021/12/28 16:04:22 [emerg] 93920#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2021/12/28 16:04:22 [emerg] 93920#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2021/12/28 16:04:22 [emerg] 93920#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2021/12/28 16:04:22 [emerg] 93920#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2021/12/28 16:04:22 [emerg] 93920#0: still could not bind()
How do I fully kill the root and _www user processes? More importantly, why is this happening? Was it something to do with the renewed LetsEncrypt SSL?
Support told me that this is a problem with MAMP Pro 5 on MacOS and that MAMP Pro 5 is no longer supported. Upgrading to MAMP 6+ solves the problem.

I can't restart my nginx

In console it showing like this:
Job for nginx.service failed because the control process exited with
error code. See "systemctl status nginx.service" and "journalctl -xe"
for details.
I tried following commands:
sudo nginx -t -c /etc/nginx/nginx.conf
and
sudo nginx -t
It is showing success message but nginx server is not restarting.
sudo systemctl status nginx.service
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 Wed 2018-07-18 18:06:51 IST; 11min ago
Process: 28271 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Process: 28259 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 5236 (code=exited, status=0/SUCCESS)
nginx error log have this errors
bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/07/18 18:06:49 [emerg] 28271#28271: bind() to [::]:443 failed (98: Address already in use)
2018/07/18 18:06:49 [emerg] 28271#28271: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/07/18 18:06:49 [emerg] 28271#28271: still could not bind()
Try to run the following two commands:
sudo fuser -k 80/tcp
sudo fuser -k 443/tcp
Then execute
sudo service nginx restart
If that worked, your hosting provider might be installing Apache on your server by default during a fresh install, so keep reading for a more permenant fix. If that didn't work, keep reading to identify the issue.
Run nginx -t and if it doesn't return anything, I would verify Nginx error log. By default, it should be located in /var/log/nginx/error.log.
You can open it with any text editor:
sudo nano /var/log/nginx/error.log
Can you find something suspicious there?
The second log you can check is the following
sudo nano /var/log/syslog
When I had this issue, it was because my hosting provider was automatically installing Apache during a clean install. It was blocking port 80.
When I executed sudo nano /var/log/nginx/error.log I got the following as the error log:
2018/08/04 06:17:33 [emerg] 634#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/08/04 06:17:33 [emerg] 634#0: bind() to [::]:80 failed (98: Address already in use)
2018/08/04 06:17:33 [emerg] 634#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
What the above error is telling is that it was not able to bind nginx to port 80 because it was already in use.
To fix this, you need to run the following:
yum install net-tools
sudo netstat -tulpn
When you execute the above you will get something like the following:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1762/httpd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1224/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1528/sendmail:acce
tcp6 0 0 :::22 :::* LISTEN 1224/sshd
You can see that port 80 is blocked by httpd (Apache). This could also be port 443 if you are using SSL.
Get the PID of the process that uses port 80 or 443. And send the kill command changing the <PID> value:
sudo kill -2 <PID>
Note in my example the PID value of Apache was 1762 so I would execute sudo kill -2 1762
Aternatively you can execute the following:
sudo fuser -k 80/tcp
sudo fuser -k 443/tcp
Now that port 80 or 443 is clear, you can start Nginx by running the following:
sudo service nginx restart
It is also advisable to remove whatever was previously blocking port 80 & 443. This will avoid any conflict in the future. Since Apache (httpd) was blocking my ports I removed it by running the following:
yum remove httpd httpd-devel httpd-manual httpd-tools mod_auth_kerb mod_auth_mysql mod_auth_pgsql mod_authz_ldap mod_dav_svn mod_dnssd mod_nss mod_perl mod_revocator mod_ssl mod_wsgi
Hope this helps.
You already have a program listening on the ports. Stop the other program or choose other ports for nginx.
If it's another nginx instance, you can search for it with ps aux | grep nginx and then kill it with kill PID

Nginx- error: bind() to 0.0.0.0:80 failed. permission denied

I am trying to run Nginx, but I am getting the error below:
bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a
socket in a way forbidden by its access permissions)
Please provide some help on what changes I need to do to make it working?
I have tried running on ports other than 80 and it works. but I need it to be running on 80.
Note: I am running on Windows 7 with command prompt running as Administrator.
If the port is already in use, you can change the default port of 80 to a different port that is not in use (maybe 8070). In conf\nginx.conf:
server {
listen 8070;
...
}
After startup, you should be able to hit localhost:8070.
tl;dr
netsh http add iplisten ipaddress=::
Faced similar issue. Run the above command in command prompt.
This should free up port 80, and you'd be able to run nginx.
Description:
netsh http commands are used to query and configure HTTP.sys settings and parameters.
add iplisten :
Adds a new IP address to the IP listen list, excluding the port number.
"::" means any IPv6 address.
For more netsh http commands refer the netsh http commands documentation.
Hope this helps!!
You have to be admin or root to bind port 80. Something you can do if you cannot run as root, is that your application listens to other port, like 8080, and then you redirect messages directed to 80 to 8080. If you are using Linux you redirect messages with iptables.
nginx: [emerg] bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
I got a similar problem, My 80 port was listening to IIS (windows machine). Stopping IIS freed up 80 port.
The problem got resolved...!!
Please check if another Proxy is running under port 80 ---> in my case IIS was running as a reverse proxy, so nginx could not start..
Stopping IIS, and starting of NGXIN solved the problem
My Tomcat server was running on port 80. Changed the port number in conf\nginx.conf file and it started to work.
This is an old question but since I had this problem recently I thought of posting another possible reason in this problem.
If the user is using Docker and has already tried all proposed solutions as stated above and is wondering why port 80 is trying to bind although on your configurations you are overwriting the port to non root port e.g. listen 8080; it seems that the newer NGINX images have a default nginx.conf file in /etc/nginx/conf.d.
Sample:
$ grep -r 80 /etc/nginx/
/etc/nginx/conf.d/default.conf: listen 80;
On my case I removed it on my Dockerfile:
RUN set -x \
&& rm -f /etc/nginx/nginx.conf \
&& rm -f /etc/nginx/conf.d/default.conf
Next step pass from my custom configurations:
COPY ["conf/nginx.conf", "/etc/nginx/nginx.conf"]

cannot change the default port on nginx

I am using the nginx web server bundled with gitlab and I have SSL setup and I am using the non-default port i.e. I have the URL setup as
external_url 'https://www.myserver.com:2443'
However, when I connect with this address, I get a connection was reset error. Running, gitlab-cal tail shows
bind() to 0.0.0.0:80 failed (98: Address already in use)
I am not sure why it still is looking at port 80. Is there some other setting that I must set here.

Resources