apache常見錯誤
常見錯誤匯總1. 防盜鏈及地址重寫1.1讓Apache 支持rewrite 模塊在編譯源碼安裝apache 時,加入選項(xiàng) –enable-rewrite1.2找到mod_rewrite.c文件進(jìn)入a
常見錯誤匯總
1. 防盜鏈及地址重寫
1.1讓Apache 支持rewrite 模塊
在編譯源碼安裝apache 時,加入選項(xiàng) –enable-rewrite
1.2找到mod_rewrite.c文件
進(jìn)入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
###################################################
如果出現(xiàn)這個錯誤:
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 出現(xiàn)這個錯誤
module rewrite_module is built-in and can`t be loaded
表示模塊是內(nèi)建的,不用再調(diào)入, 注釋掉
#LoadModule rewrite_module modules/mod_rewrite.so
#####################################################
1.6加入防盜鏈及重寫規(guī)則
說明:test.com 為測試的網(wǎng)站域名,改成自己的網(wǎng)站域名即可。
如果網(wǎng)站采用.htaccess 文件來管理重寫規(guī)則,則需要在該文件中加入: 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] 如果網(wǎng)站不是采用上述方式,則在網(wǎng)站配置文件(httpd.conf)中加入即可。
1.7開啟404導(dǎo)航到指定頁面 在
.htaccess
文件中添加:
ErrorDocument 404 /404.html //404.html名稱自定義404頁面
2. 隱藏apache 版本信息
[root@localhost ~]# curl –I 10.10.10.1
,修改httpd.conf 文件,加入:
#ServerTokens Full 默認(rèn)值是Full
ServerTokens Prod //表示只顯示產(chǎn)品名稱,不顯示具體版本號
#ServerSignature On 默認(rèn)值是On
ServerSignature Off
重啟apache 后,再次查看,只顯示了服務(wù)的名字。
3. apache 禁用目錄瀏覽
默認(rèn)的目錄命令:
清除了Options 的Indexes 后:
也可以保留Indexes 指令,使用破折號來禁用此命令。(即:-Indexes)
4. Apache日志中獲取訪客真實(shí)IP 的解決方案 1、打開文件:
/etc/httpd/conf/httd.conf
。
2、在文件中查找:”CustomLog ”, 找到如下配置塊: 查看到當(dāng)前使用的LogFormat 為”combined ” (如果實(shí)際啟用的為其他日志格式,替換相應(yīng)的格式定義即可) 。
#
# 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、保存并關(guān)閉文件/etc/httpd/conf/httd.conf。
5、重啟Apache 服務(wù)。
5.Apache 只允許域名訪問
在Apache 配置文件
6.301跳轉(zhuǎn)不帶www 域名的訪問
如果網(wǎng)站有.htaccess 文件,則需要在該文件中添加如下代碼:
//實(shí)現(xiàn)無www 訪問跳轉(zhuǎn)到www 全域名訪問
RewriteCond {HTTP_HOST} ^test.com [NC]
RewriteRule ^(.*)$ http://www.test.com/$1 [L,R=301]