首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >重用nginx.conf中域的配置语句

重用nginx.conf中域的配置语句
EN

Stack Overflow用户
提问于 2013-03-29 22:08:51
回答 1查看 7.5K关注 0票数 12

假设我为一个域设置了一个nginx配置,如下所示:

代码语言:javascript
复制
server {

  root /path/to/one;
  server_name one.example.org;

  location ~ \.php$ {
    try_files       $uri =404;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include         fastcgi_params;
  }

}

现在,如果我想添加另一个具有不同内容的域,有没有方法可以重用前一个域中的等价语句,或者我必须为我想要支持的每个新域复制所有内容?

代码语言:javascript
复制
server {

  root /path/to/two; # different
  server_name two.example.org; # different

  location ~ \.php$ {
    try_files       $uri =404;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include         fastcgi_params;
  }

}

我尝试将location指令移到server闭包之外,但很明显事情并不是这样的,因为在重启nginx时,我得到了一个错误"location directive is not allowed here“。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-30 03:16:22

您可以执行以下操作:

代码语言:javascript
复制
 server_name one.example.org two.example.org;

如果两者除了域名之外完全相同

如果您只是有类似的位置块,您可以将这些位置移动到单独的文件中,然后执行

代码语言:javascript
复制
include /etc/nginx/your-filename; 

为了在每个服务器块中轻松地使用它

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15704919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档