Nginx v1.25.1配置http2出现协议重定义警告的解决求助
解决Nginx v1.25.1的"protocol options redefined"警告
问题原因
你遇到的protocol options redefined警告,本质是多个server块监听同一个443端口,且都重复配置了http2 on;。在Nginx中,HTTP/2属于端口级别的协议配置,无需在每个监听该端口的server块中重复声明,重复设置会触发冲突警告。
解决方案
有两种可靠的修复方式:
方式1:全局配置HTTP/2(推荐)
将http2 on;移到http块的全局配置中,这样所有监听SSL端口的server都会自动启用HTTP/2,无需在每个server里单独设置。
修改后的完整配置示例:
http { http2 on; # 全局启用HTTP/2,所有SSL端口自动生效 # 保留你原有的全局配置(如include mime.types;等) # Redirect to https server { listen 80; server_name domain.name www.domain.name; location ^~ /.well-known/acme-challenge/ { root /acme-challenge; allow all; } location / { return 301 https://www.domain.name/$request_uri; } } # Redirect from HTTPS non-www to HTTPS www server { listen 443 ssl; listen [::]:443 ssl; server_name domain.name; ssl_certificate /ssl/live/domain.name/fullchain.pem; ssl_certificate_key /ssl/live/domain.name/privkey.pem; location ^~ /.well-known/acme-challenge/ { root /acme-challenge; allow all; } location / { return 301 https://www.domain.name/$request_uri; } } # Redirect to the specific path server { listen 443 ssl; listen [::]:443 ssl; server_name www.domain.name; ssl_certificate /ssl/live/domain.name/fullchain.pem; ssl_certificate_key /ssl/live/domain.name/privkey.pem; location ^~ /.well-known/acme-challenge/ { root /acme-challenge; allow all; } location / { return 301 https://www.other.domain.name/app/; } } }
方式2:仅在一个监听443的server块中设置HTTP/2
如果不想全局启用HTTP/2,可以只在其中一个监听443的server块中保留http2 on;,其他同端口的server块去掉该配置:
# Redirect from HTTPS non-www to HTTPS www server { listen 443 ssl; listen [::]:443 ssl; http2 on; # 仅在这里设置一次 server_name domain.name; # 其他配置保持不变 } # Redirect to the specific path server { listen 443 ssl; listen [::]:443 ssl; # 移除http2 on; server_name www.domain.name; # 其他配置保持不变 }
验证修复
修改配置后,先执行配置合法性检查,再重启Nginx:
nginx -t systemctl restart nginx
执行完成后所有警告都会被消除。
内容的提问来源于stack exchange,提问作者Corentin MERCIER
相关产品推荐
相关产品推荐

