用户认证,域名跳转以及日志

Apache用户认证


1.修改虚拟主机配置文件

[[email protected] ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.example.com
    <Directory /data/wwwroot/111.com>              #指定目录
        AllowOverride AuthConfig                            #打开认证
        AuthName "111.com user auth"                  #自定义认证名字
        AuthType Basic                                             #认证类型,一般Basic
        AuthUserFile /data/.htpasswd                     #指定密码文件位置
        require valid-user                                          #指定需要认证的用户为全部可用用户
    </Directory>
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
</VirtualHost>

2.创建密码文件

[[email protected] ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd weixing              #-c创建,-m  加密方式
New password:

3.重新加载

[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful

4.测试发现无法打开,通过检查发现是防火墙问题,需要打开80端口:

[[email protected] ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT

5.通过curl测试:401代表需要验证

[[email protected] ~]# 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>
<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>
[[email protected] ~]# curl -x127.0.0.1:80  111.com -I
HTTP/1.1 401 Unauthorized
Date: Fri, 02 Mar 2018 16:00:10 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

6.curl通过命令输入密码:

[[email protected] ~]# curl -x127.0.0.1:80 -uweixing:w 111.com
111.com[[email protected] ~]#
[[email protected] ~]# curl -x127.0.0.1:80 -uweixing:w 111.com -I
HTTP/1.1 200 OK
Date: Fri, 02 Mar 2018 16:01:50 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

7.针对单个文件进行认证:

[[email protected] ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.example.com
   # <Directory /data/wwwroot/111.com>
    <FilesMatch 123.php>
        AllowOverride AuthConfig
        AuthName "111.com user auth"
        AuthType Basic
        AuthUserFile /data/.htpasswd
        require valid-user
    </FilesMatch>
   # </Directory>
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
</VirtualHost>

8.编辑需要加密的文件

[[email protected] ~]# vim /data/wwwroot/111.com/123.php

9.进行测试
访问111.com不用输入密码了

[[email protected] ~]# curl -x127.0.0.1:80 111.com
111.com[[email protected] ~]# curl -x127.0.0.1:80 111.com -I
HTTP/1.1 200 OK
Date: Fri, 02 Mar 2018 16:07:38 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

访问123.php才需要

[[email protected] ~]# curl -x127.0.0.1:80 111.com/123.php
<!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>
[[email protected] ~]# curl -x127.0.0.1:80 -uweixing:wei1 111.com/123.php
123.php[[email protected] ~]# curl -x127.0.0.1:80 -uweixing:wei1 111.com/123.php -I
HTTP/1.1 200 OK
Date: Fri, 02 Mar 2018 16:08:50 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

域名跳转


1.编辑虚拟主机配置文件

[[email protected] ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.example.com
   # <Directory /data/wwwroot/111.com>
   # <FilesMatch 123.php>
   #    AllowOverride AuthConfig
   #    AuthName "111.com user auth"
   #    AuthType Basic
   #    AuthUserFile /data/.htpasswd
   #    require valid-user
   # </FilesMatch>
   # </Directory>
   <IfModule mod_rewrite.c>
        RewriteEngine on          #打开rewrite功能
                RewriteCond %{HTTP_HOST} !^111.com$              #定义跳转
        RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]         #301代表永久生效,302临时生效,L表示一次
   </IfModule>

    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
</VirtualHost>

2.编辑模块加载rewrite

[[email protected] ~]# vi /usr/local/apache2.4/conf/httpd.conf
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful

3.实现跳转:

[[email protected] ~]# curl -x127.0.0.1:80 111.com
111.com[[email protected] ~]# curl -x127.0.0.1:80 www.example.com -I
HTTP/1.1 301 Moved Permanently
Date: Sat, 03 Mar 2018 01:14:16 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Location: http://111.com/
Content-Type: text/html; charset=iso-8859-1

[[email protected] ~]# curl -x127.0.0.1:80 www.example.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://111.com/">here</a>.</p>
</body></html>

访问日志

1.查看日志:

[[email protected] ~]# ls /usr/local/apache2.4/logs/
111.com-access_log  abc.com-access_log  access_log  httpd.pid
111.com-error_log   abc.com-error_log   error_log
[[email protected] ~]# cat /usr/local/apache2.4/logs/111.com-access_log
192.168.188.130 - - [01/Mar/2018:22:31:57 +0800] "GET HTTP://www.example.com/ HTTP/1.1" 200 7
127.0.0.1 - - [02/Mar/2018:23:18:25 +0800] "GET HTTP://111.com/ HTTP/1.1" 401 381
127.0.0.1 - - [02/Mar/2018:23:18:44 +0800] "HEAD HTTP://111.com/ HTTP/1.1" 401 -
127.0.0.1 - - [02/Mar/2018:23:36:39 +0800] "HEAD HTTP://111.com/ HTTP/1.1" 401 -
192.168.188.130 - - [02/Mar/2018:23:38:10 +0800] "GET HTTP://111.com/ HTTP/1.1" 401 381
192.168.188.1 - - [02/Mar/2018:23:42:54 +0800] "GET / HTTP/1.1" 401 381
192.168.188.1 - weixing [02/Mar/2018:23:43:22 +0800] "GET / HTTP/1.1" 200 7
192.168.188.1 - - [02/Mar/2018:23:43:23 +0800] "GET /favicon.ico HTTP/1.1" 401 381
192.168.188.1 - weixing [02/Mar/2018:23:43:35 +0800] "GET /favicon.ico HTTP/1.1" 404 209
192.168.188.1 - weixing [02/Mar/2018:23:43:39 +0800] "GET / HTTP/1.1" 200 7
192.168.188.1 - weixing [02/Mar/2018:23:43:39 +0800] "GET /favicon.ico HTTP/1.1" 404 209
192.168.188.1 - weixing [02/Mar/2018:23:43:41 +0800] "GET / HTTP/1.1" 200 7
192.168.188.1 - weixing [02/Mar/2018:23:43:41 +0800] "GET /favicon.ico HTTP/1.1" 404 209
192.168.188.1 - weixing [02/Mar/2018:23:43:49 +0800] "GET / HTTP/1.1" 200 7
192.168.188.1 - weixing [02/Mar/2018:23:43:49 +0800] "GET /favicon.ico HTTP/1.1" 404 209
192.168.188.1 - weixing [02/Mar/2018:23:43:59 +0800] "GET / HTTP/1.1" 200 7
192.168.188.1 - weixing [02/Mar/2018:23:43:59 +0800] "GET /favicon.ico HTTP/1.1" 404 209

2.另一种格式的日志:

192.168.188.1 - - [03/Mar/2018:09:35:06 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.8.1000 Chrome/30.0.1599.101 Safari/537.36"
192.168.188.1 - - [03/Mar/2018:09:35:06 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.8.1000 Chrome/30.0.1599.101 Safari/537.36"
192.168.188.1 - - [03/Mar/2018:09:35:07 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.8.1000 Chrome/30.0.1599.101 Safari/537.36"
192.168.188.1 - - [03/Mar/2018:09:35:07 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.8.1000 Chrome/30.0.1599.101 Safari/537.36"
192.168.188.1 - - [03/Mar/2018:09:35:08 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.8.1000 Chrome/30.0.1599.101 Safari/537.36"
192.168.188.1 - - [03/Mar/2018:09:35:08 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.8.1000 Chrome/30.0.1599.101 Safari/537.36"

原文地址:http://blog.51cto.com/13517254/2082204

时间: 2024-08-30 10:27:53

用户认证,域名跳转以及日志的相关文章

apache 配置用户认证 域名跳转 日志 静态缓存文件 防盗链接

配置文件:/usr/local/apache2/conf/extra/httpd-vhosts.conf <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/data/www" ServerName www.1.com ServerAlias www.a.com www.b.com #配置用户认证 <Directory /data/www> AllowOverride AuthConfi

5.Apache用户认证,域名跳转,访问日志

[toc] Apache用户认证 11.18 Apache用户认证 用户认证功能就是在用户访问网站的时候,需要输入用户名密码才能进行访问.一些比较好总要的站点和网站后台都会加上用户认证,以保证安全. 1.下面对xavi.com站点来做一个全站的用户认证: vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把xavi.com那个虚拟主机编辑成如下内容 <VirtualHost *:80> DocumentRoot "/dat

Apache配置用户认证、域名跳转、日志轮询、静态文件缓存、防盗链

使用版本为httpd-2.2.29 源码编译安装环境. 1.配置网站用户认证 编辑虚拟机主机配置文件 /usr/local/apache2/conf/extra/httpd-vhosts.conf,在虚拟主机配置文件段内加入绿色标示代码: <VirtualHost *:80> DocumentRoot "/data/www" ServerName www.123.com ServerAlias www.a.com www.b.com     <Directory *&

apachey用户认证,域名跳转,日志

apache 网站用户认证编辑配置文件第一个是默认虚拟主机修改第二个虚拟主机生成密码文件 并创建用户 -m 加密类型 -c 创建 /data 文件位置重新加载配置文件访问主机 401报错 需要用户认证针对某一个单个文件修改配置文件访问限制文件时显示401域名跳转301 永久跳转虚拟主机配置文件mod_rewrite COND 定义条件rewriterule 定义规则 L表示只跳一次302临时重定向测试80监听全网 :::表示都可以-I 表示只看状态码 不看内容自动跳转到111.com 404这个

4.13 apache用户认证,跳转和访问日志

Apache用户认证 有的网站在访问的时候需要我们输入账户名和密码,这样做的好处是增加了安全性,但是用户体验会很差.但是在我们在工作中还需要在一些重要的地方做一些安全认证. 首先我们编辑虚拟主机的配置文件 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf我们用第二个虚拟主机做实验,然后在 ServerName下面下上如下的内容<Directory /data/wwwroot/123.com> //指定认证的目录(这里的网址要和前面配置文

Apache配置域名跳转、日志切割、静态缓存、防盗链、访问控制

·/usr/local/apache2/bin/apachectl  -M:查看安装了哪些模块 ·/usr/local/apache/bin/apachectl   -V:查看使用的模式 ·/usr/local/apache2/bin/apachectl  -t:检查语法错误 ·/usr/local/apache2/bin/apachectl  -l:查看安装的库文件 ·/usr/local/apache2/bin/apachectl graceful:重新加载配置 ·/usr/local/ap

apache配置-域名跳转、日志切割、静态缓存、防盗链

·/usr/local/apache2/bin/apachectl -M:查看安装了哪些模块     ·/usr/local/apache2/bin/apachectl -t:检查语法错误 ·/usr/local/apache2/bin/apachectl -l:查看安装的库文件 ·/usr/local/apache2/bin/apachectl graceful:重新加载配置·/usr/local/apache2/htcocs         主页存放目录 ·/usr/local/apache

Linux学习总结(四十)lnmp之nginx安装 用户认证 域名重定向

1 nginx 介绍 Nginx官网 nginx.org,最新版1.13,最新稳定版1.12 Nginx应用场景:web服务.反向代理.负载均衡Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty 2 nginx 安装 cd /usr

Apache的用户认证、域名跳转、Apache的访问日志

Apache的用户认证 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把111.com那个虚拟主机编辑成如下内容 <VirtualHost *:80>DocumentRoot "/data/wwwroot/111.com"ServerName 111.comServerAlias www.example.com<Directory /data/wwwroot/111.com>AllowOverri