linux 架构相关知识(二)

一、apache虚拟主机

       一台服务器可以访问多个网站,每个网站都是一个虚拟主机 概念:域名(主机名)、DNS、解析域名、hosts 任何一个域名解析到这台机器,都可以访问的虚拟主机就是默认虚拟主机。

1. windows下host用法

第一步:按下图找到host文件

第二步:按下图添加域名;

第三步:ping 设置的域名是否通畅;

Microsoft Windows [版本 10.0.17134.112]

(c) 2018 Microsoft Corporation。保留所有权利。

C:\Users\zqsgq>www.abc.com

'www.abc.com' 不是内部或外部命令,也不是可运行的程序

或批处理文件。

C:\Users\zqsgq>ping www.abc.com

正在 Ping www.abc.com [192.168.112.136] 具有 32 字节的数据:

来自 192.168.112.136 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.112.136 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.112.136 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.112.136 的回复: 字节=32 时间<1ms TTL=64

192.168.112.136 的 Ping 统计信息:

数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),

往返行程的估计时间(以毫秒为单位):

最短 = 0ms,最长 = 0ms,平均 = 0ms

C:\Users\zqsgq>ping test.com

正在 Ping www.abc.com [192.168.112.136] 具有 32 字节的数据:

来自 192.168.112.136 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.112.136 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.112.136 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.112.136 的回复: 字节=32 时间<1ms TTL=64

192.168.112.136 的 Ping 统计信息:

数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),

往返行程的估计时间(以毫秒为单位):

最短 = 0ms,最长 = 0ms,平均 = 0ms

C:\Users\zqsgq>ping ying.com

正在 Ping www.abc.com [192.168.112.136] 具有 32 字节的数据:

来自 192.168.112.136 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.112.136 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.112.136 的回复: 字节=32 时间=1ms TTL=64

来自 192.168.112.136 的回复: 字节=32 时间<1ms TTL=64

192.168.112.136 的 Ping 统计信息:

数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),

往返行程的估计时间(以毫秒为单位):

最短 = 0ms,最长 = 1ms,平均 = 0ms

C:\Users\zqsgq>

第四步:在浏览器访问域名

2. linux下配置虚拟主机

第一步:编辑配置文件httpd.conf,去掉以下的#号,使虚拟主机的配置文件生效

# Virtual hosts

Include conf/extra/httpd-vhosts.conf //去掉#号 加载虚拟主机的配置文件

第二步:进入虚拟主机配置文件中;

以下内容需要更改

<VirtualHost *:80>

ServerAdmin[email protected] //定义管理员的邮箱

DocumentRoot "/usr/local/aprche2.4/docs/dummy-host.example.com"

ServerNamedummy-host.example.com

ServerAliaswww.dummy-host.example.com

ErrorLog "logs/dummy-host.example.com-error_log"

CustomLog "logs/dummy-host.example.com-access_log" common

</VirtualHost>

<VirtualHost *:80>

ServerAdmin[email protected]

DocumentRoot "/usr/local/aprche2.4/docs/dummy-host2.example.com"

ServerNamedummy-host2.example.com

ErrorLog "logs/dummy-host2.example.com-error_log"

CustomLog "logs/dummy-host2.example.com-access_log" common

</VirtualHost>

更改为以下内容:

<VirtualHost *:80>

DocumentRoot "/data/wwwroot/abc.com" //定义网站的根目录

ServerNameabc.com //定义域名,只能写1个

ServerAliaswww.abc.comwww.123.com //定义别名,可以写多个

ErrorLog "logs/abc.com-error_log" //定义错误日志

CustomLog "logs/abc.com-access_log" common //定义访问日志

</VirtualHost>

<VirtualHost *:80>

DocumentRoot "/data/wwwroot/111.com"

ServerName111.com

ServerAliaswww.example.com

ErrorLog "logs/111.com-error_log"

CustomLog "logs/111.com-access_log" common

</VirtualHost>

第三步: 创建和虚拟主机对应的站点根目录,以及新建相应index.php

[root@ying01 php-7.1.6]# mkdir /data/wwwroot/[root@ying01 php-7.1.6]# mkdir /data/wwwroot/abc.com[root@ying01 php-7.1.6]# mkdir /data/wwwroot/111.com[root@ying01 php-7.1.6]# vim /data/wwwroot/abc.com/index.php[root@ying01 php-7.1.6]# vim /data/wwwroot/111.com/index.php[root@ying01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl -t //需要检查配置语法[root@ying01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl graceful //需要加载

第四步:ping 设置的www.abc.com,此时明显是外网的域名;

[root@ying01 wwwroot]# ping www.abc.com

PING abc.com (199.181.132.250) 56(84) bytes of data.

64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=1 ttl=128 time=176 ms

64 bytes from 199.181.132.250 (199.181.132.250): icmp_seq=2 ttl=128 time=160 ms

^C

--- abc.com ping statistics ---

3 packets transmitted, 2 received, 33% packet loss, time 8490ms

rtt min/avg/max/mdev = 160.519/168.407/176.296/7.899 ms

第五步:用curl -x192.168.112.136:80 abc.com 来测试

[root@ying01 wwwroot]# curl -x192.168.112.136:80 abc.com

abc.com[root@ying01 wwwroot]# curl -x192.168.112.136:80 www.abc.com //指向abc.com

abc.com[root@ying01 wwwroot]# curl -x192.168.112.136:80 www.qq.com //qq都指向abc.com

abc.com[root@ying01 wwwroot]# curl -x192.168.112.136:80 www.126.com

abc.com[root@ying01 wwwroot]# curl -x192.168.112.136:80 www.example.com //这个别名,指向111.com111.com[root@ying01 wwwroot]# curl -x192.168.112.136:80 www.111.com //www.111.com因为不是设置的所有指向abc.com

abc.com[root@ying01 wwwroot]# curl -x192.168.112.136:80 111.com

二、Apache 用户认证

1. 全局用户认证

在虚拟主机配置文件 httpd-vhosts.conf ,配置另一台虚拟主机信息;

[root@ying01 ~]# 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>

  • 创建密码htpasswd命令

在创建密码文件先要了解htpasswd命令: htpasswd命令是Apache的Web服务器内置工具,用于创建和更新储存用户名、域和用户基本认证的密码文件。

[root@ying01 ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd ying

New password:

Re-type new password:

Adding password for user ying

[root@ying01 ~]# cat /data/.htpasswd ying:$apr1$XFA4gjrQ$z7d4/5h81llfs5jb5i/Ot.

再增加一个用户,就不用再添加-c;c是creat,创建的意思;

[root@ying01 ~]# /usr/local/apache2.4/bin/htpasswd -m /data/.htpasswd feng

New password:

Re-type new password:

Adding password for user feng

[root@ying01 ~]# !cat

cat /data/.htpasswd

ying:$apr1$XFA4gjrQ$z7d4/5h81llfs5jb5i/Ot.

feng:$apr1$9JUoqWLH$MwA8PpAq.9ZtqHCjh9Qod/

注意:如果继续用 -c ,那么就会把之前的用户名覆盖;

连接测试此虚拟,

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl -t //更改配置后,需要检查配置语法

Syntax OK

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl graceful //需要加载

[root@ying01 ~]# curl -x192.168.112.136:80 111.com

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>401 Unauthorized</title> //401错误,

</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>

curl命令的 -I 参数,显示 http response 的头信息

[root@ying01 ~]# curl -x192.168.112.136:80 111.com -I

HTTP/1.1 401 Unauthorized

Date: Thu, 28 Jun 2018 09:37:57 GMTServer: Apache/2.4.33 (Unix) PHP/7.1.6WWW-Authenticate: Basic realm="111.com user auth" //需要你的密码Content-Type: text/html; charset=iso-8859-1

curl命令的 -u 参数,可以登录用户名,及密码;

[root@ying01 ~]# curl -x192.168.112.136:80 -uying:www123 111.com -I //ying是之前的用户名,www123是密码

HTTP/1.1 200 OK //状态码200,登录成功

Date: Thu, 28 Jun 2018 09:39:26 GMTServer: Apache/2.4.33 (Unix) PHP/7.1.6X-Powered-By: PHP/7.1.6Content-Type: text/html; charset=UTF-8

故意输错密码,就会报错;

[root@ying01 ~]# curl -x192.168.112.136:80 -uying:www1234 111.com -I //密码输错

HTTP/1.1 401 Unauthorized //状态码401,报错,未经授权

Date: Thu, 28 Jun 2018 09:39:45 GMT

Server: Apache/2.4.33 (Unix) PHP/7.1.6

WWW-Authenticate: Basic realm="111.com user auth" //提示,身份验证

Content-Type: text/html; charset=iso-8859-1

  • 我们在windows上的浏览器,查看该域名 111.com访问情况;

第一步:在hosts文件中,按下图添加 ying.com;

第二步:此时显示如下页面,提示:输入用户名及密码

第三步:输入之前我们定义的用户名,及密码,可以访问成功;

2. 单个文件进行认证

在虚拟主机配置文件 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> //匹配 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>

更改配置后,先测试111.com

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl -t //更改配置后,需要检查配置语法

Syntax OK

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl graceful //需要加载

[root@ying01 ~]# curl -x192.168.112.136:80 111.com -I //先测试111.com,访问成功

HTTP/1.1 200 OK

Date: Fri, 29 Jun 2018 01:40:37 GMTServer: Apache/2.4.33 (Unix) PHP/7.1.6X-Powered-By: PHP/7.1.6Content-Type: text/html; charset=UTF-8

111.com目录下,新建123.php,然后测试;

[root@ying01 ~]# vim /data/wwwroot/111.com/123.php //新建123.php

[root@ying01 ~]# curl -x192.168.112.136:80 111.com/123.php -I

HTTP/1.1 401 Unauthorized //401错误,需要用户认证

Date: Fri, 29 Jun 2018 01:41:03 GMT

Server: Apache/2.4.33 (Unix) PHP/7.1.6

WWW-Authenticate: Basic realm="111.com user auth"

Content-Type: text/html; charset=iso-8859-1

用-u参数,把用户名 ying,密码 www123 带上,在测试123.php

[root@ying01 ~]# curl -x192.168.112.136:80 -uying:www123 111.com/123.php -I

HTTP/1.1 200 OK //200状态码,成功

Date: Fri, 29 Jun 2018 01:41:43 GMTServer: Apache/2.4.33 (Unix) PHP/7.1.6X-Powered-By: PHP/7.1.6Content-Type: text/html; charset=UTF-8

三、域名跳转

       域名跳转类似于将网页重新指向另一个网站,但区别是域名跳转会将域名本身重新指向网站,而不使用HTML或脚本来进行重新指向。当域名被设置为跳转至另一网站,域名的地址将不会保留在浏览器的URL栏中,该栏显示的会是新页面的URL。如果您希望保留该栏中的URL,则需要使用隐形跳转。

配置域名跳转vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

[root@ying01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

以下为配置文件代码

......

<VirtualHost *:80>

DocumentRoot "/data/wwwroot/111.com"

ServerName 111.com

ServerAlias www.example.com 2111.com.cn

#<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

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@ying01 ~]# ls /data/wwwroot/111.com/123.php index.php

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl graceful

修改配置文件/httpd.conf ,把rewrite模块 加载上

[root@ying01 ~]# vim /usr/local/apache2.4/conf/httpd.conf

以下其配置内容,其余省略显示.....

LoadModule rewrite_module modules/mod_rewrite.so //把#取消,让其加载#LoadModule php5_module modules/libphp5.so

LoadModule php7_module modules/libphp7.so

.....

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl graceful

查看网络链接, :::80 代表全网都支持跳转;

[root@ying01 ~]# netstat -ntpl

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 672/sshd

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 800/master

tcp6 0 0 :::80 :::* LISTEN 1369/httpd

tcp6 0 0 :::22 :::* LISTEN 672/sshd

tcp6 0 0 ::1:25 :::* LISTEN 800/master

[root@ying01 ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.112.136 netmask 255.255.255.0 broadcast 192.168.112.255

inet6 fe80::16dc:89c:b761:e115 prefixlen 64 scopeid 0x20<link>

ether 00:0c:29:87:3f:91 txqueuelen 1000 (Ethernet)

RX packets 4105 bytes 335215 (327.3 KiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 1891 bytes 270591 (264.2 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.112.158 netmask 255.255.255.0 broadcast 192.168.112.255

ether 00:0c:29:87:3f:91 txqueuelen 1000 (Ethernet)

现在用另一个网卡,ens33:0 192.168.112.158

[root@ying01 ~]# curl -x192.168.112.158:80 111.com -I

HTTP/1.1 200 OK //200码。存在

Date: Fri, 29 Jun 2018 02:55:33 GMT

Server: Apache/2.4.33 (Unix) PHP/7.1.6

X-Powered-By: PHP/7.1.6

Content-Type: text/html; charset=UTF-8

[root@ying01 ~]# curl -x192.168.112.158:80 2111.com.cn -I

HTTP/1.1 301 Moved Permanently //301码,跳转

Date: Fri, 29 Jun 2018 02:55:49 GMT

Server: Apache/2.4.33 (Unix) PHP/7.1.6

Location: http://111.com/

Content-Type: text/html; charset=iso-8859-1

[root@ying01 ~]# curl -x192.168.112.158: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> //跳转到111.com

</body></html>

2111.com下面,不存在的域名,只是显示跳转

[root@ying01 ~]# curl -x192.168.112.158:80 2111.com.cn/qqqaaa/ssss -I

HTTP/1.1 301 Moved Permanently

Date: Fri, 29 Jun 2018 02:59:15 GMTServer: Apache/2.4.33 (Unix) PHP/7.1.6Location: http://111.com/qqqaaa/ssssContent-Type: text/html; charset=iso-8859-1

真实存在的域名,会显示200状态码;

[root@ying01 ~]# curl -x192.168.112.158:80 111.com/123.php -I

HTTP/1.1 200 OK

Date: Fri, 29 Jun 2018 03:03:51 GMTServer: Apache/2.4.33 (Unix) PHP/7.1.6X-Powered-By: PHP/7.1.6Content-Type: text/html; charset=UTF-8

四、 Apache访问日志

1. Apache访问日志所在位置

打开日志的目录,/usr/local/apache2.4/logs/

[root@ying01 ~]# 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

[root@ying01 ~]# ls /usr/local/apache2.4/logs/111.com-access_log

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

[root@ying01 ~]# cat !$ |tail -5

cat /usr/local/apache2.4/logs/111.com-access_log |tail -5192.168.112.136 - - [28/Jun/2018:10:55:49 +0800] "HEAD HTTP://2111.com.cn/ HTTP/1.1" 301 -

192.168.112.136 - - [28/Jun/2018:10:56:30 +0800] "GET HTTP://2111.com.cn/ HTTP/1.1" 301 223192.168.112.136 - - [28/Jun/2018:10:58:30 +0800] "GET HTTP://2111.com.cn/qqqaaa/ssss-I HTTP/1.1" 301 236192.168.112.136 - - [28/Jun/2018:10:59:15 +0800] "HEAD HTTP://2111.com.cn/qqqaaa/ssss HTTP/1.1" 301 -

192.168.112.136 - - [28/Jun/2018:11:03:51 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 -

2. 查看日志格式

打开apache主配置文件,查找日志的格式内容

[root@ying01 ~]# vim /usr/local/apache2.4/conf/httpd.conf

以下httpd.conf的内容,搜索LogFormat

.........

<IfModule log_config_module>

#

# The following directives define some format nicknames for use with

# a CustomLog directive (see below).

#

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined //复杂的格式

LogFormat "%h %l %u %t \"%r\" %>s %b" common //我们之前用户的格式

<IfModule logio_module>

# You need to enable mod_logio.c to use %I and %O

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio

</IfModule>

3. 更改日志格式

再次打开虚拟主机,配置文件: vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>

DocumentRoot "/data/wwwroot/111.com"

ServerName111.com

ServerAliaswww.example.com 2111.com.cn

#<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

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 //把之前的common,改为combined

</VirtualHost>

重新加载虚拟主机配置文件,测试123.php,目的是为了生成新的日志

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl graceful

[root@ying01 ~]# curl -x192.168.112.158:80 111.com/123.php -I

HTTP/1.1 200 OK

Date: Fri, 29 Jun 2018 03:45:42 GMTServer: Apache/2.4.33 (Unix) PHP/7.1.6X-Powered-By: PHP/7.1.6Content-Type: text/html; charset=UTF-8

再次查看新生成的日志;

[root@ying01 ~]# cat /usr/local/apache2.4/logs/111.com-access_log |tail -5192.168.112.136 - - [28/Jun/2018:10:56:30 +0800] "GET HTTP://2111.com.cn/ HTTP/1.1" 301 223

192.168.112.136 - - [28/Jun/2018:10:58:30 +0800] "GET HTTP://2111.com.cn/qqqaaa/ssss-I HTTP/1.1" 301 236

192.168.112.136 - - [28/Jun/2018:10:59:15 +0800] "HEAD HTTP://2111.com.cn/qqqaaa/ssss HTTP/1.1" 301 -

192.168.112.136 - - [28/Jun/2018:11:03:51 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 -

192.168.112.136 - - [28/Jun/2018:11:45:42 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29

五、访问日志不记录静态文件

      很多网站大多为静态网页,网页内部的图片、css文件等同样有其网址链接,如果不设置,这些无效的信息也将被存入访问日志中,会导致访问日志文件大小快速增加,占用大量存储空间。我们可以通过设置不记录某些文件来减少无效信息,节省内存资源。

例如下面网站的的代码 (按F12,会出现)

首先做一个实验,我们先访问 .jpg的文件;无论有无此文件,它都会产生日志;

[root@ying01 ~]# curl -x192.168.112.136:80 111.com/jasas.jpg

<!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 /jasas.jpg was not found on this server.</p></body></html>

查看日志,我们发现,最后一条 .jpg的日志,就是我们刚才访问的记录;

[root@ying01 ~]# cat /usr/local/apache2.4/logs/111.com-access_log |tail -3192.168.112.136 - - [28/Jun/2018:11:03:51 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 -

192.168.112.136 - - [28/Jun/2018:11:45:42 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0"

192.168.112.136 - - [28/Jun/2018:12:09:23 +0800] "GET HTTP://111.com/jasas.jpg HTTP/1.1" 404 207 "-" "curl/7.29.0"

现在配置虚拟主机的配置文件:vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

[root@ying01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

以下为配置文件内容.....

<VirtualHost *:80>

DocumentRoot "/data/wwwroot/111.com"

ServerName 111.com

ServerAlias www.example.com 2111.com.cn

#<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

RewriteCond %{HTTP_HOST} !^111.com$

RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]

</IfModule>

ErrorLog "logs/111.com-error_log"

SetEnvIf Request_URI ".*\.gif$" img

SetEnvIf Request_URI ".*\.jpg$" img

SetEnvIf Request_URI ".*\.png$" img

SetEnvIf Request_URI ".*\.bmp$" img

SetEnvIf Request_URI ".*\.swf$" img

SetEnvIf Request_URI ".*\.js$" img

SetEnvIf Request_URI ".*\.css$" img

CustomLog "logs/111.com-access_log" combined env=!img //不记录关于变量img的请求日志

</VirtualHost>

以上模块释义: 将所有访问图片的请求定义为变量img,在访问记录(日志)中将其排除即可。重新加载后使用curl访问img变量中所指定格式的文件内容时将不会产生访问记录。img是变量。

此时我们再访问类似的日志,看日志是否记录;

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl graceful

[root@ying01 ~]# curl -x192.168.112.136:80 111.com/aaaaa.jpg //访问aaaaa.jpg

<!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 /aaaaa.jpg was not found on this server.</p></body></html>

[root@ying01 ~]# cat /usr/local/apache2.4/logs/111.com-access_log |tail -3

192.168.112.136 - - [28/Jun/2018:11:03:51 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 -

192.168.112.136 - - [28/Jun/2018:11:45:42 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0"

192.168.112.136 - - [28/Jun/2018:12:09:23 +0800] "GET HTTP://111.com/jasas.jpg HTTP/1.1" 404 207 "-" "curl/7.29.0"

从结果看,访问日志不记录,该日志;说明刚才配置成功;

六、访问日志切割

首先还是配置文件,按下面更改 访问日志(CustomLog)的内容

[root@ying01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

以下为配置文件内容.....

<VirtualHost *:80>

DocumentRoot "/data/wwwroot/111.com"

ServerName 111.com

ServerAlias www.example.com 2111.com.cn

#<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

RewriteCond %{HTTP_HOST} !^111.com$

RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]

</IfModule>

ErrorLog "logs/111.com-error_log"

SetEnvIf Request_URI ".*\.gif$" img

SetEnvIf Request_URI ".*\.jpg$" img

SetEnvIf Request_URI ".*\.png$" img

SetEnvIf Request_URI ".*\.bmp$" img

SetEnvIf Request_URI ".*\.swf$" img

SetEnvIf Request_URI ".*\.js$" img

SetEnvIf Request_URI ".*\.css$" img

CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img

</VirtualHost>

CustomLog "|/usr/local/apache2/bin/rotatelogs -l logs/123test-access_log_%Y%m%d_log 86400" combined env=!img

语句释义:

  • /usr/local/apache2.4/bin/rotatelogs工具是apache自带的分割日志的工具

  • -l参数按当前系统时间为基准进行切割(我国为CST),否则默认UTC

  • %Y%m%d表示年月日,这样会每天记录一个带日期的日志文件,更方便

  • 86400(s)表示每天都进行切割,一天24小时等于86400秒

修改完配置,来访问网站,查看它的日志生成情况

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl graceful

[root@ying01 ~]# ls /usr/local/apache2.4/logs/

111.com-access_log 111.com-error_log abc.com-access_logabc.com-error_log access_log error_log httpd.pid

[root@ying01 ~]# curl -x192.168.112.136:80 111.com/123.php //访问123.php一次123.php[root@ying01 ~]#

[root@ying01 ~]# ls /usr/local/apache2.4/logs/ //里面已经有记录了111.com-access_20180629.log 111.com-access_log 111.com-error_log abc.com-access_log abc.com-error_log access_log error_log httpd.pid

[root@ying01 ~]# ls /usr/local/apache2.4/logs/111.com-access_20180629.log

/usr/local/apache2.4/logs/111.com-access_20180629.log

[root@ying01 ~]# cat !$

cat /usr/local/apache2.4/logs/111.com-access_20180629.log //查看此日志

192.168.112.136 - - [28/Jun/2018:12:41:40 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 "-" "curl/7.29.0"

[root@ying01 ~]# !curl

curl -x192.168.112.136:80 111.com/123.php //再访问一次

123.php[root@ying

[root@ying01 ~]# cat /usr/local/apache2.4/logs/111.com-access_20180629.log //再次查看,此时有2个日志192.168.112.136 - - [28/Jun/2018:12:41:40 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 "-" "curl/7.29.0"192.168.112.136 - - [28/Jun/2018:12:44:52 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 "-" "curl/7.29.0"

七、 静态元素过期时间

       浏览器访问网站的图片时会把静态的文件缓存在本地电脑里,这样下次再访问时就不用去远程下载了。 但是缓存多久呢?如果网站图片更新了呢,那么应该访问新图片才是。所以这就涉及到静态文件缓存时长的问题了,也就是“缓存过期时间”。

在主配置文件,去掉#号,启用expires模块

[root@ying01 ~]# vim /usr/local/apache2.4/conf/httpd.conf

以下为配置内容....

LoadModule expires_module modules/mod_expires.so //去掉#,加载模块

在上次虚拟机配置下,增加以下模块

[root@ying01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

以下虚拟主机配置内容......

<IfModule mod_expires.c>

ExpiresActive on //打开该功能的开关

ExpiresByType image/gif "access plus 1 days"

ExpiresByType image/jpeg "access plus 24 hours"

ExpiresByType image/png "access plus 24 hours"

ExpiresByType text/css "now plus 2 hour"

ExpiresByType application/x-javascript "now plus 2 hours"

ExpiresByType application/javascript "now plus 2 hours"

ExpiresByType application/x-shockwave-flash "now plus 2 hours"

ExpiresDefault "now plus 0 min" //除上述外的文件指定默认的过期时间

</IfModule>

名词

释义

acess

表示从访问时间开始

now

表示按当前时间开始

plus

在前面的时间基础上加上

1 hours/days

表示文件的生命周期

那么语句:access plus 24 hours 指该文件从访问的时间开始24小时内有效,无需重新获取

保存配置文件,检查语法错误,并重新加载

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@ying01 ~]# /usr/local/apache2.4/bin/apachectl graceful

猜你喜欢

转载自blog.csdn.net/weixin_40876986/article/details/90053032