current position:Home>Nginx gets the header and sets$_ SERVER

Nginx gets the header and sets$_ SERVER

2021-08-25 08:53:58 Yuan Yueping

Nginx obtain header Custom variable

nginx The agent defaults to header Of the parameters in "_" Underline out , Therefore, the background server can't get the tape "_" Parameter name of the line .:

underscores_in_headers on; # This property defaults to off, Said if header name Include underscores in , Ignore .
  
  • 1.

Customize header by X-Real-IP, Through the second nginx Get the header It needs to be like this :

  • $http_x_real_ip; ( All in lowercase , And there's another one in front http_)

Nginx towards $_SERVER Add variables to

location ~ \.php$ {
try_files      $uri =404;
root           /www/wwwroot/app;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
fastcgi_param  MYENV 'DEV';  
include        fastcgi_params;
}
     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

fastcgi_param  MYENV 'DEV';   # Right here php Of $_SERVER A variable has been added to

adopt php obtain $_SERVER Get variable

"MYENV" => "DEV"

Nginx obtain header Add custom values to $_SERVER

Customize header The value of is UserName=yyp, need nginx Get the value and add it to $_SERVER in

nginx Acquired key All need to be replaced with lowercase , meanwhile key You need to add http_

location ~ \.php$ {
underscores_in_headers on;
try_files      $uri =404;
root           /www/wwwroot/app;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
fastcgi_param  UserNmae $http_username;  
include        fastcgi_params;
}
     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

Case study

nginx To configure

    location ~ \.php?.*$ { 
        fastcgi_pass   unix:/tmp/php-cgi-56.sock;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
        include        fastcgi_params; 
        fastcgi_param  TOKEN $http_username;  
        fastcgi_param  TOKEN88 $http_userpassword;  
        
    } 

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

Configuration page

cat b.php   adopt b Page to get the settings header Value 
<html>
<head>
    <script language="php"> echo "php is php."; </script>
 
</head>
<body>
<?php echo "<table border='1'>"; foreach ($_SERVER as $key=>$value){ echo "<tr>"; echo "<td>$key</td>"; echo "<td>$value</td>"; echo "</tr>"; } echo "</table>"; ?>
</body>
</html>

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

adopt curl Command to change header

curl 'http://192.168.2.84/b.php' -H 'username: yyp2' -H 'userpassword: 123' > a.html
     
  • 1.

Results show
watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=

 

copyright notice
author[Yuan Yueping],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210825085355030b.html

Random recommended