nginx中server配置的共同部分抽取

如下所示:

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块中是不可以的),谢谢!

阅读 4k
1 个回答

已找到答案:

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

  2. 修改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;
}
推荐问题
宣传栏