Nginx改端口为81仍绑定80,求与HAProxy共存的解决方案
我正在使用Nginx和HAProxy,想要让HAProxy绑定80端口,所以把Nginx的监听端口改成了81。但修改配置后启动Nginx时,它还是尝试绑定0.0.0.0:80端口,提示地址已被占用并启动失败。相关配置及错误日志如下,如何解决实现两者共存?
相关配置与日志
Nginx默认配置(路径:/etc/nginx/sites-enabled)
## # You should look at the following URL's in order to grasp a solid understanding # of Nginx configuration files in order to fully unleash the power of Nginx. # https://www.nginx.com/resources/wiki/start/ # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ # https://wiki.debian.org/Nginx/DirectoryStructure # # In most cases, administrators will remove this file from sites-enabled/ and # leave it as reference inside of sites-available where it will continue to be # updated by the nginx packaging team. # # This file will automatically load configuration files provided by other # applications, such as Drupal or Wordpress. These applications will be made # available underneath a path with that package name, such as /drupal8. # # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. ## # Default server configuration # server { listen 81; listen [::]:81; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } # pass PHP scripts to FastCGI server # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): # fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # Virtual Host configuration for example.com # # You can move that to a different file under sites-available/ and symlink that # to sites-enabled/ to enable it. # #server { # listen 80; # listen [::]:80; # # server_name example.com; # # root /var/www/example.com; # index index.html; # # location / { # try_files $uri $uri/ =404; # } #}
HAProxy配置
global log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners stats timeout 30s user haproxy group haproxy daemon # Default SSL material locations ca-base /etc/ssl/certs crt-base /etc/ssl/private # Default ciphers to use on SSL-enabled listening sockets. # For more information, see ciphers(1SSL). This list is from: # https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/ # An alternative list with additional directives can be obtained from # https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=haproxy ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS ssl-default-bind-options no-sslv3 defaults log global mode http option httplog option dontlognull timeout connect 5000 timeout client 50000 timeout server 50000 errorfile 400 /etc/haproxy/errors/400.http errorfile 403 /etc/haproxy/errors/403.http errorfile 408 /etc/haproxy/errors/408.http errorfile 500 /etc/haproxy/errors/500.http errorfile 502 /etc/haproxy/errors/502.http errorfile 503 /etc/haproxy/errors/503.http errorfile 504 /etc/haproxy/errors/504.http frontend myfrontend bind 127.0.0.1:80 rate-limit sessions 50 http-response set-header Cache-Control no-cache,\ max-age=600 if { capture.req.uri -m beg /api/ } capture request header Host len 32 acl shipping-report.local hdr(host) -i shipping-report.local www.shipping-report.local/report use_backend report_shipping if shipping-report.local backend report_shipping server report01 shipping-report.local:8000
错误日志
nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Wed 2022-08-31 16:04:03 +07; 1min 0s ago Docs: man:nginx(8) Process: 30507 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=1/FAILURE) Process: 26443 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS) Process: 31737 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE) Process: 31721 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 30483 (code=exited, status=0/SUCCESS) Thg 8 31 16:04:01 falcon-anhnkd systemd[1]: Starting A high performance web server and a reverse proxy server... Thg 8 31 16:04:01 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Thg 8 31 16:04:01 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Thg 8 31 16:04:02 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Thg 8 31 16:04:02 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Thg 8 31 16:04:03 falcon-anhnkd nginx[31737]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) Thg 8 31 16:04:03 falcon-anhnkd nginx[31737]: nginx: [emerg] still could not bind() Thg 8 31 16:04:03 falcon-anhnkd systemd[1]: nginx.service: Control process exited, code=exited status=1 Thg 8 31 16:04:03 falcon-anhnkd systemd[1]: nginx.service: Failed with result 'exit-code'. Thg 8 31 16:04:03 falcon-anhnkd systemd[1]: Failed to start A high performance web server and a reverse proxy server.
解决方案
问题出在你只修改了sites-enabled下的默认配置,但Nginx的其他配置文件里还有监听80端口的规则,按以下步骤排查修改:
检查Nginx主配置文件
打开/etc/nginx/nginx.conf,搜索listen 80,有些默认配置会在http块下直接定义server块监听80端口,找到后把端口改成81,或者直接注释掉该server块。检查所有启用的站点配置
执行命令ls -l /etc/nginx/sites-enabled/,确认是否有除了默认配置之外的其他软链文件,逐一打开这些文件,检查是否存在listen 80的配置,有则修改为81。检查其他包含的配置文件
查看nginx.conf里的include指令,比如是否有include /etc/nginx/conf.d/*.conf;这类配置,进入对应目录(比如conf.d),检查所有.conf文件里是否有监听80端口的规则,修改为81。验证配置并重启服务
修改完成后,执行nginx -t验证配置语法是否正确,输出test is successful后,执行systemctl restart nginx重启Nginx服务。优化HAProxy绑定范围(可选)
你的HAProxy目前只绑定127.0.0.1:80,只能接收本地请求。如果需要对外提供服务,把frontend里的bind 127.0.0.1:80改成bind 0.0.0.0:80,这样外部请求才能到达HAProxy。
内容的提问来源于stack exchange,提问作者Mộc Uyển Thanh

