Ubuntu

nginx - 綁定()到 0.0.0.0:80 失敗(98:地址已在使用中)

  • November 2, 2017

我有一個反向代理,設置了 5 個伺服器配置,但是從一開始它就一直抱怨無法監聽我指定的埠,因此伺服器無法啟動。

我嘗試了幾件事:

  • 將配置拆分為內部的多個文件sites-available並創建符號連結sites-enabled
  • 刪除listen屬性並允許它回退到預設值
  • 將埠更改為ipv6only=on- 這會導致錯誤
  • 將埠更改為[::]:80
  • 更改埠以便沒有衝突,即 , 80,8182

使用netstat我可以看到我需要的埠上沒有執行任何東西,當我執行時,nginx -t我得到一個成功的輸出,說語法正常並且 nginx.conf 測試成功。

這是我的nginx輸出:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:81 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:82 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:83 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:84 failed (98: Address already in use)

有誰知道這是為什麼造成的?提前致謝

編輯:lsof -Pni | grep LISTEN輸出:

sshd      1288     root    3u  IPv4  15712      0t0  TCP *:22 (LISTEN)
sshd      1288     root    4u  IPv6  15714      0t0  TCP *:22 (LISTEN)
nginx    14025     root    6u  IPv6 852470      0t0  TCP *:80 (LISTEN)
nginx    14025     root    7u  IPv6 852471      0t0  TCP *:443 (LISTEN)
nginx    14026 www-data    6u  IPv6 852470      0t0  TCP *:80 (LISTEN)
nginx    14026 www-data    7u  IPv6 852471      0t0  TCP *:443 (LISTEN)
dnsmasq  23894  dnsmasq    5u  IPv4 650875      0t0  TCP *:53 (LISTEN)
dnsmasq  23894  dnsmasq    7u  IPv6 650877      0t0  TCP *:53 (LISTEN)

這是我的伺服器配置:

server {
       listen 80;
       server_name hac-staging-proxy.redant.cloud;

       location / {
               proxy_pass http://195.219.8.212/;
               proxy_set_header Host www.uat2prd.halfordsautocentres.com;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_buffering off;
       }
}

server {
       listen 81;
       server_name halfords-c1-staging-proxy.redant.cloud;

       location / {
               proxy_pass http://195.219.8.206/;
               proxy_set_header Host www.c1.uat2prd.halfordsautocentres.com;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_buffering off;
       }

}

server {
       listen 82;
       server_name halfords-c2-staging-proxy.redant.cloud;

       location / {
               proxy_pass http://195.219.8.206/;
               proxy_set_header Host www.c2.uat2prd.halfordsautocentres.com;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_buffering off;
       }

}

server {
       listen 83;
       server_name halfords-staging-proxy.redant.cloud;

       proxy_redirect http://www.uat2prd.halfords.com http://halfords-staging-proxy.redant.cloud;
       proxy_redirect https://www.uat2prd.halfords.com https://halfords-staging-proxy.redant.cloud;

       location / {
               add_header Set-Cookie Experiment=FH;
               resolver 127.0.0.1;
               proxy_pass http://www.uat2prd.halfords.com;
               # sub_filter_types *;
               sub_filter 'www.uat2prd.halfords.com' 'halfords-staging-proxy.redant.cloud';
               sub_filter 'www.c1.uat2prd.halfords.com' 'halfords-c1-staging-proxy.redant.cloud';
               sub_filter 'www.c2.uat2prd.halfords.com' 'halfords-c2-staging-proxy.redant.cloud';
               sub_filter_once off;
               proxy_set_header Set-Cookie Experiment=FH;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_buffering off;
       }
}

server {
       listen 84;
       server_name halfords-staging-proxy.redant.cloud2;

       proxy_redirect https://www.uat2prd.halfords.com https://halfords-staging-proxy.redant.cloud;
       proxy_redirect http://www.uat2prd.halfords.com http://halfords-staging-proxy.redant.cloud;

       ssl_certificate           /etc/nginx/domain.cert;
       ssl_certificate_key       /etc/nginx/domain.key;
       ssl_dhparam               /etc/ssl/certs/dhparam.pem;

       ssl on;
       ssl_session_cache  builtin:1000  shared:SSL:10m;
       ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
       ssl_ciphers "INTENTIONALLY REMOVED"
       ssl_prefer_server_ciphers on;

       location / {
               add_header Set-Cookie Experiment=FH;
               resolver 127.0.0.1;
               proxy_pass https://www.uat2prd.halfords.com;
               # proxy_ssl_session_reuse off;
               # sub_filter_types *;
               sub_filter 'www.uat2prd.halfords.com' 'halfords-staging-proxy.redant.cloud';
               sub_filter 'www.c1.uat2prd.halfords.com' 'halfords-c1-staging-proxy.redant.cloud';
               sub_filter 'www.c2.uat2prd.halfords.com' 'halfords-c2-staging-proxy.redant.cloud';
               sub_filter_once off;
               proxy_set_header Set-Cookie Experiment=FH;
               proxy_set_header Host www.uat2prd.halfords.com;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_buffering off;
       }
}

不要在’listen’的同時’listen *:80'

$$ :: $$:80’(對於埠 443 也是如此)。 原因是在雙棧 IPv4/IPv6 主機上,如果您在 IPv6 TCP 埠 X 上偵聽,而在 IPv4 TCP 埠 X 上沒有任何東西在偵聽,您將得到兩者。如果您看到連接以 ::ffff:1.2.3.4 的形式出現(其中 1.2.3.4 是 IPv4 地址),那麼這就是正在發生的事情。這稱為 IPv4 映射的 IPv6 地址。

這一點,當 nginx 然後去偵聽 IPv4 埠 80 (0.0.0.0:80) 時,它會遇到這樣一個事實,即它也有效地這樣做了(通過

$$ :: $$:80). 圍繞這種行為有很多實現細節,但除非您精通網路程式,否則我不會在這裡提及它們。

希望有幫助,卡梅倫

引用自:https://serverfault.com/questions/881481