Tagged: web服务器

秒懂系列|Apache用户认证配置之Basic认证

很多时候我们可能需要对服务器资源进行保护,通常的做法是在应用层通过鉴权来实现,如果你嫌自己去实现鉴权太麻烦,那就直接让Apache去帮你实现吧!
Apache常见的用户认证可以分为下面三种:
- 基于IP,子网的访问控制(ACL)
- 基本用户验证(Basic Authentication)
- 消息摘要式身份验证(Digest Authentication)

基于IP的访问控制可以通过配置 Allow From实现!这里不多讲。
一般的,我们还会在IP的基础上,再增加一层 Basic Authentication,实现一个基本的服务器用户认证!

1、生成用户名密码文件

/usr/local/apache2/bin/htpasswd -bc users.pwd test hehe1234

Adding password for user test

/usr/local/apache2/bin/htpasswd -b users.pwd test2 hehe4321

Adding password for user test2

cat users.pwd

test:$apr1$4R3foyQ5$1KGHVA5HQL8M9b0K/2UWO0
test2:$apr1$pKLy86CD$W9hFUvs4F06OBXtQhCbPV/

可以看到用户名密码文件已经生成了,一行一个!

2、配置 VirtualHost,如:

<VirtualHost *:80>
    DocumentRoot /usr/local/www/pma/
    DirectoryIndex index.php index.html index.shtml
    ServerName pma.979137.com
    CustomLog "logs/pma.979137.com-access_log" common
    ErrorLog "logs/pma.979137.com-error_log"
    <Directory /usr/local/www/pma/>
        Options Includes FollowSymLinks
        AllowOverride AuthConfig
        AuthName "PMA Contents." 
        AuthType basic
        AuthUserFile /usr/local/apache/conf/users.pwd 
        Require valid-user
    </Directory>
</VirtualHost>
  • AllowOverride 表示通过配置文件进行身份验证
  • AuthName 发送给客户端报文头内容:WWW-Authenticate
  • AuthType 认证类型
  • AuthUserFile 这个就是刚刚生成的用户名密码文件
  • Require 指定哪些用户或组才能被授权访问。如:
    • require user test test2(只有用户 test 和 test2 可以访问)
    • requires groups managers (只有组 managers 中成员可以访问)
    • require valid-user (在 AuthUserFile 指定的文件中任何用户都可以访问)

我们来看一下效果:

在浏览器访问:

cURL请求:

curl -v http://pma.979137.com/test.php

* Trying 10.223.28.1...
* Connected to pma.979137.com (10.223.28.1) port 80 (#0)
> GET /test.php HTTP/1.1
> Host: pma.979137.com
> User-Agent: curl/7.43.0
> Accept: */*>
< HTTP/1.1 401 Authorization Required < Date: Fri, 06 Jan 2017 07:02:15 GMT < Server: Apache/2.2.27 (Unix) PHP/5.3.29 < WWW-Authenticate: Basic realm=" PMA Contents." < Content-Length: 490 < Content-Type: text/html; charset=iso-8859-1 < >401 Authorization Required
>Authorization Required
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.

在没有携带用户名和密码时,HTTP Code 返回了 401,并输出了 Authorization Required!
表示需要该请求需要进行认证!

我们再来看下,携带密码请求:

curl -v -u 'test:hehe1234' 'http://pma.979137.com/test.php'

* Trying 10.223.28.1...
* Connected to pma.979137.com (10.223.28.1) port 80 (#0)
* Server auth using Basic with user 'test'
> GET /test.php HTTP/1.1
> Host: pma.979137.com
> Authorization: Basic c2hpbGlhbmd4aWU6YWl5aTEzMTQ=
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK < Date: Fri, 06 Jan 2017 07:14:14 GMT < Server: Apache/2.2.27 (Unix) PHP/5.3.29 < X-Powered-By: PHP/5.3.29 < Content-Length: 25 < Content-Type: text/html < string(11) "hello word!"

HTTP Code 已经是 200 了,并且返回了正确的内容!
至此,一个简单的 Basic 认证就OK了!这种认证一般可用于浏览器访问,也可以用于 API 认证!

秒懂系列|Apache和NGINX如何按天分割日志

默认情况下ApacheNGINX等web服务器,是没有帮我们按天分日志的,而是把所有日志放到一个文件,当流量大而日志多的时候,管理起来非常方便,一方面是内容多不易查找定位问题,另一方面文件也会越来越大,无效内容越来越多。
常规做法是把日志按天分割,网上很多通过 写脚本+定时任务 来做的,这其实有点多余,而且不好维护,Linux本身自带了很多日志处理程序,比如:logrotate ,它可以实现日志的自动滚动,日志归档等功能。
下面我们介绍使用 logrotate 实现
- Apache按天分割日志
- Nginx按天分割日志

Apache

Apache自身即成了 logrotate,一般在 /usr/local/apache2/bin/rotatelogs
先找到你的 rotatelogs 路径

locate rotatelogs

/usr/local/apache2/bin/rotatelogs

在你的 VirtualHost 模块添加或替换

<VirtualHost *:80>
    ......
    ErrorLog "| /usr/local/apache2/bin/rotatelogs /data/logs/apache/979137.com-error_log-%Y%m%d 86400 480"
    CustomLog "| /usr/local/apache2/bin/rotatelogs /data/logs/apache/979137.com-access_log-%Y%m%d 86400 480" common
</VirtualHost>

指定分割时间:86400 默认单位为s。也就是24小时
指定分区时差:480 默认单位m,也就是8小时

只保留30天日志,自动清理脚本:

#!/bin/bash

LOG_PATH_APACHE="/data/logs/apache/"
DAYS_AGO=date -d "-30 day" +%Y%m%d
\rm -rf ${LOG_PATH_APACHE}*log-${DAYS_AGO}

加入到crontab,每天执行一次即可

0 4 * * * /bin/bash /data/cron/clear_logs.sh > /dev/null 2&>1

NGINX

假设:
- 日志在 /data/logs/nginx/ 下面
- Nginx 安装在 /usr/local/nginx/

1、在 /etc/logrotate.d 目录下创建一个 Nginx 的配置文件 Nginx 配置内容如下

/data/logs/nginx/xxx.qq.com-access_log /data/logs/nginx/xxx.qq.com-error_log {
    su nobody nobody
    daily
    rotate 30
    notifempty
    sharedscripts
    postrotate
    if [ -f /usr/local/nginx/logs/nginx.pid ]; then
        /bin/kill -USR1 /bin/cat /usr/local/nginx/logs/nginx.pid
    fi
    endscript
}

配置解释:
1)配置需要分割的日志文件,也可以用 * 代替
2)su nobody nobody :一般我们的日志目录是允许所有用户进行写操作的,logrotate 认为这不是不安全的,这时 logrotate 会报错如下:

error: skipping "/data/logs/nginx/cafe.qq.com-error_log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.

所以我们需要在配置里使用 su 切换身份执行
3)daily :日志文件每天进行滚动
4)rotate 30 :保留30天的日志
5)notifempty:日志文件为空不进行滚动
6)sharedscripts :运行postrotate脚本
9)/bin/kill -USR1 \/bin/cat /usr/local/nginx/logs/nginx.pid\ :Nginx平滑重启,也即让Nginx重新生成文件

2、执行logrotate

/usr/sbin/logrotate -f /etc/logrotate.d/nginx

如此,Nginx就会自动的按天产生日志了!

微信公众号:程序员到架构师

最新文章

Return Top