【问题标题】:Putting if else in nginx Vhost configuration with Puppet Nginx jfryman module使用 Puppet Nginx jfryman 模块将 if else 放入 nginx Vhost 配置中
【发布时间】:2016-03-02 08:03:06
【问题描述】:

我想为有一些 if 条件语句的网站自动化 nginx vhost 配置。我试图用jfryman-nginx puppet forge 模块来做到这一点,但我无法配置文件。我想要的配置如下:

 server {
  listen   8080;

  server_name abcqwe.com;

  root /data/vvv/abcqwe.com;
  index index.php index.html index.htm;

  access_log /data/vvv/abcqwe.com/logs/access.log;
  error_log  /data/vvv/abcqwe.com/logs/error.log;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to index.html
    #try_files $uri $uri/ /index.html;

    if (!-f $request_filename){
      set $rule_0 1$rule_0;
    }

    try_files $uri $uri/ /index.html;
  }

那么我如何将这个 if 条件放入我的虚拟主机配置中:

 if (!-f $request_filename){
       set $rule_4 1$rule_4;
     }

有可能吗?我试过location_cfg_prepend,但这没有帮助..

【问题讨论】:

  • 我不清楚您在问什么,但请尝试阅读“如果是邪恶”的文章。
  • 我有同样的问题,仍在寻找解决方案。问题似乎在于 location_cfg_prepend 添加一个 ;在生成的配置文件中关闭 if } 之后。 nginx 似乎不喜欢那样。 location_cfg_append 做同样的事情。
  • @zuniga 我发布了一个答案。 github 上的最新源中有一个修复,但它不在 puppet forge 中。
  • 谢谢@GuyHughes,我会尝试最新版本,但这对我来说在生产环境中还不够好,必须等到修复程序发布
  • @zuniga,github master 分支应该是生产质量的。简单的选择是使用 puppet forge 中的 v0.0.9 模块,并编辑 .erb 模板以在适当的地方删除分号。您可以使用 a patch 在您的生产机器上应用更改。

标签: if-statement nginx rewrite puppet conditional-statements


【解决方案1】:

jfryman-nginx v0.0.9 也有同样的问题。

In the latest source templates on github,如果您使用location_custom_cfg_{pre,ap}pend,似乎不应该添加;

但是in v0.0.9, the latest bundled puppet forge release,即使该行只有} 导致nginx 失败,模板也会在之后添加;

解决方案:

  1. 使用location_**custom**_cfg_{pre,ap}pend 选项

  2. A 或 B:

    A.编辑模板中的;

    B.从github拉取最新版本的源码:

sudo rm -rf /etc/puppet/modules/nginx && cd /etc/puppet/modules && sudo git clone https://github.com/jfryman/puppet-nginx nginx

示例

  nginx::resource::location { 'sub.dom.com_root':
    ensure                                                                       => present,
    vhost                                                                        => 'sub.dom.com',
    location                                                                     => '~ \.php$',
    fastcgi                                                                      => 'unix:/var/run/php5-fpm.sock',                                                                                                                                                              
    www_root                                                                     => '/var/www/sub.dom.com',
    location_custom_cfg_append                                                   => {
      'if (-f $request_filename)'                                                => '{ break; }',
      'if (-d $request_filename)'                                              => '{ break; }',
      'if ($request_filename !~ (js|css|jpg|png|gif|robots\.txt|index\.php.*) )' => '{ rewrite ^/(.*) /index.php?$1 last; }',
    },
  }

【讨论】:

  • 我通过添加 # 作为最后一个字符并强制额外的来解决它;成为评论:'if (-f $request_filename)' => '{ break; }#'
【解决方案2】:

请尝试在 nginx 中使用LuaModuleLua 有大量配置的模块。

Lua语法中使用非常简单。

请阅读this

【讨论】:

    【解决方案3】:

    您现在可以为此使用raw_prependraw_append 参数。

    请参阅location.pp 页面了解更多详情。

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 2014-02-24
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      相关资源
      最近更新 更多