nginx not listening to port 80

2019-01-31 02:22发布

I've just installed a Ubuntu 12.04 server and nginx 1.2.7, removed default from sites-enabled and added my own file into sites-available and symlink at sites-enabled. Then restarted nginx.

Problem: However going to the URL does not load the site. netstat -nlp | grep nginx and netstat -nlp | grep 80 both returns no results! lsof -i :80 also returns nothing. A dig from another server returns the correct ip address so it shouldn't be a DNS problem. I was able to connect to apache which I have now stopped its service. nginx logs also show nothing.

How should I troubleshoot this problem?

/etc/nginx/site-available/mysite.com

server {
    listen   80;
    server_name www.mysite.com mysite.com *.mysite.com;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    root /var/www/mysite/public;

    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args ;
    }
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_read_timeout 300;
    }

}

7条回答
劳资没心,怎么记你
2楼-- · 2019-01-31 02:54

You are probably binding nginx to port 80 twice. Is that your full config file? Don't you have another statement listening to port 80?

查看更多
Evening l夕情丶
3楼-- · 2019-01-31 02:59

I had this same problem, the solution was that I had not symlinked my siteconf file correctly. Try running vim /etc/nginx/sites-enabled/mysite.com—can you get to it? I was getting "Permission Denied."

If not run:

rm /etc/nginx/sites-enabled/mysite.com
ln -s /etc/nginx/sites-available/mysite.com /etc/nginx/sites-enabled/mysite.com
查看更多
欢心
4楼-- · 2019-01-31 03:10

I ran into the same problem, I got a Failed to load resource: net::ERR_CONNECTION_REFUSED error when connecting over HTTP, but fine over HTTPS. Ran netstat -tulpn and saw nginx not binding to port 80 for IPv4. Done everything described here. Turned out to be something very stupid:

Make sure the sites-available file with the default_server is actually enabled.

Hope this saved some other poor idiot out there some time.

查看更多
时光不老,我们不散
5楼-- · 2019-01-31 03:11

I've found it helpful to approach debugging nginx with the following steps:

1... Make sure nginx is running.

ps aux | grep nginx

2... Check for processes already bound to the port in question.

lsof -n -i:80

3... Make sure nginx has been reloaded.

sudo nginx -t
sudo nginx -s reload

On Mac, brew services restart nginx is not sufficient to reload nginx.

4... Try creating simple responses manually to make sure your location path isn't messed up. This is especially helpful when problems arise while using proxy_pass to forward requests to other running apps.

location / {
    add_header Content-Type text/html;
    return 200 'Here I am!';
}
查看更多
贼婆χ
6楼-- · 2019-01-31 03:14

Have you checked if your nginx binary really exists? please check if

#whereis nginx

outputs the binary path and check this path with your init script from /etc/init.d/nginx. e.g.

DAEMON=/usr/sbin/nginx

(In my init script "test -x $DAEMON || exit 0" is invoked and in any case this script returned nothing - my binary was completely missing)

查看更多
男人必须洒脱
7楼-- · 2019-01-31 03:16

If your logs are silent on the issue, you may not be including the sites-enabled directory. One simple way to tell that the site is being loaded is to set the error/access log path within your server block to a unique path, reload nginx, and check if the files are created.

Ensure the following include directive exists within the http context in /etc/nginx/nginx.conf.

http {
  ...
  include /etc/nginx/sites-enabled/*;
}
查看更多
登录 后发表回答