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

Pub*_*nka 5 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;
#}
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

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

如果我卷曲这个网址,响应如下。

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

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

我们将非常感谢您的帮助。

Pub*_*nka 2

我已经找到了解决方案。为了实现http2,需要在nginx中启用SSL。我尝试过,效果很好。我从这个链接找到了答案。

如何在 Ubuntu 16.04 上设置支持 HTTP/2 的 Nginx

现在我的配置文件如下。

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;
#}
}
Run Code Online (Sandbox Code Playgroud)