Apache經常使用配置項

301是永久重定向javascript

302是臨時重定向php

編輯虛擬主機的配置文件須要用到apache的rewrite模塊css

因此要用要先檢查apache是否安裝了rewrite模塊java

# ./bin/apachectl -M | grep rewritechrome

 rewrite_module (shared)apache

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

<IfModule mod_rewrite.c>緩存

     RewriteEngine onapp

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

     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 "access plus 1 days"
  ExpiresByType image/jpeg "access plus 24 hours"
  ExpiresByType image/png "access plus 24 hours"
  ExpiresByType text/css "now plus 2 hours"
  ExpiresByType application/x-javascript "now plus 2 hours"
  ExpiresByType application/x-shockwave-flash "now plus 2 hours"
  ExpiresDefault "now plus 0 min"
</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, 15 Mar 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, 16 Mar 2016 06:10:29 GMT

Content-Type: image/png

若是沒效果記得查看一下模塊的開關是否是打開了

Apache防盜鏈

SetEnvIfNoCase Referer "^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

<IfModule mod_rewrite.c>

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

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

        RewriteRule .* - [F]

 </IfModule>

Apache限制某個目錄

<IfModule mod_rewrite.c>

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

    RewriteRule .* - [F]

</IfModule>

相關文章
相關標籤/搜索