在Apache的条件 [英] Conditions in Apache

查看:101
本文介绍了在Apache的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

KOHANA_ENV 环境VAR设置为开发为例。现在有一系列的规则,我想只有当变量被设置为生产申请(打开mod_deflate模块,设置Expires头默认,关闭的ETag等。 ),如:

I have KOHANA_ENV environment var set to DEVELOPMENT for example. Now there is a set of rules I'd like to apply only if that var is set to PRODUCTION (turn on mod_deflate, set expires headers defaults, turn off ETags, etc.), like:

if (KOHANA_ENV == PRODUCTION) {
    // .. do stuff
}

有没有一种方法在所有做这在Apache级或最好是有两个conf文件?

Is there a way to do this on Apache level at all or it's better to have two conf files?

推荐答案

我与伟大的模块的帮助下做的 mod_macro

I do it with the help of the great module mod_macro.

让我们假设你在的/ etc / apache2的/ envvars中(用于Debian的状分布是存放apache的环境在变量的地方):

Let's say you have in /etc/apache2/envvars (for a Debian-like distribution it's the place to store apache environnement variables):

#export KOHANA_ENV=PROD
export KOHANA_ENV=DEV

您(联合国)COMENT取决于生产或开发。

Where you [un]coment depending on production or development.

在你有你的虚拟主机,或只是它的一部分与宏定义的另一边。宏是写一些变量的通用配置部分的方式。我用它完成Virtualhosts但这里只用一个虚拟主机的一部分的例子。我们将使用环境在变量来决定使用哪个宏(关键字使用

In the other side you have your VirtualHost, or just a part of it defined with a macro. Macro is the way to write a generic configuration part with some variables. I use it for complete Virtualhosts but here's an example with just a part of a VirtualHost. We'll use the environnement variable to decide which macro to use (keyword Use):

<Virtualhost *:80>
    ServerName foobar.com
    #(...)
    Use EnvStuff_${KOHANA_ENV} /tmp
    #(...)

下面的宏需要一个参数(TMP目录路径),它不是一项义务。

Here the macro takes an argument (tmp directory path) it's not an obligation.

那么你应该只定义2个不同的宏所在的环境在变的是宏名 EnvStuff_PROD &功放的一部分; EnvStuff_DEV

Then you should only define 2 different macro where the environnement variable is part of the macro name EnvStuff_PROD & EnvStuff_DEV:

<Macro EnvStuff_PROD $tmp>
    <IfModule mod_expires.c>
        # Enable expirations.
        ExpiresActive On
        # Cache all files for 2 weeks after access (A).
        ExpiresDefault A1209600
    </IfModule>
    <IfModule mod_headers.c>
       Header set MyHeader "Hello this is PRODUCTION envirronment. It took %D microseconds for Apache to serve this request."
        # Serve gzip compressed CSS files if they exist and the client accepts gzip.
        RewriteCond %{HTTP:Accept-encoding} gzip
        RewriteCond %{REQUEST_FILENAME}\.gz -s
        RewriteRule ^(.*)\.css $1\.css\.gz [QSA]
        #(...)
    </IfModule>
    php_admin_value upload_tmp_dir $tmp/upload
    #(... other php settings for production)
</Macro>
<Macro EnvStuff_DEV $tmp>
    <IfModule mod_expires.c>
        # Enable expirations.
        ExpiresActive Off
    </IfModule>
    <IfModule mod_headers.c>
       Header set MyHeader "Hello this is DEVELOPMENT envirronment. It took %D microseconds for Apache to serve this request."
    </IfModule>
    php_admin_value upload_tmp_dir $tmp/upload
</Macro>

在这些例子中可以checkl头在响应中,你会很容易地看到,如果它为你工作。

In these examples you can checkl headers in responses and you'll see easily if it worked for you.

要小心,如果环境在变量没有设置好,你会得到一些问题,也许你可以创建一个宏 _ EnvStuff ,以及: - )

Be careful, if the environnement variable is not set well, you'll get some problems, maybe you can create a macro EnvStuff_ as well :-)

这篇关于在Apache的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆