多个 "Server" 块的相同 "Location" 规则

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

我必须为每个网站配置一个专用证书的多 https 网站。这样效果很好。

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;
        }
}

现在,我想分解 "location" 块,因为它总是相同的。可能吗 ? (我也试过只在服务器块上,但不可能在 ssl 属性中放置一个变量)

非常感谢您的帮助。

埃里克

对此类因式分解使用 include 指令:

include

在 nginx 配置文件夹中创建文件,例如 /etc/nginx/conf.d/location_php.cnf(不是 .conf 以避免被 nginx 自动加载)

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

然后将其包含到服务器块中:

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;
}