Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does nginx fastcgi_pass support variables?

Tags:

php

nginx

fastcgi

I would like to use dynamic host resolution with nginx and fastcgi_pass.

When fastcgi_pass $wphost:9000; is set in the conf then nginx displays the error [error] 7#7: *1 wordpress.docker could not be resolved (3: Host not found),

but when I set fastcgi_pass wordpress.docker:9000;it is working except for the fact the that after a wordpress restart nginx still points to an old ip.

server {
  listen [::]:80;
  include /etc/nginx/ssl/ssl.conf;

  server_name app.domain.*;

  root /var/www/html;
  index index.php index.html index.htm;

  resolver 172.17.42.1 valid=60s; 
  resolver_timeout 3s;

  location / {
    try_files $uri $uri/ /index.php?q=$uri&$args; ## First attempt to serve request as file, then as directory, then fall back to index.html
  }

  #error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

  set $wphost wordpress.docker;  

  # pass the PHP scripts to FastCGI server listening on wordpress.docker
  location ~ \.php$ {
    client_max_body_size    25M;
    try_files               $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass            $wphost:9000;
    fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param           SCRIPT_NAME $fastcgi_script_name;
    fastcgi_index           index.php;
    include                 fastcgi_params;
  }

  location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
    add_header        Pragma public;
    add_header        Cache-Control "public, must-revalidate, proxy-revalidate";
    access_log        off;
    log_not_found     off;
    expires           168h;
  }

  # deny access to . files, for security
  location ~ /\. {
        access_log    off;
        log_not_found off; 
        deny          all;
  }

}

I have different virtual host configuration where I use proxy_pass http://$hostname; and in this setup everything is working as expected and the host is found.

After trying different options I wonder if fastcgi_pass does support variables

like image 930
Vad1mo Avatar asked May 26 '15 16:05

Vad1mo


1 Answers

It does work if you pass the upstream as a variable instead, for example:

upstream fastcgi_backend1 {
   server php_node1:9000;
}

upstream fastcgi_backend2 {
   server php_node2:9000;
}

server {
    listen 80;
    server_name _;

    set $FASTCGI_BACKEND1 fastcgi_backend1;
    set $FASTCGI_BACKEND2 fastcgi_backend2;
    
    location ~ ^/site1/index.php$ {
        fastcgi_pass   $FASTCGI_BACKEND1;
    }

    location ~ ^/site2/index.php$ {
        fastcgi_pass   $FASTCGI_BACKEND2;
    }
}
like image 105
Jared Chu Avatar answered Nov 16 '22 13:11

Jared Chu