0

My aim is to have configurable upstream, so I can use whatever php version I need per project / virtual host.

I tried:

upstream php {
    server php7-fpm-alpine:9000;
}
server {
    listen       80;
    server_name  somesite.com;
    root         /www/somesite.com;

    include /etc/nginx/nginx-wp-common.conf;
}

nginx-wp-common.conf has fastcgi_pass php;

My setup works for 1 site, but once I start adding more virtual hosts for other domains nginx complains:

duplicate upstream "php"

As you can see my aim is modularity in choosing upstream and DRY principles.

2
  • Do you want fastcgi_pass php; to point to different upstream in each server? Commented May 4, 2016 at 11:10
  • @AlexeyTen yes, that is my intention. Commented May 4, 2016 at 11:34

1 Answer 1

2

if the upstream name (php) should be the same for each version of PHP, then you have to move the upstream block(s) into external files and include the one you need.

exmaple:

move

upstream php {
    server php7-fpm-alpine:9000;
}

to a file /etc/nginx/upstream-php7.conf

and include that file in your /etc/nginx/nginx-wp-common.conf

optionally create different upstreams with different names (like upstream php7 {...}) and use the desired one in the fastcgi_pass

EDITED:

another option:

define different upstream blocks:

upstream php5 {
    server php5-fpm-alpine:9000;
}
upstream php7 {
    server php7-fpm-alpine:9000;
}

modify your server block(s), set different values for $upstream for different vhosts

server {
    listen       80;
    server_name  somesite.com;
    root         /www/somesite.com;
    set $upstream php7;

    include /etc/nginx/nginx-wp-common.conf;
}
server {
    listen       80;
    server_name  othersite.com;
    root         /www/othersite.com;
    set $upstream php5;

    include /etc/nginx/nginx-wp-common.conf;
}

modify nginx-wp-common.conf

fastcgi_pass $upstream;
Sign up to request clarification or add additional context in comments.

4 Comments

I'm trying to have different upstream per virtual host. None of your proposed solutions help this.
I have edited my answer. I think this should now solve your problem.
I remember I had to rename the file with the upstreams to something like 00-my-upstreams.conf to ensure that they are always loaded first using the clasical /etc/nginx/sites-enabled configuration
@PerroVerd loading order should not matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.