phpc++nginxfastcgi

How does getenv() get the parameters of the http request parsed by nginx?


When writing CGI program for spawn-fcgi, use getenv() to get environment variables, that is, the parameters set by fastcgi_param command in fastcgi.conf. but when I use the env command output on the terminal in Linux, I can't find the corresponding environment variable. Why?

fastcgi_param

Sets a parameter that should be passed to the FastCGI server. The value can contain text, variables, and their combination. These directives are inherited from the previous configuration level if and only if there are no fastcgi_param directives defined on the current level.

Since it says that these parameters are passed by nginx to the FastCGI server, how does getenv() get these parameters? I learned that getenv() is used to obtain environment variables. So did spawn-fcgi create these temporary environment variables according to fastcgi.conf when ngnix passed dynamic requests?


Solution

  • The fastcgi library directly modifies the environ variable of the program, simulating a new environment after accepting a request. See: https://github.com/FastCGI-Archives/fcgi2/blob/874b148dfeadf8ebadff89cf228e63f836e509a3/doc/FCGI_Accept.3

    After accepting a new request, FCGI_Accept assigns new values to the global variables stdin, stdout, stderr, and environ. After FCGI_Accept returns, these variables have the same interpretation as on entry to a CGI program.

    And https://pubs.opengroup.org/onlinepubs/7908799/xsh/environ.html