Search code examples
linuxnginxload-balancingfastcgiphp

Nginx Load Balancer having two load balanced nginx+php-fpm (Primary script unknown) error


We have two web servers with nginx+php-fpm ( 10.0.0.10 and 10.0.0.20 ), which is load balanced behind another nginx server ( just nginx ), when we try to browse we get file not found error, with error logs listed at the bottom.

Load Balancer (10.0.0.1)

nginx.conf

upstream test_rack {
    server 10.0.0.10:80;
    server 10.0.0.20:80;
}

server {
    location / {
       proxy_pass http://test_rack;
     }
}}

Upstream Server (10.0.0.20)

subdomains.conf

server {
listen       80;
server_name ~^(?<sub>.+)\.example\.com$;

root /data/vhost/$sub.example.com/htdocs;

location / { 
    try_files $uri /index.php;
 }


location ~ \.php$ {
    fastcgi_pass   unix:/var/run/php5-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    include        fastcgi_params;
}}     

Error on webserver (10.0.0.10 and 10.0.0.20)

*1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 10.0.0.1, server: ~^(?<sub>.+)\.example\.com$, request: "GET / HTTP/1.0", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "test_rack"

Solutions tried :

fastcgi_param  SCRIPT_FILENAME   /data/vhost/$sub.example.com/htdocs/$fastcgi_script_name;

Solution

  • Add proxy_set_header Host $host; to first nginx.

    Otherwise your upstreams get test_rack instead of original hostname and $sub variable is empty.