编辑虚拟主机配置文件:php
[root@1 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com <Directory /data/wwwroot/111.com> SetEnvIfNoCase Referer "http://111.com" local_ref SetEnvIfNoCase Referer "http://ask.apelearn.com" local_ref SetEnvIfNoCase Referer "^$" local_ref #定义referer白名单 <FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)"> Order Allow,Deny Allow from env=local_ref #定义规则:容许变量local_ref指定的referer访问,拒绝其余全部访问。 </FilesMatch> </Directory> ErrorLog "logs/111.com-error_log" SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img SetEnvIf Request_URI ".*\.png$" img SetEnvIf Request_URI ".*\.bmp$" img SetEnvIf Request_URI ".*\.swf$" img SetEnvIf Request_URI ".*\.js$" img SetEnvIf Request_URI ".*\.css$" img CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img #CustomLog "logs/111.com-access_log" combined env=!img </VirtualHost> 检测语法错误并重载: [root@1 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@1 ~]# /usr/local/apache2.4/bin/apachectl graceful
注: 若是在referer白名单中不加“^#”(空referer),直接访问指定内容将会被拒绝。css
curl -e 指定refererhtml
[root@1 ~]# curl -e "http://ask.apelearn.com/" -x192.168.8.131:80 111.com/baidu.png -I
编辑虚拟主机配置文件:mysql
在配置文件加入以下参数: [root@1 admin]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf …… <Directory /data/wwwroot/111.com/admin/> Order deny,allow Deny from all Allow from 127.0.0.1 #只容许IP--127.0.0.1访问“/data/wwwroot/111.com/admin/”目录中的内容 </Directory> …… [root@1 admin]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@1 admin]# /usr/local/apache2.4/bin/apachectl graceful 测试: [root1 admin]# curl -x127.0.0.1:80 111.com/admin/index.php 121212 更换IP访问: [root@1 admin]# curl -x192.168.8.131:80 111.com/admin/index.php -I HTTP/1.1 403 Forbidden Date: Wed, 02 Aug 2017 08:48:49 GMT Server: Apache/2.4.27 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1 #报错(403)!!!即,只有指定IP--127.0.0.1能够访问该目录。
**说明:**本节用于设定指定IP访问指定目录的权限!sql
使用FilesMatch参数: [root@1 admin]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf …… <Directory /data/wwwroot/111.com> <FilesMatch admin.php(.*)> Order deny,allow Deny from all Allow from 127.0.0.1 </FilesMatch> </Directory> …… [root@1 admin]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@1 admin]# /usr/local/apache2.4/bin/apachectl graceful [root@1 admin]# curl -x127.0.0.1:80 111.com/admin.php -I HTTP/1.1 404 Not Found Date: Wed, 02 Aug 2017 09:24:22 GMT Server: Apache/2.4.27 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1 #由于访问的文件不存在,因此报错:404!
说明: 本节内容应用于对某些请求设定权限。apache
禁止访问某些文件/目录vim
增长Files选项来控制,好比要不容许访问 .inc 扩展名的文件,保护php类库:服务器
<Files~"\.inc$"> Order Allow,Deny Deny from all </Files>
禁止访问某些指定的目录curl
可使用<DirectoryMatch> 正则匹配:测试
<Directory~"^/var/www/(.+/)*[0-9]{3}"> Order Allow,Deny Deny from all </Directory>
也可使用目录全局路径
经过文件匹配来进行禁止,好比禁止全部针对图片的访问:
<FilesMatch \.?i:gif|jpe?g|png)$> Order Allow,Deny Deny from all <FilesMatch>
针对URL相对路径的禁止访问
<Location /dir/> Order Allow,Deny Deny from all </Location>
[root@1 ~]# /usr/local/apache2/bin/apachectl -M
若是没有加载,须要进行加载配置。
2. 设置header
在Apache配置文件中加入下面参数:
Header add MyHeader "Hello"
在APACHE的httpd.conf中,KeepAlive指的是保持链接活跃,相似于Mysql的永久链接。换一句话说,若是将KeepAlive设置为On,那么来自同一客户端的请求就不须要再一次链接,避免每次请求都要新建一个链接而加剧服务器的负担。
KeepAlive的链接活跃时间固然是受KeepAliveTimeOut限制的。若是第二次请求和第一次请求之间超过KeepAliveTimeOut的时间的话,第一次链接就会中断,再新建第二个链接。
因此,通常状况下,图片较多的网站应该把KeepAlive设为On。可是KeepAliveTimeOut应该设置为多少秒就是一个值得讨论的问题了。
若是KeepAliveTimeOut设置的时间太短,例如设置为1秒,那么APACHE就会频繁的创建新链接,固然会耗费很多的资源;反过来,若是KeepAliveTimeOut设置的时间过长,例如设置为300秒,那么APACHE中确定有不少无用的链接会占用服务器的资源,也不是一件好事。
因此,到底要把KeepAliveTimeOut设置为多少,要看网站的流量、服务器的配置而定。
其实,这和MySql的机制有点类似,KeepAlive至关于mysql_ connect或mysql_ pconnect,KeepAliveTimeOut至关于wait_timeout。