I have a docker-container running Nginx and i need to get the Version of Nginx in one of my php files. I tried using docker exec nginx_container nginx -v and it's works only in terminal, it doesn't return the $output.
Does someone know a properway to solve this?
CodePudding user response:
If your nginx serves static files, you can make a request for one of those and then look at the Server
header in the response.
To illustrate, run nginx and make a request
docker run --rm -d -p 8080:80 nginx
curl -v http://localhost:8080/
The response will include the Nginx version in the Server
header. On my machine, it's Server: nginx/1.23.0
.
CodePudding user response:
Here is a small example (it's been a while since I've used PHP, so there is probably a better way of doing this):
$request = curl_init();
curl_setopt( $request , CURLOPT_URL , "http://localhost/" );
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $request , CURLOPT_HEADER , true );
curl_setopt( $request , CURLOPT_NOBODY , true );
$response = curl_exec( $request );
$headers = explode("\r\n", $response);
$server = implode("", preg_grep ('/Server/', $headers));
echo $server;
Result:
Server: nginx
In my case there is no version information because we have turned off the version information using:
bash-5.0# cat/etc/nginx/nginx.conf | grep server_tokens
server_tokens off;
If you are running PHP in the same docker container you can just ignore the curl and run the command through php using shell_exec
:
php > echo shell_exec('nginx -v');
nginx version: nginx/1.18.0