trsgw 发表于 2015-5-7 10:23:07

apache的常用配置

域名跳转
# vi/usr/local/apache2/conf/extra/httpd-vhosts.conf
*************************************
<IfModule mod_rewrite.c>
         RewriteEngine on
         RewriteCond %{HTTP_HOST} ^www.aaa.com$ //注意空格
         RewriteCond %{HTTP_HOST} ^www.bbb.com$
         RewriteRule ^/(.*)$ http://www.123.com/$1
   </IfModule>
或者:    <IfModule mod_rewrite.c>
               RewriteEngine on
               RewriteCond %{HTTP_HOST} !^www.123.com$//注意空格
               RewriteRule ^/(.*)$ http://www.123.com/$1
      </IfModule>
************************
# curl -x127.0.0.1:80 www.123.com -I
HTTP/1.1 301 Moved Permanently
Date: Tue, 05 May 2015 18:40:12 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
X-Powered-By: PHP/5.3.28
location: forum.php
Cache-Control: max-age=0
Expires: Tue, 05 May 2015 18:40:12 GMT
Content-Type: text/html

# curl -x127.0.0.1:80 www.aaa.com -I
HTTP/1.1 301 Moved Permanently
Date: Tue, 05 May 2015 18:40:21 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Location: http://www.123.com/
Cache-Control: max-age=0
Expires: Tue, 05 May 2015 18:40:21 GMT
Content-Type: text/html; charset=iso-8859-1

# curl -x127.0.0.1:80 www.bbb.com -I
HTTP/1.1 301 Moved Permanently
Date: Tue, 05 May 2015 18:40:28 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Location: http://www.123.com/
Cache-Control: max-age=0
Expires: Tue, 05 May 2015 18:40:28 GMT
Content-Type: text/html; charset=iso-8859-1

配置静态文件缓存
# vi/usr/local/apache2/conf/extra/httpd-vhosts.conf

<Ifmodule mod_headers.c>
<filesmatch "\.(html|htm|txt)$">
header set cache-control "max-age=3600"
</filesmatch>
<filesmatch "\.(css|js|swf)$">
header set cache-control "max-age=604800"
</filesmatch>
<filesmatch "\.(ico|gif|jpg|jpeg|png|flv|pdf)$">
header set cache-control "max-age=29030400"
</filesmatch>
</ifmodule>

# curl -x127.0.0.1:80 www.123.com/static/js/logging.js -I
HTTP/1.1 200 OK
Date: Tue, 05 May 2015 18:59:19 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Last-Modified: Fri, 26 Dec 2014 01:49:42 GMT
ETag: "22a88-25b-50b14bd049980"
Accept-Ranges: bytes
Content-Length: 603
cache-control: max-age=604800
Content-Type: application/javascript

配置访问控制
# vi/usr/local/apache2/conf/extra/httpd-vhosts.conf
<Directory /data/www/>
    <filesmatch ".*">
      Order deny,allow
      Deny from all
      Allow from 127.0.0.1
    </filesmatch>
</Directory>

# curl -x192.168.1.110:80 www.123.com/1.txt -I
HTTP/1.1 403 Forbidden
Date: Tue, 05 May 2015 19:17:26 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Content-Type: text/html; charset=iso-8859-1
# curl -x127.0.0.1:80 www.123.com/1.txt -I
HTTP/1.1 200 OK
Date: Tue, 05 May 2015 19:18:49 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Last-Modified: Sat, 02 May 2015 15:22:31 GMT
ETag: "2325b-a-5151ae5c57002"
Accept-Ranges: bytes
Content-Length: 10
cache-control: max-age=3600
Content-Type: text/plain
这样配置会导致不能访问网站,在工作中是不允许的,一般应用于禁止访问网站的重要目录。

禁止访问某个目录:
# vi/usr/local/apache2/conf/extra/httpd-vhosts.conf
<Directory /data/www/admin/>
    <filesmatch ".*">
      Order deny,allow
      Deny from all
      Allow from 127.0.0.1
    </filesmatch>
    </Directory>

# curl -x127.0.0.1:80 www.123.com/admin/1.txt -I//没有创建目录及文件导致错误
HTTP/1.1 404 Not Found
Date: Tue, 05 May 2015 19:23:04 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Content-Type: text/html; charset=iso-8859-1
# mkdir /data/www/admin/
# touch /data/www/admin/1.txt
# curl -x127.0.0.1:80 www.123.com/admin/1.txt -I
HTTP/1.1 200 OK
Date: Tue, 05 May 2015 19:24:08 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Last-Modified: Tue, 05 May 2015 19:24:06 GMT
ETag: "2325e-0-5155a9f3c2ff5"
Accept-Ranges: bytes
cache-control: max-age=3600
Content-Type: text/plain
X-Pad: avoid browser bug

# curl -x192.168.1.110:80 www.123.com/admin/1.txt -I
HTTP/1.1 403 Forbidden
Date: Tue, 05 May 2015 19:24:23 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Content-Type: text/html; charset=iso-8859-1


禁止访问某个文件
# vi/usr/local/apache2/conf/extra/httpd-vhosts.conf
    <Directory /data/www/>
    <filesmatch "^admin.php(.*)$">
      Order deny,allow
      Deny from all
      Allow from 127.0.0.1
    </filesmatch>
    </Directory>
# curl -x192.168.1.110:80 www.123.com/admin.phpsfsafa -I
HTTP/1.1 403 Forbidden
Date: Tue, 05 May 2015 20:12:46 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Content-Type: text/html; charset=iso-8859-1
# curl -x192.168.1.110:80 www.123.com/admin.php -I
HTTP/1.1 403 Forbidden
Date: Tue, 05 May 2015 20:15:35 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
Content-Type: text/html; charset=iso-8859-1
# curl -x127.0.0.1:80 www.123.com/admin.php -I
HTTP/1.1 200 OK
Date: Tue, 05 May 2015 20:15:58 GMT
Server: Apache/2.2.16 (Unix) DAV/2 PHP/5.3.28
X-Powered-By: PHP/5.3.28
*******
Content-Type: text/html; charset=gbk

限制php解析
# vi/usr/local/apache2/conf/extra/httpd-vhosts.conf
<Directory /data/www/path>
      php_admin_flag engine off
       <filesmatch "(.*)php">
      Order deny,allow
      Deny from all
      </filesmatch>
</Directory>
www]# curl -x192.168.1.110:80 'http://www.123.com/path/1.php'
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /path/1.php
on this server.</p>
</body></html>
discuz伪静态配置:
首先在论坛后台的管理中心选择全局-->SEO设置-->URL 静态化选项上全部点勾-->提交
<IfModule mod_rewrite.c>

         RewriteEngine on
         RewriteCond %{HTTP_HOST} ^www.aaa.com$
         RewriteCond %{HTTP_HOST} ^www.bbb.com$
         RewriteRule ^/(.*)$ http://www.123.com/$1
         RewriteCond %{QUERY_STRING} ^(.*)$
         RewriteRule ^/topic-(.+)\.html$ /portal.php?mod=topic&topic=$1&%1
         RewriteCond %{QUERY_STRING} ^(.*)$
         RewriteRule ^/article-(+)-(+)\.html$ /portal.php?mod=view&aid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/forum-(\w+)-(+)\.html$ /forum.php?mod=forumdisplay&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/thread-(+)-(+)-(+)\.html$ /forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/group-(+)-(+)\.html$ /forum.php?mod=group&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/space-(username|uid)-(.+)\.html$ /home.php?mod=space&$1=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/blog-(+)-(+)\.html$ /home.php?mod=space&uid=$1&do=blog&id=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/archiver/(fid|tid)-(+)\.html$ /archiver/index.php?action=$1&value=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^/(+*)-(+)\.html$ /plugin.php?id=$1:$2&%1
    </IfModule>



页: [1]
查看完整版本: apache的常用配置