多"服务器"块的相同"位置"规则

elh*_*tis 6 nginx

我必须配置多个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;
        }
}
Run Code Online (Sandbox Code Playgroud)

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

非常感谢你的帮助.

埃里克

Ale*_*gin 10

使用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;
}
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)