nginx错误:(99:不能分配请求的地址)

我运行Ubuntu Hardy 8.04和nginx 0.7.65,当我尝试启动我的nginx服务器时:

$ sudo /etc/init.d/nginx start 

我得到以下错误:

 Starting nginx: [emerg]: bind() to IP failed (99: Cannot assign requested address) 

其中“IP”是我的IP地址的占位符。 有谁知道为什么这个错误可能会发生? 这是在EC2上运行。

我的nginx.conf文件如下所示:

 user www-data www-data; worker_processes 4; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; access_log /usr/local/nginx/logs/access.log; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 3; gzip on; gzip_comp_level 2; gzip_proxied any; gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; include /usr/local/nginx/sites-enabled/*; } 

和我的/usr/local/nginx/sites-enabled/example.com看起来像:

 server { listen IP:80; server_name example.com; rewrite ^/(.*) https://example.com/$1 permanent; } server { listen IP:443 default ssl; ssl on; ssl_certificate /etc/ssl/certs/myssl.crt; ssl_certificate_key /etc/ssl/private/myssl.key; ssl_protocols SSLv3 TLSv1; ssl_ciphers ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:-LOW:-SSLv2:-EXP; server_name example.com; access_log /home/example/example.com/log/access.log; error_log /home/example/example.com/log/error.log; } 

使用Amazon EC2和弹性IP,服务器并不像大多数其他服务器那样知道其IP。

所以你需要告诉你的linux允许进程绑定到非本地地址。 只需/etc/sysctl.conf下行添加到/etc/sysctl.conf文件中:

 # allow processes to bind to the non-local address # (necessary for apache/nginx in Amazon EC2) net.ipv4.ip_nonlocal_bind = 1 

然后通过以下方式重新加载你的sysctl.conf:

$ sysctl -p /etc/sysctl.conf

这将在重新启动后罚款。

要避免在配置中对IP地址进行硬编码,请执行以下操作:

 listen *:80 

正如上面提到的kirpit,你会希望允许linux进程绑定到本地IP地址:

 nano /etc/sysctl.conf # allow processes to bind to the non-local address net.ipv4.ip_nonlocal_bind = 1 sysctl -p /etc/sysctl.conf 

然后你想添加与你的弹性IP相关的私有IP地址 ,并将其添加到你的网站配置:

 nano /etc/nginx/sites-available/example.com 

重新加载nginx:

 service nginx reload 

全做完了!

对于Amazon EC2和弹性IP, sysctl.conf将不起作用,因为nginx仍然不在eth0侦听。

所以,你需要listen *;

可能有剩余的进程/程序正在80端口使用/侦听。

你可以使用netstat -lp来检查。 杀死该进程并启动nginx。

使用Amazon EC2和弹性IP,服务器并不像大多数其他服务器那样知道其IP。 所以在Apache虚拟主机文件中,至少你把*:80而不是你的弹性IP:80

然后它正常工作。 所以从理论上讲,为nginx做*:80应该是一样的,但是当你得到[emerg]:bind()到0.0.0.0:80失败(98:地址已经在使用中)。 尚未找到解决方案。 。

对于将来可能要处理这个问题的人来说,我只是在AWS实例中查找了我的私有IP,并且绑定了这个IP。 我证实了nginx能够公开地听取并在那之后执行我的重写。 我不能做*:端口,因为我有一个内部的服务器,我正在代理。

如果您使用网络管理器,则必须等待启动服务之前提升网络接口:

systemctl启用NetworkManager-wait-online.service