PS:其实没有....用处的。。。
htpasswd命令是Apache的Web服务器内置工具,用于创建和更新储存用户名、域和用户基本认证的密码文件。
htpasswd [option] [参数]
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/www/sdw.com"
ServerName sdw.com
ServerAlias www.example.com
<Directory /data/www/sdw.com> //指定认证的目录
AllowOverride AuthConfig //这个相当于打开认证的开关
AuthName "sdw.com user auth" //自定义认证的名字,作用不大
AuthType Basic //认证的类型,一般为Basic
AuthUserFile /data/.htpasswd //指定密码文件所在位置
require valid-user //指定需要认证的用户为全部可用用户
</Directory>
ErrorLog "logs/111.com-error_logo"
CustomLog "logs/111.com-access_log" common
</VirtualHost>
[root@dl-001 wwwroot]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd dl
New password:
Re-type new password:
Adding password for user dl
[root@dl-001 wwwroot]# cat /data/.htpasswd
dl:$apr1$QaOr7opI$AfAGBv1/utJws62.S/sbl.
[root@dl-001 wwwroot]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@dl-001 wwwroot]# /usr/local/apache2.4/bin/apachectl graceful
使用curl进行测试
[root@dl-001 ~]# curl -x192.168.0.168:80 sdw.com
HTTP/1.1 401 Authorization Required //说明:因为生成了密码,所以在不指定用户名和密码的情况下会报401错误
Date: Wed, 20 Dec 2017 14:48:52 GMT
Server: Apache/2.2.34 (Unix) DAV/2 PHP/5.6.30
WWW-Authenticate: Basic realm="sdw.com user auth"
Content-Type: text/html; charset=iso-8859-1
域名跳转类似于将网页重新指向另一个网站,但区别是域名跳转会将域名本身重新指向网站,而不使用HTML或脚本来进行重新指向。当域名被设置为跳转至另一网站,域名的地址将不会保留在浏览器的URL栏中,该栏显示的会是新页面的URL。如果您希望保留该栏中的URL,则需要使用隐形跳转。
把www.sdw.com 跳转到 sdw.com上
<VirtualHost *:80>
ServerAdmin 1239666161@qq.com
DocumentRoot "/data/www/sdw.com"
ServerName sdw.com
ServerAlias www.sdw.com
<IfModule mod_rewrite.c>
#//需要mod_rewrite模块支持
RewriteEngine on
RewriteCond %{HTTP_HOST} !^sdw.com$
#//定义rewrite的条件,主机名(域名)不是www.123.com满足条件
RewriteRule ^/(.*)$ http://sdw.com/$1 [R=301,L]
#//定义rewrite规则,当满足上面的条件时,这条规则才会执行
</IfModule>
<Directory /data/www/sdw.com>
AllowOverride AuthConfig
AuthName "123.com user auth"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</Directory>
ErrorLog "logs/sdw.com-error_log"
CustomLog "logs/sdw.com-access_log" common
</VirtualHost>
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so //去掉#,以启用这个模块
[root@localhost ~]# curl -x 192.168.0.168:80 www.sdw.com -I
HTTP/1.1 301 Moved Permanently
Date: Sat, 03 Mar 2018 15:11:58 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Location: http://sdw.com/
Content-Type: text/html; charset=iso-8859-1
通过优化 可以更加方便进行查询日志
[root@localhost ~]# ls /usr/local/apache2/logs/
123test-access_log abstest-error_log dummy-host2.example.com-error_log error_log
123test-error_log access_log dummy-host.example.com-access_log httpd.pid
abctest-access_log dummy-host2.example.com-access_log dummy-host.example.com-error_log
[root@localhost ~]#cat /usr/local/apache2/logs/123test-access_log //common格式日志
192.168.204.128 - - [02/Mar/2018:19:06:28 +0800] "HEAD HTTP://linuxtestbak.com/ HTTP/1.1" 301 -
192.168.204.128 - - [02/Mar/2018:19:07:51 +0800] "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28
192.168.204.128 - - [02/Mar/2018:19:09:05 +0800] "HEAD HTTP://www.linuxtestbak.com/ HTTP/1.1" 301 -
192.168.204.1 - - [02/Mar/2018:19:10:55 +0800] "GET / HTTP/1.1" 200 28
192.168.204.1 - - [02/Mar/2018:19:11:08 +0800] "GET / HTTP/1.1" 200 28
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf //搜索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
说明:combined和common两种格式,默认使用common格式。
## 3、更改日志的格式为combined
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
ErrorLog "logs/123test-error_log"
CustomLog "logs/123test-access_log" combined
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
## 4,做一些访问操作之后,再查看日志。
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]# cat /usr/local/apache2.4/logs/123test-access_log
192.168.204.128 - - [02/Mar/2018:19:06:28 +0800] "HEAD HTTP://linuxtestbak.com/ HTTP/1.1" 301 -
192.168.204.128 - - [02/Mar/2018:19:07:51 +0800] "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28
192.168.204.128 - - [02/Mar/2018:19:09:05 +0800] "HEAD HTTP://www.linuxtestbak.com/ HTTP/1.1" 301 -
192.168.204.1 - - [02/Mar/2018:19:10:55 +0800] "GET / HTTP/1.1" 200 28
192.168.204.1 - - [02/Mar/2018:19:11:08 +0800] "GET / HTTP/1.1" 200 28
192.168.204.1 - - [02/Mar/2018:19:20:16 +0800] "GET / HTTP/1.1" 200 28 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
192.168.204.1 - - [02/Mar/2018:19:20:19 +0800] "GET / HTTP/1.1" 200 28 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
192.168.204.1 - - [02/Mar/2018:19:20:27 +0800] "GET / HTTP/1.1" 200 28 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
192.168.204.128 - - [02/Mar/2018:19:20:39 +0800] "HEAD HTTP://www.linuxtestbak.com/ HTTP/1.1" 301 - "-" "curl/7.29.0"
192.168.204.128 - - [02/Mar/2018:19:20:45 +0800] "HEAD HTTP://www.linuxtestbak.com/ HTTP/1.1" 301 - "-" "curl/7.29.0"
192.168.204.128 - - [02/Mar/2018:19:20:54 +0800] "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28 "-" "curl/7.29.0"
192.168.204.128 - - [02/Mar/2018:19:20:57 +0800] "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28 "-" "curl/7.29.0"
192.168.204.128 - - [02/Mar/2018:19:20:58 +0800] "GET HTTP://linuxtest.com/ HTTP/1.1" 200 28 "-" "curl/7.29.0"
原文地址:http://blog.51cto.com/sdwaqw/2082716