nginx中server配置的共同部分抽取linux反向代理php

博客分类: nginx 阅读次数: comments

nginx中server配置的共同部分抽取linux反向代理php

如下所示:

server {
    listen       80;
    server_name  dev.drpb.com;
    root /Users/Stone/repo/oschina/drsoft/page-builder;
    location ~ \.php$ {
        fastcgi_index  index.php;
        fastcgi_pass  127.0.0.1:9000;
        include fastcgi.conf;
    }
}
server {
    listen       80;
    server_name  dev.drp.com;
    root /Users/Stone/repo/oschina/drsoft/site;
    location ~ \.php$ {
        fastcgi_index  index.php;
        fastcgi_pass  127.0.0.1:9000;
        include fastcgi.conf;
    }
}

nginx配置中有两个server配置块, 它们有共同的php反向代理配置部分.

请问如何将:

location ~ \.php$ {
    fastcgi_index  index.php;
    fastcgi_pass  127.0.0.1:9000;
    include fastcgi.conf;
}

抽取出来放到一个地方,而不是在每个server中都写一次(我试着放到其共同父级的http块中是不可以的),谢谢!

已找到答案:

将location部分抽取到一个外部文件,如命名为:common_rules.conf,放到nginx目录下。

修改server配置为:

server {
    server_name  dev.drpb.com;
    root /Users/Stone/repo/oschina/drsoft/page-builder;
    include common_rules.conf;
}
server {
    server_name  dev.drp.com;
    root /Users/Stone/repo/oschina/drsoft/site;
    include common_rules.conf;
}