21

I use nginx with several fastcgi backends (php-cgi, mod-mono-fastcgi4). Now I need to sent an additional http header to the fastcgi backend, basically the same as proxy_set_header does when using nginx as reverse proxy. But to my findings, there is no such thing as fastcgi_set_header in nginx.

Somebody got any ideas how to do this anyways? I dont want to use additional nginx modules as the solution muste be easily deployable on a wide range of customer systems.

Dynalon
  • 6,577
  • 10
  • 54
  • 84

4 Answers4

17

I took a quick look at the manual and I think the closest you will find is passing fastcgi parameters:

The request headers are transferred to the FastCGI-server in the form of parameters. In the applications and the scripts run from the FastCGI-server, these parameters are usually accessible in the form of environment variables. For example, the header "User-agent" is transferred as parameter HTTP_USER_AGENT. Besides the headers of the HTTP request, it is possible to transfer arbitrary parameters with the aid of directive fastcgi_param.

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

Community
  • 1
  • 1
Kamu
  • 186
  • 1
  • 4
  • 1
    I've found that, too. However, fastcgi_param are used to set variable that are send to the FastCGI server - not HTTP Headers. The problem is you can't access the fastcgi variables from within the application (which is a php script or a asp.net webapp). – Dynalon Feb 27 '12 at 12:20
  • 12
    I got it working now using 'fastcgi_param HTTP_X_MYVAR "myvalue";'. If the variable name starts with HTTP_ it seems it gets copied over to the headers. However, this is undocumented and implementation specific and might change with future releases – Dynalon Feb 27 '12 at 12:33
  • Good find, Dyna. I'll make note of that. – Kamu Feb 27 '12 at 12:43
  • 1
    Hi both, check out $_SERVER in PHP to see fastcgi_params. http://stackoverflow.com/questions/8098927/nginx-variables-similar-to-setenv-in-apache – That Realty Programmer Guy Mar 22 '14 at 07:52
  • 1
    @Dynalon is incorrect. FastCGI parameters are accessible in the `$_SERVER` superglobal in PHP. – NeoVance Oct 12 '19 at 04:01
  • Given that my posting is from 2012, the situation today in PHP might be different then back then. – Dynalon Oct 12 '19 at 09:05
11

The URLs to the nginx wiki articles above are broken.

nginx exposes request header values via variables prefixed with $http_, so a request header of HTTP_USER_AGENT is available via $http_user_agent.

Likewise a request header named CHICKEN_SOUP would be available via $http_chicken_soup.

The example below shows how to pass the the Authorization HTTP request header 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
}
zoot
  • 304
  • 2
  • 7
  • 3
    This is the best answer of this topic. This configuration works well. Other answers does not work. but there is some hint. any underlines are usually removed. then for example variable CHICKEN_SOUP is presented both as CHICKENSOUP and HTTP_CHICKENSOUP in environment if you add config 'fastcgi_param CHICKENSOUP $http_chickensoup . Unfortunately $http_chicken_soup is inaccessible, I've spend some time for debug this one. – Znik Feb 14 '19 at 11:11
  • The example makes no sense whatsoever. NGINX will automatically pass *all* client request headers over to PHP-FPM. – Danila Vershinin Nov 23 '19 at 16:12
-1

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

location / {
    more_set_input_headers 'Foo: bar baz';
    ...
}
mschuetz
  • 23
  • 4
  • 1
    it is completly unnecessary. resolve wroten by @zoot is enough with standard modules. then you don't need any recompilation. – Znik Feb 14 '19 at 10:53
-1

Nginx now has:

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

Which can be used in your location rules if you are adding headers which aren't already specified in your request. By default fastcgi uses:

fastcgi_pass_request_headers on;

Which will pass all incoming Headers from the request to fastcgi.

Derek Dowling
  • 479
  • 1
  • 4
  • 15
  • 6
    @Dyna No, it should not. @Derek Dowling : Your first solution is wrong, `fastcgi_pass_header` does the opposite of what you explain : it permits passing header(s) from the fastcgi server reply back to the client, not from the initial request to the fastcgi server. The intention of this directive is to keep in the reply special headers that can be interpreted by reverse proxies (like `X-Accel-Buffering`, `X-Accel-Limit-Rate` etc) in some specific cases. – Xavier Lucas Mar 10 '15 at 15:00
  • 2
    It isn't corrent according to doc : Permits passing otherwise disabled header fields from a FastCGI server to a client. – Saman Mohamadi Jan 01 '16 at 13:02