我已经设置了一个具有 5 个服务器配置的反向代理,但是从一开始它就一直在抱怨无法侦听我指定的端口,因此服务器无法启动。
我尝试了一些方法:
- 将配置拆分成多个文件
sites-available
并创建符号链接sites-enabled
- 删除该
listen
属性并允许其恢复为默认值 - 将端口更改为
ipv6only=on
- 这会导致错误 - 将端口更改为
[::]:80
- 更改端口以避免冲突,即,,,,
80
等等。81
82
使用时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;
}
}
答案1
不要同时使用“listen *:80”和“listen [::]: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)。
围绕此行为有很多实现细节,但除非您精通网络编程,否则我不会在这里提及它们。
希望这对你有帮助,卡梅伦