首页 文章

nginx错误:(99:无法分配请求的地址)

提问于
浏览
21

我正在运行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;

        }

8 回答

  • 8

    使用Amazon EC2和弹性IP,服务器实际上并不知道其IP与大多数其他服务器一样 .

    所以你需要告诉你的linux允许进程绑定到非本地地址 . 只需将以下行添加到 /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

    在重新启动时会很好 .

  • -1

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

    listen *:80
    
  • 0

    正如上面提到的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关联的 private ip address 并将其添加到您的站点配置:

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

    重新加载nginx:

    service nginx reload
    

    全部完成!

  • 1

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

    您可以使用netstat -lp检查它 . 杀掉那个进程并启动nginx .

  • 0

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

    然后它正常工作 . 因此从理论上讲,对于nginx执行*:80应该可以正常工作,但是当你这样做时,[emerg]:bind()到0.0.0.0:80失败(98:地址已经在使用中) . 尚未找到解决方案 . .

  • 0

    对于将来可能正在处理此问题的人,我只是在AWS实例中查找我的私有IP并将其绑定到该实例 . 我确认nginx能够公开收听并在此之后执行重写 . 我不能做*:PORT,因为我有一个我代理的内部服务器 .

  • 0

    如果您使用的是网络管理器,则必须等到升级网络接口才能启动该服务:

    systemctl启用NetworkManager-wait-online.service

  • 49

    对于Amazon EC2和弹性IP, sysctl.conf 将无法正常运行,因为nginx仍然无法监听 eth0 .

    所以,你需要 listen *;

相关问题