1.简介
安装:yum install -y httpd
主程序:/usr/sbin/httpd
httpd2.4支持MPM的动态切换
配置文件:/etc/httpd/conf /etc/httpd/conf.d/*.conf
模块相关的配置文件:/etc/httpd/conf.modules.d/*.conf
模块文件路径:/usr/lib64/httpd/modules
systemd unit file: /usr/lib/systemd/system/httpd.service
访问日志,错误日志,默认目录:/var/log/httpd (access_log error_log)
网页文件目录:/var/www
网页文件默认目录:/var/www/html
例如:
https://mp.csdn.net/index.html
scheme://server[:port][/PATH/TO/SOME_SOURCE]
httpd默认 DocumentRoot: /var/www/html
服务控制:
systemctl start httpd.service
systemctl stop httpd.service
systemctl restart httpd.service
2. httpd特性
CGI:Common Gateway Interface
虚拟主机: IP PORT FQDN
反向代理
负载均衡
路径别名
丰富的用户认证机制
支持第三方模块
...
3.常用配置项
/etc/httpd/conf/httpd.conf
格式: 指令 值
其中,指令不区分大小写,值则有可能区分大小写
通常以新增配置文件的方式修改httpd配置项,解耦,灵活
0.配置文件中的配置块:
<Directory />
AllowOverride none
Require all denied
</Directory>
1.监听端口
Listen [IP-address:]portnumber [protocol]
---地址可省略,端口不能省略
例如:Listen 80
2.长连接
KeepAlive On|Off ------ Enables HTTP persistent connections
KeepAliveTimeout num[ms] ------ Amount of time the server will wait for subsequent requests on a persistent connection
MaxKeepAliveRequests number ------ Number of requests allowed on a persistent connection
3.MPM
httpd -M ----列出所有模块
在以下文件中配置:/etc/httpd/conf.modules.d/00-mpm.conf
mpm_prefork.conf文件内容如下:
<IfModule mpm_prefork_module>
StartServers 10 # 启动时进程数
MinSpareServers 5 # 最小空闲进程数
MaxSpareServers 10 # 最大空闲进程数
MaxRequestWorkers 100 # 最大并发进程数
MaxConnectionsPerChild 10000 # 最大连接数限制
</IfModule>
PV(page view): 一次完整的页面访问,包括该页面的所有资源
UV(user view): 独立的用户浏览量
IP: IP访问量
4.DSO (Dynamically Shared Objects)
所有使用 httpd -M 显示为 (shared) 的模块都可以使用 LoadModule 装载
5.定义Main Server的文档页面路径
ServerName [scheme://]domain-name|ip-address[:port]
ServerAlias
DocumentRoot directory-path
---- 指定 URL PATH 与 File System PATH 的映射关系
6.站点访问控制
可基于两种机制对资源进行访问控制
文件系统路径:
<Directory "">
</Directory>
<File "">
</File>
<FileMatch "PATTERN">
</FileMatch>
URL路径:
<Location "">
</Location>
<LocationMatch "PATTERN">
</LocationMatch>
基于源地址的访问控制:
Require all granted
Require all deneid
基于IP控制:
Require ip 10 172.20 192.168.2
Require not ip 10 172.20 192.168.2
基于host控制:
Require host HOST_NAME
Require not host HOST_NAME
要放置在<RequireAll> 或 <RequireAny> 配置块中
Options:
Indexes
If a URL which maps to a directory is requested, and there is no
DirectoryIndex
(e.g.,index.html
) in that directory, thenmod_autoindex
will return a formatted listing of the directory.如果该目录没有index.html,则可以列出该目录下所有资源文件
FollowSymLinks
The server will follow symbolic links in this directory. This is the default setting.
Even though the server follows the symlink it does not change the pathname used to match against
<Directory>
sections.The
FollowSymLinks
andSymLinksIfOwnerMatch
Options
work only in<Directory>
sections or.htaccess
files.Omitting this option should not be considered a security restriction, since symlink testing is subject to race conditions that make it circumventable.
如果使用该选项,则可以访问连接文件 连接至 其他目录或文件
7.定义站点主页面
DirectoryIndex index.html
8.定义路径别名
Alias /webpath /full/filesystem/path
即 将 /webpath 这个URL重定向至 文件系统的/full/filesystem/path
注意:别名的路径 /full/filesystem/path 也需要显式授权 (Require all granted)
9.设定默认字符集
AddDefaultCharset UTF-8
10.日志设定
ErrorLog "logs/error_log"
CustomLog "logs/access_log" combined
----相对于ServerRoot的路径 ( ServerRoot "/etc/httpd"),而该目录的logs是个软链接:
[root@localhost ~]# ll /etc/httpd/logs
lrwxrwxrwx. 1 root root 19 Dec 21 21:59 /etc/httpd/logs -> ../../var/log/httpd
所以http的log都放在/var/log/httpd下
log的格式由 LogFormat 定义
Common Log Format (CLF)
"%h %l %u %t \"%r\" %>s %b"
Common Log Format with Virtual Host
"%v %h %l %u %t \"%r\" %>s %b"
NCSA extended/combined log format
"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
各种字符的定义:http://httpd.apache.org/docs/2.4/mod/mod_log_config.html
11.基于用户的访问控制
认证质询:
www-authenticate:响应码401,拒绝客户端请求,要求账号密码
认证:
Authorization:客户端输入账号密码后再次发送请求报文,认证通过后,服务器发送响应资源
认证有两种:
basic:明文
digest:消息摘要认证
配置示例:
<Directory "/data/www">
Options None
AllowOverride None
AuthType basic
AuthName "Hello Please Auth"
AuthUserFile "/etc/httpd/conf.d/myAuth.users"
Require user hss tom
#Require valid-user
#AuthGroupFile "/path/to/groupfile"
#Require group groupname
</Directory>
认证文件可使用htpasswd
htpasswd [options] /path/to/authfile username
-c 第一次创建文件时使用,如果文件已存在,则会覆盖
-m md5格式加密
-s sha格式加密
-D 删除指定用户
-b 批量添加用户 htpasswd -b [options] /path/to/authfile username password
12.虚拟主机
基于IP: ---- 为每个主机提供至少一个IP
基于PORT: ---- 为每个主机提供至少一个PORT
基于FQDN: ---- 为每个主机提供至少一个FQDN,根据请求报文中的host字段路由到不同的虚拟主机
配置示例:
<VirtualHost 192.168.75.10:80> #地址可以写成*:80
ServerName www.a.com
DocumentRoot "/data/www/a_com"
<Directory "/data/www/a_com">
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.75.10:80>
ServerName www.b.com
DocumentRoot "/data/www/b_com"
<Directory "/data/www/b_com">
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
13.status页面
配置示例:
<Location /url/path/to/status>
SetHandler server-status
Require all granted #注意,此处可做认证
</Location>
URL:
基本语法:
<scheme>://[<user>[:<password>]@]<host>:<port>/<path>[;<params>][?<query>][#<frag>]
params: 参数
http://www.baidu.com/bbs/hello;gender=f
query:
http://www.baidu.com/bbs/item.php?username=tom&title=abc
frag:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html-single/Installation_Guide/index.html#ch-Boot-x86
14.curl命令
curl [options] [URL...]
curl的常用选项:
-A/--user-agent <string> 设置用户代理发送给服务器
-e/--referer <URL> 来源网址
--compressed 要求返回是压缩的格式
-I/--head 只显示响应报文首部信息
--basic 使用HTTP基本认证
--tcp-nodelay 使用TCP_NODELAY选项
--cacert <file> CA证书 (SSL)
-H/--header <line>自定义首部信息传递给服务器
--limit-rate <rate> 设置传输速度
-u/--user <user[:password]>设置服务器的用户和密码
-0/--http1.0 使用HTTP 1.0
15.user/group
指定以哪个用户的身份运行httpd服务进程;
User apache
Group apache
16、使用mod_deflate模块压缩页面优化传输速度
该操作节约带宽,但是消耗更多CPU
SetOutputFilter DEFLATE
# mod_deflate configuration
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css