当设置了用户认证后,用户访问网站时,须要输入用户名和密码才能访问。
能够全局设置,也能够为某几个虚拟主机单独配置。
下面以全局配置进行操做示例。html
[root@test-a ~]# vim /usr/local/apache2.4/conf/httpd.conf
<Directory "/usr/local/apache2.4/htdocs"> # # Possible values for the Options directive are "None", "All", # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn't give it to you. # # The Options directive is both complicated and important. Please see # http://httpd.apache.org/docs/2.4/mod/core.html#options # for more information. # Options Indexes FollowSymLinks # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # AllowOverride FileInfo AuthConfig Limit # # AllowOverride None # # Controls who can get stuff from this server. # # Require all granted ALLOWOVERRIDE AuthConfig # 这里至关于打开了认证开关 AuthType Basic # 认证类型,通常使用Basic AuthName "test" # 自定义认证的名字,做用不大 AuthUserFile /data/.webpasswd # 认证秘钥文件(使用apche自带的工具生成) require valid-user # 指定须要认证的用户为所有用户 </Directory>
[root@test-a ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.webpasswd test # -c 建立密钥文件,-m 使用md5加密 New password: Re-type new password: Adding password for user test # 已有文件,添加用户 [root@test-a ~]# /usr/local/apache2.4/bin/htpasswd -m /data/.webpasswd test1 New password: Re-type new password: Adding password for user test1
[root@test-a ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK
[root@test-a ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@test-a ~]# curl -x127.0.0.1:80 www.123.com # 返回401错误码 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Unauthorized</title> </head><body> <h1>Unauthorized</h1> <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@test-a ~]# curl -x127.0.0.1:80 -utest:test www.123.com # 带用户名密码,访问OK <html><body><h1>It works!</h1></body></html>
<VirtualHost *:80> DocumentRoot "/tmp/web-default" # 网站资源目录 ServerName test.com # 域名 ServerAlias www.test.com www.123.com # 域名别名 <IfModule mod_rewrite.c> # 须要mod_rewrite模块支持 RewriteEngine on #打开rewrite功能 RewriteCond %{HTTP_HOST} !^www.123.com$ # 定义rewrite的条件,主机名(域名)不是www.123.com知足条件 RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L] # 定义rewrite规则,当知足上面的条件时,这条规则才会执行,301是永久重定向,302是临时重定向,临时的不会增长搜索引擎的权重,通常都是用301 </IfModule> </VirtualHost>
[root@test-a apache2.4]# /usr/local/apache2.4/bin/apachectl -M | grep rewrite rewrite_module (shared) # 删除httpd.conf 里 rewrite_module (shared) 前面的# # 从新加载配置 [root@test-a apache2.4]# /usr/local/apache2.4/bin/apachectl graceful
# curl -x127.0.0.1:80 -I www.123.com HTTP/1.1 301 Moved Permanently Date: Fri, 16 Nov 2018 08:10:20 GMT Server: Apache/2.4.37 (Unix) PHP/5.6.32 Location: http://www.123.com/ Content-Type: text/html; charset=iso-8859-1
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined LogFormat "%h %l %u %t "%r" %>s %b" common
h来源ip、l用户密码、u用户、t时间、r行为,网址、s状态码、b大小
{Referer}浏览器进入一个网站后的第二个页面,referer记录的日志的就是第一个访问页面的网址是什么、在百度中搜索进入开源中国网站首页后,referer记录的就是百度搜出来的结果页面网址
{User-Agent}用户代理(怎么得到网址内容,是浏览器仍是curl)web