本地主机发送了无效响应。 ERR_INVALID_HTTP_RESPONSE

localhost sent an invalid response. ERR_INVALID_HTTP_RESPONSE

我正在努力在 nginx docker 中启用 http2。当我调用本地主机时出现此错误。我的 nginx 配置文件如下。

server {
listen       2020 http2;
server_name  localhost;

#charset koi8-r;
#access_log  /var/log/nginx/host.access.log  main;

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}

Http2监听端口是2020,我在容器启动的时候已经暴露到2020了。

docker run -d --name nginx -p 80:80 -p 2020:2020 -v 
/home/pubudu/work/nginx/nginx-tms/config/conf.d:/etc/nginx/conf.d/:ro  
nginx:latest

这是教程link: How To Set Up Nginx with HTTP/2 Support on Ubuntu 16.04

我需要使用端口 443 吗?是强制性的吗? (我没有为此使用 ssl)

如果我 curl 这个 url,响应如下。

警告:二进制输出可能会弄乱您的终端。使用“--output -”来告诉 警告:curl 无论如何都要将它输出到您的终端,或者考虑“--output 警告:“保存到文件。

我知道该页面在 http2 中以二进制形式传输。我想获取 nginx 索引页作为响应。

非常感谢您的帮助。

Curl 不直接支持 HTTP/2 over HTTP (h2c),坚持使用详细的升级方法 in this post:

Over an http:// URL If CURLOPT_HTTP_VERSION is set to CURL_HTTP_VERSION_2_0, libcurl will include an upgrade header in the initial request to the host to allow upgrading to HTTP/2. Possibly we can later introduce an option that will cause libcurl to fail if not possible to upgrade. Possibly we introduce an option that makes libcurl use HTTP/2 at once over http://

Nginx 不支持 HTTP/2 和 HTTP/1 通过相同的明文通道(参见 this enhancement request),因此它不适用于 curl。

您可以尝试使用 nghttp2 or h2c instead of curl but not sure if that works though not sure if they do. The reality is HTTP/2 is best negotiated over HTTPS and many tools (including all the browsers) only use HTTP/2 over HTTPS because of this

我已经找到了解决办法。需要在nginx中启用SSL才能实现http2。我试过了,效果很好。我从这个link中找到了答案。

How To Set Up Nginx with HTTP/2 Support on Ubuntu 16.04

现在我的配置文件如下。

server {
listen       443 ssl http2;
server_name  localhost;

ssl_certificate /etc/nginx/ssl/nginx-selfsigned.crt;
ssl_certificate_key /etc/nginx/ssl/nginx-selfsigned.key;


#charset koi8-r;
#access_log  /var/log/nginx/host.access.log  main;

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}