Nginx: Override host header when using fastcgi_pass

纵然是瞬间 提交于 2019-12-05 18:34:44

问题


I'm trying to override the http host header that is passed to my php-based application (specifically Phabricator) when using fastcgi_pass.
I've found a lot of examples for doing this when using proxy_pass, but I can't seem to find an example of how to do this with fastcgi_pass. Specifically, I'd like the proxied php application to see the host header as "phabricator.localhost".

(The reason for this is that I want to associate several different domains with the Phabricator webapp, but it only allows one domain to be associated and it rejects any requests not made that that one domain.)

I'm pretty new to configuring Nginx with FastCGI, so I'm not sure how fastcgi works. Any help is appreciated.

Here is my Nginx server configuration:

  server {
    server_name phabricator.localhost  www.example.com example.com;
    root /opt/phabricator/phabricator/webroot;

    location / {
      index index.php;
      rewrite ^/(.*)$ /index.php?__path__=/$1 last;
    }

    location = /favicon.ico {
      try_files $uri =204;
    }

    location /index.php {
    fastcgi_pass   localhost:9000;
    fastcgi_index   index.php;

    #### HERE ARE MY ATTEMPTS #####
    #proxy_set_header HOST phabricator.localhost;
    #fastcgi_param SERVER_NAME phabricator.localhost;
    #fastcgi_pass_header 'Host: phabricator.localhost';
    #fastcgi_pass_header 'Host: phabricator.localhost';
    #add_header Host phabricator.localhost;
    #proxy_set_header Host phabricator.localhost;
    #### END ATTEMPTS ####

    fastcgi_param  REDIRECT_STATUS    200;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;
    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
    fastcgi_param  REMOTE_ADDR        $remote_addr;
  }
}

回答1:


Have you tried HTTP_HOST? The following works for me:

fastcgi_param HTTP_HOST phabricator.localhost;


来源:https://stackoverflow.com/questions/28889673/nginx-override-host-header-when-using-fastcgi-pass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!