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;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏