httpd的页面登录认证
在httpd的虚拟主机配置文件中添加用户认证的密码的相关配置
<VirtualHost *:80>
? DocumentRoot "/usr/local/httpd/docs/123.com"
? ServerName 123.com
? ServerAlias www.123.com
? <directory /usr/local/httpd/docs/123.com>
? AllowOverride AuthConfig
? AuthName "Please is passwd :"
? AuthType Basic
? AuthUserFile /usr/local/httpd/conf/htpasswd
? require valid-user
? </Directory>
? ErrorLog "logs/123.com-error_log"
? CustomLog "logs/123.com-access_log" common
</VirtualHost>
directory 指定需要认证的网页根目录,访问此目录下的网页都需要输入认证的账户名和密码
AllowOverride AuthConfig 账户密码认证的生效配置,保持此配置即可
AuthName 账户密码认证输入时提示的信息
AuthType Basic 账户密码认证的类型,常用Basic的类型即可
AuthUserFile /usr/local/httpd/conf/htpasswd 指定账户密码配置文件的目录
require valid-user 指定需要认证的用户为全部可用的用户(配置文件内的用户)
使用httpd下的htpasswd文件来添加用户认证的名称及密码,第一次创建密码认证的文件时,必须要加-c选项,这样才能创建一个保存用户和密码的认证文件,后续新添加用户则不再需要使用-c选项
[[email protected] extra]# /usr/local/httpd/bin/htpasswd -c /usr/local/httpd/conf/htpasswd user
New password:
Re-type new password:
Adding password for user user ? ? ? ? ?
[[email protected] extra]# /usr/local/httpd/bin/htpasswd /usr/local/httpd/conf/htpasswd user1
New password:
Re-type new password:
Adding password for user user1
[[email protected] extra]# cat /usr/local/httpd/conf/htpasswd
user:$apr1$Xh6mrleA$/PHet3mzd4ZKbk7eLTMfH1
user1:$apr1$zK/oShKB$Q7.pLsBHEsphBA6HnCl0E/
重新加载一下httpd的配置
[[email protected] extra]# /usr/local/httpd/bin/apachectl graceful
访问域名检验用户认证设置的结果
域名跳转
httpd设置域名跳转
<VirtualHost *:80>
? DocumentRoot "/usr/local/httpd/docs/abcd.com"
? ServerName abcd.com
? ServerAlias www.abcd.com
? ErrorLog "logs/abcd.com-error_log"
? CustomLog "logs/abcd.com-access_log" common
? <IfModule mod_rewrite.c>
? RewriteEngine on
? RewriteCond %{HTTP_HOST} !^111.com$
? RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]
? </IfModule>
</VirtualHost>
IfModule mod_rewrite.c ? 表示需要mod_rewrite模块支持
RewriteEngine ?表示大考rewrite功能
RewriteCond 定义了rewrite跳转的条件
RewriteRule ?定义rewrite规则,满足RewriteCond条件时,会执行这里定义的规则
查看httpd是否加载了rewrite模块,若没有rewrite模块(httpd编译时大都会把这个模块安装上),按照下个步骤加载rewrite
[[email protected] conf]# /usr/local/httpd/bin/apachectl -M |grep rewrite
rewrite_module (shared)
打开httpd配置文件中的rewrite模块,查找rewrite的模块所在行去掉注释重新加载httpd配置文件即可
[[email protected] conf]# vim httpd.conf
~
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
[[email protected] conf]# /usr/local/httpd/bin/apachectl graceful
测试域名跳转的访问请求,配置文件里指定了301标识,这里的301也表示为域名跳转的标识
[[email protected] conf]# curl -x127.0.0.1:80 http://www.abcd.com -I
HTTP/1.1 301 Moved Permanently
Date: Sun, 29 Jul 2018 17:48:31 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.37
Location: http://111.com/
Content-Type: text/html; charset=iso-8859-1
httpd日志
httpd日志记录,在主配置文件中查找到LogFormat段的配置,LogFormat这里是让日志以特定形式记录保存,可以在这里自定义日志格式
<IfModule log_config_module>
? ?#
? ?# The following directives define some format nicknames for use with
? ?# a CustomLog directive (see below).
? ?#
? LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
? LogFormat "%h %l %u %t \"%r\" %>s %b" common
Referer是记录点击页面请求的信息
User-Agebt是记录访问段浏览器类型的信息
在虚拟主机中定义记录更详细的日志
[[email protected] httpd]# vim conf/extra/httpd-vhosts.conf
-------------------------省略的配置---------------------------------
? ErrorLog "logs/123.com-error_log"
? CustomLog "logs/123.com-access_log" combined ? <---记录模式,更为详细的日志信息
</VirtualHost>
重新加载httpd生效服务的配置
[[email protected] httpd]# /usr/local/httpd/bin/apachectl graceful
对比修改前后记录的日志内容,日志记了客户端访问的浏览器类型,访问页面和操作系统等信息
192.168.1.112 - - [30/Jul/2018:00:56:33 +0800] "GET / HTTP/1.1" 401 381
192.168.1.112 - - [30/Jul/2018:00:57:13 +0800] "GET /favicon.ico HTTP/1.1" 401 381
192.168.1.112 - - [30/Jul/2018:00:57:13 +0800] "GET /favicon.ico HTTP/1.1" 401 381
192.168.1.112 - user [30/Jul/2018:04:39:41 +0800] "GET / HTTP/1.1" 200 7
192.168.1.112 - user [30/Jul/2018:04:44:07 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
192.168.1.112 - user [30/Jul/2018:04:44:08 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
192.168.1.112 - user [30/Jul/2018:04:44:09 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
原文地址:http://blog.51cto.com/8844414/2153771
时间: 2024-10-09 14:56:55