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?
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?
2
Answers
Each process has its own set of environment variables.
When a new process gets created it inherits the environment variables from its parent process.
The
env
command gets started by your terminal process, it inherits the terminal’s environment variables and that’s what it shows you.As you know, your terminal does not get started by spawn-fcgi. Although nobody knows which came first, the chicken or the egg, it’s fairly certain that your terminal process does not get started by spawn-fcgi. And that’s why your terminal does not show the environment variables that spawn-fcgi sets and are inherited by the processes that spawn-fcgi, itself, starts.
The
fastcgi
library directly modifies theenviron
variable of the program, simulating a new environment after accepting a request. See: https://github.com/FastCGI-Archives/fcgi2/blob/874b148dfeadf8ebadff89cf228e63f836e509a3/doc/FCGI_Accept.3And https://pubs.opengroup.org/onlinepubs/7908799/xsh/environ.html