localhost发送了无效的响应。 ERR_INVALID_HTTP_RESPONSE

时间:2018-06-04 04:45:25

标签: nginx http2

我正在努力在nginx docker中启用http2。当我调用localhost时,我收到此错误。我的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

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

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

如果我卷曲这个网址,请回复如下。

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

我知道该页面在http2中作为二进制文件传输。我希望得到nginx索引页面作为响应。

您的帮助将非常感激。

2 个答案:

答案 0 :(得分:1)

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

  

通过http://网址   如果CURLOPT_HTTP_VERSION设置为CURL_HTTP_VERSION_2_0,则libcurl将在对主机的初始请求中包含升级头,以允许升级到HTTP / 2。可能我们稍后会引入一个选项,如果无法升级,将导致libcurl失败。可能我们引入了一个选项,使libcurl一次性通过http://

使用HTTP / 2

Nginx在同一明文通道上不支持HTTP / 2和HTTP / 1(请参阅this enhancement request),因此它不适用于curl。

您可以尝试使用nghttp2h2c代替curl,但不确定是否有效但不确定是否有效。实际情况是HTTP / 2最好通过HTTPS协商,许多工具(包括所有浏览器)仅使用HTTPS / because of this上的HTTP / 2。

答案 1 :(得分:0)

我找到了解决方法。需要在nginx中启用SSL才能实现http2。我试过了,它运作良好。我从这个链接找到了答案。

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;
#}
}