针对httpd用户加验证

修改虚拟主机配置文件。

vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把123.com那个虚拟主机编辑成如下内容 <VirtualHost *:80> DocumentRoot "/data/wwwroot/www.123.com" ServerName www.123.com <Directory /data/wwwroot/www.123.com> //指定认证的目录 AllowOverride AuthConfig //这个相当于打开认证的开关 AuthName "123.com user auth" //自定义认证的名字,作用不大 AuthType Basic //认证的类型,一般为Basic,其他类型阿铭没用过 AuthUserFile /data/.htpasswd //指定密码文件所在位置 require valid-user //指定需要认证的用户为全部可用用户 </Directory> </VirtualHost>

添加aming用户并配置密码,这个地方是难点, [root@aminglinux-001 ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd aming New password: Re-type new password: Adding password for user aming [root@aminglinux-001 ~]# cat /data/.htpasswd aming:$apr1$ddMCbmzv$ZgObr361t5HLhMsdewYf2/ [root@aminglinux-001 ~]# /usr/local/apache2.4/bin/htpasswd -m /data/.htpasswd zhangsan New password: Re-type new password: Adding password for user zhangsan 重新加载配置 -t,graceful [root@aminglinux-001 wwwroot]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@aminglinux-001 wwwroot]# /usr/local/apache2.4/bin/apachectl graceful

[root@aminglinux-001 wwwroot]# curl -x127.0.0.1:80 111.com <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Unauthorized</title> </head><body> Unauthorized <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.</p> </body></html> [root@aminglinux-001 wwwroot]# curl -x127.0.0.1:80 111.com -I HTTP/1.1 401 Unauthorized Date: Sun, 04 Mar 2018 09:01:00 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 WWW-Authenticate: Basic realm="111.com user auth" Content-Type: text/html; charset=iso-8859-1

在本机host文件中加入本机ip 111.com 再浏览器尝试,提示输入用户名密码,显示内容

如果在命令行加入-u 参数测试,-u 后面跟用户名:密码,也可成功。 [root@aminglinux-001 wwwroot]# curl -x127.0.0.1:80 -uaming:1q2w3e 111.com -I HTTP/1.1 200 OK Date: Sun, 04 Mar 2018 09:04:54 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 X-Powered-By: PHP/7.1.6 Content-Type: text/html; charset=UTF-8

针对单个文件做认证 编辑虚拟主机配置文件 [root@aminglinux-001 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 重启服务 [root@aminglinux-001 ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@aminglinux-001 ~]# /usr/local/apache2.4/bin/apachectl graceful

添加123.php文件并编辑

命令行访问1111.com主机成功,访问111.com/123.php错误。 [root@aminglinux-001 ~]# curl -x192.168.67.128:80 111.com/123.php <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Unauthorized</title> </head><body> Unauthorized <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.</p> </body></html>

此时,针对此文件添加用户名和密码即可。

[root@aminglinux-001 ~]# curl -x127.0.0.1:80 -uaming:1q2w3e 111.com/123.php -I HTTP/1.1 200 OK Date: Sun, 04 Mar 2018 09:26:59 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 X-Powered-By: PHP/7.1.6 Content-Type: text/html; charset=UTF-8 登录 网页验证成功。

又尝试了如果添加不是root用户,也不是系统用户,只是针对httpd的用户,新增一个k用户,并设置密码,也可以访问123.php文件。

域名跳转***

vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

RewriteRule ^/(.)$ http://111.com/$1 [R=301,L] $1 表示的是前面第一个小括号的内容, 如果前面有第二个小括号,后面应该加$2 比如 RewriteRule ^/(.)(【1-9】+)$ http://111.com/$1 $2[R=301,L]

定义状态码, R=301 ,L代表只跳转一次,last R=301表示永久生效 R=302表示临时生效,不会降低原有域名权重 重新加载 检查是否加载rewrite模块

检查模块是否加载到
vi /usr/local/apache2.4/conf/httpd.conf

重新加载,看看是否加载到

修改虚拟主机配置文件,添加域名 2111.com.cn 重启,生效

开始测试域名跳转

显示301 跳转成功

403没授权或者用户名密码不对 200 用户名密码正确 通过网页测试访问 2111.com.cn/123.php,跳转到

记录访问日志

编辑配置文件 vim /usr/local/apache2.4/conf/httpd.conf //搜索LogFormat 修改记录日志格式 重新加载配置文件 -t ,graceful 开始测试: 1、查看日志文件,没有跳转过来的连接跟之前相同。 2、在51博客中创建一个博客新建一个链接,在博客中打开链接后,在日志里即可看到新的日志文件