Nginx

nginx 無法正確響應 HTTP/1.1

  • November 8, 2020

我遇到了一個問題,即某些首先嘗試通過 HTTP/1.1 連接的客戶端未正確轉發到 HTTPS

curl -v http://indentationerror.com/
*   Trying 217.45.175.173:80...
* Connected to indentationerror.com (217.45.175.173) port 80 (#0)
> GET / HTTP/1.1
> Host: indentationerror.com
> User-Agent: curl/7.73.0
> Accept: */*
> 
* Received HTTP/0.9 when not allowed

* Closing connection 0
curl: (1) Received HTTP/0.9 when not allowed

但是,使用命令:

curl -v http://indentationerror.com/ --http2-prior-knowledge

nginx 按預期通過 HTTP/2 返回 301 重定向

我的配置目前是:

server {
       server_name www.indentationerror.com;
       listen 443 ssl http2;
       listen [::]:443 ssl http2;
       ssl    on;
       root "/http/indentationerror.com/www";
       include /etc/nginx/genericHandlers/indentationerror.com;
}

server {
       server_name indentationerror.com;
       listen 443 ssl http2 default_server;
       listen [::]:443 ssl http2 default_server;
       ssl    on;
       root "/http/indentationerror.com/www";
       include /etc/nginx/genericHandlers/indentationerror.com;
}

server {
       listen 80 default_server;
       listen [::]:80 default_server;
       server_name $host;
       return 301 https://$host$request_uri;   
}

損壞的部分(在我發現問題後添加):

server {
   server_name api.indentationerror.com;
   listen 80 http2;
   listen [::]:80 http2;
   root "/http/indentationerror.com/api";

   include /etc/nginx/genericHandlers/indentationerror.com;
}

server {
   server_name api.indentationerror.com;
   listen 443 ssl http2;
   listen [::]:443 ssl http2;
   ssl    on;
   root "/http/indentationerror.com/api";
   include /etc/nginx/genericHandlers/indentationerror.com;

}

我的一個埠 80 伺服器指令(您可以在“api”伺服器中看到它)意外地有一個“http2”指令。出於某種原因,在一個伺服器塊上使用它會影響其他伺服器塊。感謝邁克爾的提示。對於遇到此問題的其他人,從所有埠 80 伺服器中刪除“http2”可以解決此問題。

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