搜索

首页  >  问答  >  正文

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

过去多啦不再A梦过去多啦不再A梦2796 天前527

全部回复(1)我来回复

  • 阿神

    阿神2017-05-16 17:18:37

    已找到答案:

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

    回复
    0
  • 取消回复