Send optional header to FastCGI backend using nginx

I am using nginx with several backcgi fastcgi (php-cgi, mod-mono-fastcgi4). Now I need to send an additional HTTP header to the fastcgi backend, basically the same as proxy_set_header when using nginx as the reverse proxy. But, to my conclusions, there is no such thing as fastcgi_set_header in nginx.

Anyone have any ideas how to do this? I do not want to use additional nginx modules, since the solution should be easy to deploy in a wide range of client systems.

+6
source share
4 answers

I took a quick look at the manual and I think that the closest thing you will find is to pass fastcgi parameters:

Request headers are sent to the FastCGI server as parameters. In applications and scripts launched from the FastCGI server, these parameters are usually available as environment variables. For example, the "User-agent" header is passed as the HTTP_USER_AGENT parameter. In addition to the HTTP request headers, you can pass arbitrary parameters using the fastcgi_param directive.

http://wiki.nginx.org/HttpFcgiModule#Parameters.2C_transferred_to_FastCGI-server .

fastcgi_param

Syntax

: fastcgi_param parameter value

http://wiki.nginx.org/HttpFcgiModule#fastcgi_param

+7
source

Now Nginx has:

fastcgi_pass_header 'Cache-Control: no-cache, must-revalidate'; 

What you can use in your location rules if you add headers that are not yet specified in your request. By default, fastcgi uses:

 fastcgi_pass_request_headers on; 

which will pass all incoming headers from the request to fastcgi.

+4
source

You can do this with the third-party ngx_headers_more module. After building nginx with the module turned on, you can do the following in your configuration:

 location / { more_set_input_headers 'Foo: bar baz'; ... } 
0
source

The URLs of the wiki nginx articles described above do not work.

nginx provides request header values ​​through variables prefixed with $ http_ , so the HTTP_USER_AGENT request header is available through $ http_user_agent .

Similarly, a request header named CHICKEN_SOUP will be available through $ http_chicken_soup .

The following example shows how to pass the HTTP Authorization header of HTTP scripts to PHP scripts running under php-fpm (PHP FastCGI Process Manager).

 location ~ \.php$ { fastcgi_pass unix:/path/to/socket; fastcgi_index index.php; fastcgi_param HTTP_AUTHORIZATION $http_authorization; ... other settings } 
0
source

Source: https://habr.com/ru/post/909444/


All Articles