apache常见错误

常见错误汇总1. 防盗链及地址重写1.1让Apache 支持rewrite 模块在编译源码安装apache 时,加入选项 –enable-rewrite1.2找到mod_rewrite.c文件进入a

常见错误汇总

1. 防盗链及地址重写

1.1让Apache 支持rewrite 模块

在编译源码安装apache 时,加入选项 –enable-rewrite

1.2找到mod_rewrite.c文件

进入apache 源码目录下找到mod_rewrite.c文件【Http-2.2版本一般在/modules/mappers/】

[root@localhost ~]# cd httpd-2.2.7/modules/mappers/

1.3编译mod_rewrite.c文件

说明:/usr/local/apache/为apache 安装路径,apxs 必须为绝对路径。

[root@localhost mappers]# /usr/local/apache/bin/apxs -c mod_rewrite.c

[root@localhost mappers]# /usr/local/apache/bin/apxs -i -a -n mod_rewrite mod_rewrite.la

1.4查看是否生成mod_rewrite.so

[root@localhost mappers]# ls /usr/local/apache/modules

如果有mod_rewrite.so,说明编译成功。

1.5修改httpd.conf 加入加载mod_rewrite模块。

LoadModule rewrite_module modules/mod_rewrite.so

###################################################

如果出现这个错误:

Syntax error on line 329 of /usr/local/apache2/conf/httpd.conf:

Can`t loacte API module staructure `mod_rewrite_module` in file

/usr/local/apache2/modules/mod_rewrite.so:/usr/local/apache2/lib/libapr-

0.so.0:undefined symbol:mod_rewrite_module

修改http.conf

,

原来:httpd.conf 里面写的mod_rewrite_module

改成 rewrite_module

如果重启apache 出现这个错误

module rewrite_module is built-in and can`t be loaded

表示模块是内建的,不用再调入, 注释掉

#LoadModule rewrite_module modules/mod_rewrite.so

#####################################################

1.6加入防盗链及重写规则

说明:test.com 为测试的网站域名,改成自己的网站域名即可。

如果网站采用.htaccess 文件来管理重写规则,则需要在该文件中加入: RewriteEngine on

RewriteCond {HTTP_REFERER} !^$

RewriteCond {HTTP_REFERER} !^http://test.com/.*$ [NC]

RewriteCond {HTTP_REFERER} !^http://test.com$ [NC]

RewriteCond {HTTP_REFERER} !^http://www.test.com/.*$ [NC]

RewriteCond {HTTP_REFERER} !^http://www.test.com$ [NC]

RewriteRule .*.(jpg|gif|png|jpeg|js|css|swf)$ http://www.test.com/404.html [R,L] 如果网站不是采用上述方式,则在网站配置文件(httpd.conf)中加入即可。

1.7开启404导航到指定页面 在

.htaccess

文件中添加:

ErrorDocument 404 /404.html //404.html名称自定义404页面

2. 隐藏apache 版本信息

[root@localhost ~]# curl –I 10.10.10.1

,

修改httpd.conf 文件,加入:

#ServerTokens Full 默认值是Full

ServerTokens Prod //表示只显示产品名称,不显示具体版本号

#ServerSignature On 默认值是On

ServerSignature Off

重启apache 后,再次查看,只显示了服务的名字。

3. apache 禁用目录浏览

默认的目录命令:

清除了Options 的Indexes 后:

也可以保留Indexes 指令,使用破折号来禁用此命令。(即:-Indexes)

4. Apache日志中获取访客真实IP 的解决方案 1、打开文件:

/etc/httpd/conf/httd.conf

2、在文件中查找:”CustomLog ”, 找到如下配置块: 查看到当前使用的LogFormat 为”combined ” (如果实际启用的为其他日志格式,替换相应的格式定义即可) 。

#

# For a single logfile with access, agent, and referer information

# (Combined Logfile Format), use the following directive:

,

#

CustomLog logs/access_log combined

3、在文件中查找:”LogFormat ”, 找到如下配置块(combined 格式定义):

LogFormat "h l u t "r" >s b "{Referer}i" "{User-Agent}i"" combined 将其修改为:

LogFormat "h l u t "r" >s b "{Referer}i" "{User-Agent}i" "{X-Forwarded-For}i" " combined

4、保存并关闭文件/etc/httpd/conf/httd.conf。

5、重启Apache 服务。

5.Apache 只允许域名访问

在Apache 配置文件

6.301跳转不带www 域名的访问

如果网站有.htaccess 文件,则需要在该文件中添加如下代码:

//实现无www 访问跳转到www 全域名访问

RewriteCond {HTTP_HOST} ^test.com [NC]

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

标签: