PHP: Get HTTP Protocol Version (HTTP/1.1 vs HTTP/2)

和自甴很熟 提交于 2019-12-24 01:26:00

问题


Until now my php application assumed HTTP 1.1 everywhere. So I defined all headers like so:

header("HTTP/1.1 500 Internal Server Error"); 

But now my server also supports HTTP 2 and I want to update all header responses with the right HTTP status code.

How to I get the HTTP Protocol version of the http request?

(My webserver is nginx, but I guess it is irrelevant if I am using nginx or apache.)


回答1:


The server protocol should be available through SERVER_PROTOCOL from the server environment, usually exposed through $_SERVER['SERVER_PROTOCOL'] inside your application.

From phpinfo() under Apache 2.4:

SERVER_PROTOCOL => HTTP/1.1



回答2:


changing /etc/nginx/factcgi_params:

#fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  SERVER_PROTOCOL    HTTP/2.0;

Header should be:-

header($_SERVER['SERVER_PROTOCOL'].'404 Not Found');


来源:https://stackoverflow.com/questions/34718412/php-get-http-protocol-version-http-1-1-vs-http-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!