httpd2.2配置文件详解/虚拟主机配置等 (二)

2023年 7月 15日 28.6k 0

httpd2.2   (二)

1,定义默认主页面[root@192 ~]# vim /etc/httpd/conf/httpd.confDirectoryIndex index.html index.html.var 如果你需要改为别的页面,在这里定义即可,这里的顺序也重要,从左到右依次匹配。

2,日志错误日志

ErrorLog logs/error_loghttpd路径下的logs是一个软连接,指向/var/log/httpd/lrwxrwxrwx. 1 root root   19 Oct 30 07:19 logs -> ../../var/log/httpd

访问日志CustolLog logs/access_log combined

LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined  #LogFormat后面的跟的是宏LogFormat "%h %l %u %t "%r" %>s %b" commonLogFormat "%{Referer}i -> %U" refererLogFormat "%{User-agent}i" agent

%H:Remote host 远程主机%L: 客户端用户通过Identd登录时使用的名称,一般用户名应该为空-%u: 认证登录的名字,无登录机制一般为-表示为空%t:收到客户端请求时的时间”:显示引号本身,而不作为引号符号%r:请求报文的首行%>s: 响应状态码%b: 响应报文的大小,单位为字节,不包含首部信息%{Referer}i: 记录http首部Referer对应的值:即访问入口,从哪个页面跳转至此页面%{User-Agent}i: 记录http首部User-Agent对应的值,即浏览器类型

详情点击官方文档参考:http://httpd.apache.org/docs/2.2/mod/mod_log_config.html

3,路径别名DocumentRoot "/var/www/html"

比如说,我们在/var/www/html/下建一个images文件夹,文件夹里存放1.html,写入1.html,当我们访问ip/1.html的时候,应该访问的是/var/www/html/1.htmlAlias为:Alias /icons/ "/var/www/icons/"

我们在这里指定一个新的路径,这个路径并不存在Alias /images/ "/web/pics/"Alias /icons/ "/var/www/icons/"Alias /images/ "/web/pics/"此时,我们使用alias的images到/web/pics/,在进行访问就找不到了。

 

 

当我们打开日志进行查看,提示这个找不到这个问题。因为这个路径根本就不存在[root@192 images]# cat /var/log/httpd/error_log  [Sat Oct 31 07:44:20 2015] [error] [client 192.168.233.1] File does not exist: /web[Sat Oct 31 07:44:20 2015] [error] [client 192.168.233.1] File does not exist: /web[Sat Oct 31 07:44:20 2015] [error] [client 192.168.233.1] File does not exist: /web我们创建这个文件并且复制这个文件[root@192 html]# mkdir -pv /web/pics/ && cp /var/www/html/images/1.html  /web/pics/

复制完成后,访问的页面表示Alias 定义的/web/pics/路径下的1.html了

4,设定默认字符集AddDefaultCharset UTF-8#常用字符集:GBK,GB2312,GB18030

5,基于用户的访问控制质询:WWW-Authenticate:服务器用401状态拒绝客户端请求,说明需要用户提供用户名和密码,弹出对话框认证:Authorization:客户端用户填入账号密码后再次发请求至服务器:认证通过,则请求授权

需要用户认证后才能访问的安全域5.1,安全域名称,提供认证原因说明http协议支持的认证方式:basic和digest,而basic是基于base64编码,basic机制,basic是基本的,目前使用较多,digest浏览器支持的不是很好

5.2示例:创建安全域页面路径[root@192 html]# mkdir linux创建页面文件[root@192 html]# echo tom 11 jreey 123 >> linux/index.html修改配置文件,添加配置[root@192 ~]# vim /etc/httpd/conf/httpd.conf <Directory "/var/www/html/linux">        Options None         # none表示不启用任何属性        AllowOverride None       #是否允许覆盖        AuthType Basic            #认证类型        AuthName "This My Home,Pls Dot Move" #认证领域的名字        AuthUserFile /etc/httpd/users/.htpasswd #这里的用户是虚拟用户,非系统用户        Require User tom,jerry      #要求谁能登录,如果有多个用户,使用空格分隔(Require Valid-user  #所有合法的账号)    </Directory>

htpasswd命令用户维护此文件htpasswd命令的使用参数: -c:添加第一个用户时创建此文件 -m:以md5格式加密用户密码存放 -s:以sha格式加密用户密码存放 -D:删除指定用户

开始创建用户[root@192 html]# mkdir /etc/httpd/users#第一次使用-c -m[root@192 html]# htpasswd -c -m /etc/httpd/users/.htpasswd tomNew password: Re-type new password: Adding password for user tom#第二次添加用户则只需要-m即可[root@192 html]# htpasswd -m /etc/httpd/users/.htpasswd jerryNew password: Re-type new password: Adding password for user jerry[root@192 html]#

删除之需要-D指明文件路径下的文件中的用户名即可

组认证:[root@192 html]# vim /etc/httpd/conf/httpd.conf <Directory "/var/www/html/linux">        Options None        AllowOverride None        AuthType Basic        AuthName "This My Home,Pls Dot Move"        AuthUserFile /etc/httpd/users/.htpasswd        AuthUserFile /etc/httpd/users/.htgroup        Require group work   #定义一个组,组名叫work    </Directory>组文件定义: 每行定义一个组  格式为:Grp_Name:User1 User2 ...

[root@192 html]# echo work: tom jerry > /etc/httpd/users/.htgroup

6,虚拟主机一个屋里器可以服务于多个站点,每个站点可通过一个或多个虚拟主机来实现httpd三种类型的虚拟主机:1,基于IP2,基于PORT3,基于FQDN

注意:在定义之前需要关闭Main server[root@192 html]# vim /etc/httpd/conf/httpd.conf#DocumentRoot "/var/www/html"  注释掉

定义虚拟主机的方法:<virtualHost "IP:PORT"> ServerName ServerAlias DocumentRoot</VirtualHost>

注意:大多数可用全局或main server中的指令,都可以定义有VirtualHost中

第一种,基于端口的虚拟主机[root@192 html]# vim /etc/httpd/conf/httpd.confListen 80Listen 8080

<VirtualHost *:80>        ServerName www.1.com        DocumentRoot /vhosts/1.com/htdocs/</VirtualHost><VirtualHost *:8080>        ServerName www.2.com        DocumentRoot /vhosts/2.com/htdocs/</VirtualHost>

[root@192 html]# mkdir -p /vhosts/{1.com,2.com}/htdocs[root@192 html]# echo www.1.com > /vhosts/1.com/htdocs/index.html[root@192 html]# echo www.2.com > /vhosts/2.com/htdocs/index.html[root@192 html]# echo 192.168.233.138 www.2.com > /etc/hosts[root@192 html]# echo 192.168.233.138 www.1.com >> /etc/hosts[root@192 html]# curl www.1.comwww.1.com[root@192 html]# curl www.2.com:8080www.2.com[root@192 html]#

如果你要在浏览器中显示,可能需要修改host文件windows文件路径:C:WindowsSystem32driversetc

第二种:基于IP的虚拟主机注意:基于IP则需IP[root@192 html]# ifconfig eth1:0 192.168.233.139/24 up[root@192 html]# vim /etc/httpd/conf/httpd.conf#Listen 8080 注释掉,因为使用基于IP的<VirtualHost 192.168.233.138:80>        ServerName www.1.com        DocumentRoot /vhosts/1.com/htdocs/</VirtualHost><VirtualHost 192.168.233.139:80>        ServerName www.2.com        DocumentRoot /vhosts/2.com/htdocs/</VirtualHost>

[root@192 html]# service httpd restart

提示:这里也可以开启Listen 8080.<VirtualHost 192.168.233.139:8080>即可,可复用,如下:[root@192 html]# vim /etc/httpd/conf/httpd.confListen 80Listen 8080<VirtualHost 192.168.233.138:8080>        ServerName www.3.com        DocumentRoot /vhosts/3.com/htdocs/</VirtualHost>

[root@192 html]# mkdir /vhosts/3.com/htdocs -p[root@192 html]# echo www.3.com > /vhosts/3.com/htdocs/index.html[root@192 html]# service httpd restart现在便可以访问三个站点

 

第三种:基于FQDN[root@192 html]# vim /etc/httpd/conf/httpd.confNameVirtualHost 192.168.233.138:80  #这项需要开启,指定这些主机是通过那台机器进行访问,2.4则不需要<VirtualHost 192.168.233.138:80>        ServerName www.1.com        DocumentRoot /vhosts/1.com/htdocs/</VirtualHost><VirtualHost 192.168.233.138:80>        ServerName www.2.com        DocumentRoot /vhosts/2.com/htdocs/</VirtualHost><VirtualHost 192.168.233.138:8080>        ServerName www.3.com        DocumentRoot /vhosts/3.com/htdocs/</VirtualHost>  注意,这里测试时,如果是windows需要改host文件[root@192 html]# httpd -tSyntax OK[root@192 html]# service httpd restartStopping httpd:                                            [  OK  ]Starting httpd:                                            [  OK  ][root@192 html]#

 注意:额外经常用于每个虚拟主机的配置有以下:ErrorLog,CustomLog,<Directory>,<Location>,ServerAlias

 

7,内置的status页面<Location /server-status>    SetHandler server-status    Order deny,allow    Deny from all    Allow from 192.168.233.0/24改成IP地址段或域名即可</Location>

这个页面显示的运行时间,可可用进程(。。。。)模型不同,显示各不相同

——:空闲进程S:正在启动R:正在读取请求W:发送响应报文K:持久链接读D:DNS查询C:正在关闭链接L:正在记录日志g:正在关闭i:空间的清理工作进程-

ExtendedStatus On可显示更详细信息,但是这个可能会消耗系统资源!

相关文章

10分钟搞定支持通配符的永久有效免费HTTPS证书
300 多个 Microsoft Excel 快捷方式
一步步配置基于kubeadmin的kubevip高可用
istio全链路传递cookie和header灰度
REST Web 服务版本控制
2023 年最适合 Windows 11 使用的 20 个应用

发布评论