Apache常用配置项

时间:2021-06-26 13:49:48

301是永久重定向

302是临时重定向

编辑虚拟主机的配置文件需要用到apache的rewrite模块

所以要用要先检查apache是否安装了rewrite模块

# ./bin/apachectl -M | grep rewrite

 rewrite_module (shared)

# vim /usr/local/apache/conf/extra/httpd-vhosts.conf

<IfModule mod_rewrite.c>

     RewriteEngine on

     RewriteCond %{HTTP_HOST} ^www.aaa.com$ [OR]

     RewriteCond %{HTTP_HOST} ^www.bbb.com$

     RewriteRule ^/(.*)$ http://www.fansik.com/$1 [R=301,L]

</IfModule>

Apache日志切割

CustomLog "|/usr/local/apache/bin/rotatelogs -l /usr/local/apache/logs/discuz-access_%Y%m%d_log 86400" combined

Apache不记录以下结尾的内容

    SetEnvIf Request_URI ".*\.gif$" image-request

    SetEnvIf Request_URI ".*\.jpg$" image-request

    SetEnvIf Request_URI ".*\.png$" image-request

    SetEnvIf Request_URI ".*\.bmp$" image-request

    SetEnvIf Request_URI ".*\.swf$" image-request

    SetEnvIf Request_URI ".*\.js$" image-request

    SetEnvIf Request_URI ".*\.css$" image-request

CustomLog "|/usr/local/apache/bin/rotatelogs -l /usr/local/apache/logs/discuz-access_%Y%m%d_log 86400" combined env=!image-request

Apache配置静态缓存

<IfModule mod_expires.c>
  ExpiresActive on
  ExpiresByType image/gif "accessplus 1 days"
  ExpiresByType image/jpeg "accessplus 24 hours"
  ExpiresByType image/png "accessplus 24 hours"
  ExpiresByType text/css "now plus 2hours"
  ExpiresByType application/x-javascript"now plus 2 hours"
  ExpiresByTypeapplication/x-shockwave-flash "now plus 2 hours"
  ExpiresDefault "now plus 0min"
</IfModule>

加载好上面的配置后可用curl来检查一下哦

# curl-x127.0.0.1:80 'http://120.55.194.217/static/image/common/logo.png' -I

如果出现红字,说明配置成功了,缓存时间是86400秒

HTTP/1.1 200 OK

Date: Tue, 15Mar 2016 06:10:29 GMT

Server:Apache/2.4.6 (Unix) PHP/5.6.6

Last-Modified:Tue, 08 Dec 2015 02:39:16 GMT

ETag:"1149-52659e1375d00"

Accept-Ranges:bytes

Content-Length:4425

Cache-Control:max-age=86400

Expires: Wed, 16Mar 2016 06:10:29 GMT

Content-Type:image/png

如果没效果记得查看一下模块的开关是不是打开了

Apache防盗链

SetEnvIfNoCaseReferer "^http://.*\.discuz\.com" local_ref
SetEnvIfNoCase Referer ".*\.luntan\.com" local_ref
< filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png|js|css)">
    Order Allow,Deny
    Allow from env=local_ref
< /filesmatch>

Apache访问控制(只允许本机访问admin的管理页面admin)

<filesmatch"(.*)admin(.*)">

        Order deny,allow

        Deny from all

        Allow from 127.0.0.1

<filesmatch>

Apache禁止解析某个目录下的php文件

<Directory/opt/discuz>

        php_admin_flag engine off

        <filesmatch "(.*)php">

                Order deny,allow

                Deny from all

        </filesmatch>

</Directory>

 Apache禁止指定user_agent

<IfModulemod_rewrite.c>

        RewriteCond %{HTTP_USER_AGENT}^.*curl.* [NC,OR]

        RewriteCond %{HTTP_USER_AGENT}^.*chrome* [NC]

        RewriteRule .* - [F]

 </IfModule>

Apache限制某个目录

<IfModulemod_rewrite.c>

    RewriteCond %{REQUEST_URI} ^.*/tmp/.* [NC]

    RewriteRule .* - [F]

</IfModule>