码迷,mamicode.com
首页 > 系统相关 > 详细

2018-04-13 Linux学习

时间:2018-04-14 19:22:46      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:Linux学习

11.18 Apache用户认证

httpd的用户认证

vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf   //把111.com那个虚拟主机编辑成如下内容

<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.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>

/usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd aming

重新加载配置
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful

绑定hosts,浏览器测试
curl -x127.0.0.1:80 111.com -I  //状态码为401
curl -x127.0.0.1:80 -uaming:passwd 111.com -I  //状态码为200

httpd的用户认证

还可以针对单个文件进行认证
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
<FilesMatch aming.php> //指定认证的目录
AllowOverride AuthConfig //这个相当于打开认证的开关
AuthName "111.com user auth" //自定义认证的名字,作用不大
AuthType Basic //认证的类型,一般为Basic,其他类型阿铭没用过
AuthUserFile /data/.htpasswd //指定密码文件所在位置
Require valid-user //指定需要认证的用户为全部可用用户
</FilesMatch>
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" common
</VirtualHost>

操作过程

[root@aming-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    <Directory /data/wwwroot/111.com>
        AllowOverride AuthConfig
        AuthName "111.com user auth"
        AuthType Basic
        AuthUserFile /data/.htpasswd
        Require valid-user
    </Directory>
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
</VirtualHost>

[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@aming-01 ~]# curl -x127.0.0.1:80 111.com -I
HTTP/1.1 401 Unauthorized
Date: Thu, 22 Mar 2018 20:10:27 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.34
WWW-Authenticate: Basic realm="111.com user auth"
Content-Type: text/html; charset=iso-8859-1

[root@aming-01 ~]# curl -x127.0.0.1:80 -uaming:123 111.com -I
HTTP/1.1 200 OK
Date: Thu, 22 Mar 2018 20:10:48 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.34
X-Powered-By: PHP/5.6.34
Content-Type: text/html; charset=UTF-8

[root@aming-01 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT

11.19-20 域名跳转

域名跳转

需求,把123.com域名跳转到www.123.com,配置如下:

<VirtualHost *:80>
    ServerAdmin abc@test.com
    DocumentRoot "/data/wwwroot/abc.com"
    ServerName abc.com
    ServerAlias www.abc.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规则,当满足上面的条件时,这条规则才会执行
    </IfModule>
    ErrorLog "logs/abc.com-error_log"
    CustomLog "logs/abc.com-access_log" common
</VirtualHost>

/usr/local/apache2.4/bin/apachectl -M|grep -i rewrite   //若无该模块需要编辑配置文件 http.conf ,删除 rewrite_module(shared) 前面的#

curl -x127.0.0.1:80 -I 123.com    //状态码为301

操作过程

<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.exampl.com 2111.com.cn
    #<Directory /data/wwwroot/111.com>
    #    AllowOverride AuthConfig
    #    AuthName "111.com user auth"
    #    AuthType Basic
    #    AuthUserFile /data/.htpasswd
    #    Require valid-user
    #</Directory>
    <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{HTTP_HOST} !^111.com$
       RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]
    </IfModule>
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
</VirtualHost>

[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl -M|grep -i rewrite
[root@aming-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf
[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl -M|grep -i rewrite
 rewrite_module (shared)

[root@aming-01 ~]# curl -x127.0.0.1:80 -I 2111.com.cn
HTTP/1.1 301 Moved Permanently
Date: Thu, 22 Mar 2018 21:18:05 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.34
Location: http://111.com/
Content-Type: text/html; charset=iso-8859-1

[root@aming-01 ~]# curl -x127.0.0.1:80 2111.com.cn
<!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>

[root@aming-01 ~]# curl -x192.168.104.160:80 http://111.com/dfafdjsaf
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /dfafdjsaf was not found on this server.</p>
</body></html>

11.21 Apache访问日志

访问日志

访问日志记录用户的每一个请求
vim /usr/local/apache2.4/conf/httpd.conf   //搜索 LogFormat
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

修改虚拟主机配置文件如下
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.exampl.com 2111.com.cn
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
</VirtualHost>

重新加载配置文件 -t , graceful

curl -x127.0.0.1:80 -I 111.com

tail /usr/local/apache2.4/logs/111.com-access_log

操作过程

<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.exampl.com 2111.com.cn
    #<Directory /data/wwwroot/111.com>
    #    AllowOverride AuthConfig
    #    AuthName "111.com user auth"
    #    AuthType Basic
    #    AuthUserFile /data/.htpasswd
    #    Require valid-user
    #</Directory>
    <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{HTTP_HOST} !^111.com$
       RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]
    </IfModule>
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" combined
</VirtualHost>

[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl graceful

[root@aming-01 ~]# tail -5 /usr/local/apache2.4/logs/111.com-access_log 
192.168.104.160 - - [23/Mar/2018:05:21:42 +0800] "GET http://111.com/dfafdjsaf HTTP/1.1" 404 207
192.168.104.1 - - [23/Mar/2018:05:42:37 +0800] "GET / HTTP/1.1" 200 8 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"
192.168.104.1 - - [23/Mar/2018:05:42:38 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://111.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"
192.168.104.160 - - [23/Mar/2018:05:43:35 +0800] "GET http://111.com/ HTTP/1.1" 200 8 "-" "curl/7.29.0"
192.168.104.160 - - [23/Mar/2018:05:43:42 +0800] "HEAD http://111.com/ HTTP/1.1" 200 - "-" "curl/7.29.0"

2018-04-13 Linux学习

标签:Linux学习

原文地址:http://blog.51cto.com/9298822/2103486

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!