elhostis
elhostis

Reputation: 1087

A same "Location" rule for multi "Server" block

I have to configure multi https website with a dedicated certificate for each website. It works fine like that.

server {
        listen   443;
        server_name client1.localhost.eu;

        ssl on;
        ssl_certificate ...;
        ssl_certificate_key ...;

        root   /var/www/client1;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm-client1.sock;
            fastcgi_index index.php;
            include fastcgi_params;
        }
}

server {
        listen   443;
        server_name client2.localhost.eu;

        ssl on;
        ssl_certificate ...;
        ssl_certificate_key ...;

        root   /var/www/client2;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm-client2.sock;
            fastcgi_index index.php;
            include fastcgi_params;
        }
}

Now, I would like to factorize the "location" block, because it is always the same. Is it possible ? (I have also tried to have only on server block, but it's not possible to put a variable in the ssl attribute)

Thanks a lot for your help.

Eric

Upvotes: 8

Views: 1613

Answers (1)

Aleksey Deryagin
Aleksey Deryagin

Reputation: 2675

Use include directive for such factorization:

include

Create file in the nginx config folder like /etc/nginx/conf.d/location_php.cnf (not .conf to avoid auto-loading by nginx)

location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm-client2.sock;
            fastcgi_index index.php;
            include fastcgi_params;
}

and then include it into server blocks:

server {
        listen   443;
        server_name client1.localhost.eu;

        ssl on;
        ssl_certificate ...;
        ssl_certificate_key ...;

        root   /var/www/client1;
        include /etc/nginx/conf.d/location_php.cnf;
        # OR use relative path to nginx config root:
        # include conf.d/location_php.cnf;
}

Upvotes: 12

Related Questions