httpd主配置文件的默认配置:
[root@KOU CA]# grep -v “#” /etc/httpd/conf/httpd.conf | grep -v “^$”
ServerRoot "/etc/httpd" Listen 80 Include conf.modules.d/*.conf User apache Group apache ServerAdmin root@localhost <Directory /> AllowOverride none Require all denied </Directory> DocumentRoot "/var/www/html" <Directory "/var/www"> AllowOverride None Require all granted </Directory> <Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> <IfModule dir_module> DirectoryIndex index.html </IfModule> <Files ".ht*"> Require all denied </Files> ErrorLog "logs/error_log" LogLevel warn <IfModule log_config_module> 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> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio </IfModule> CustomLog "logs/access_log" combined </IfModule> <IfModule alias_module> ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" </IfModule> <Directory "/var/www/cgi-bin"> AllowOverride None Options None Require all granted </Directory> <IfModule mime_module> TypesConfig /etc/mime.types AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType text/html .shtml AddOutputFilter INCLUDES .shtml </IfModule> AddDefaultCharset UTF-8 <IfModule mime_magic_module> MIMEMagicFile conf/magic </IfModule> EnableSendfile on IncludeOptional conf.d/*.conf
1、修改监听的IP和PORT;
格式:Listen [IP-address:] portnumber [protocol]
1>省略IP,表示监听本机上的所有IP:0.0.0.0;
2>Listen指令可以重复出现多次:
Listen 80
Listen 8000
Listen 192.170.2.1:80
Listen 192.170.2.5:8000
3>如果是使用SSL,则https必须标注;
Listen 192.170.2.1:443 https
Listen 192.170.2.1:8443 https
2、user/group
User apache
Group apache
指定以哪个用户的身份运行httpd服务进程;
3、持久连接(保持连接、长连接):Persistent Connection;
tcp连接建立后,每个资源获取完成后不全断开连接,而是继续等待其他资源的请求;
缺点:对并发访问量较大的服务器,长连接机制会使后续某些请求无法得到正常响应;
解决方案:用连接数的数量限制、连接时间限制来断开持久连接;
KeepAlive on | Off
KeepAliveTimeout 15
MaxKeepAliveRequests 100
KeepAliveTimeout num[ms] :超时时间单位默认是”秒”;httpd-2.4支持毫秒,数值后面加’ms’,表示毫秒;
测试方法(使用telnet):
~]# telnet WEB_SERVER_IP PORT 回车后输入如下信息:
GET /URL HTTP/1.1
Host:WEB_SERVER_IP
举例测试:
~]# vim /etc/httpd/conf.d/keepalive.conf
KeepAlive on
KeepAliveTimeout 10
MaxKeepAliveRequests 100
[root@KOU ~]# telnet 192.168.206.88 80
Trying 192.168.206.88... Connected to 192.168.206.88. Escape character is '^]'. GET /index.html HTTP/1.1 Host:192.168.206.88 HTTP/1.1 404 Not Found Date: Fri, 01 Jun 2018 05:37:03 GMT Server: Apache/2.4.6 (CentOS) Content-Length: 208 Content-Type: text/html; charset=iso-8859-1 <!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 /index.html was not found on this server.</p> </body></html> Connection closed by foreign host. 10秒后服务器会自动断开长连接; [root@KOU ~]#
4、MPM
httpd 2.2版本:
———————
1> 不支持同时编译多个MPM模块;
2> centos 6的rpm包提供了3个应用程序文件:httpd(prefork),httpd.worker,httpd.event;分别用于实现对不同的MPM机制的支持;
3> 更换使用其他MPM机制:编辑文件”/etc/sysconfig/httpd”,启用”HTTPD=/usr/sbin/httpd.worker”或者”HTTPD=/usr/sbin/httpd.event”;
4> 更改了MPM机制后,重启httpd服务;
prefork的配置:
<IfModule prefork.c> StartServers 8 服务启动后初始启动的子进程数; MinSpareServers 5 保留空闲的服务器子进程的最小数量; MaxSpareServers 20 保留空闲的服务器子进程的最大数量; ServerLimit 256 生命周期内允许最多多少子进程(MaxClients)存在; MaxClients 256 允许启动的服务器子进程的最大数量; MaxRequestsPerChild 4000 每个子进程最大允许请求多少次,达到此数值后,子进程强制退出;数字"0"表示子进程不会被强制退出;
worker的配置:
<IfModule worker.c> StartServers 4 服务启动后初始启动的子进程数; MaxClients 300 允许存在的最大线程数; MinSpareThreads 25 最小空闲线程; MaxSpareThreads 120 最大空闲线程; ThreadsperChild 25 每个子进程创建的线程数; MaxRequestsPerChild 0 每个子进程最大允许请求多少次,达到此数值后,子进程强制退出;数字"0"表示子进程不会被强制退出;
httpd 2.4 版本:
———————-
MPM模块是以动态模块方式加载的;
更改MPM的运行模块类型,只需更改配置MPM模块配置文件,启动相应模块,更改完后重启服务或重载httpd的主配置文件即可;
~]#vim /etc/httpd/conf.modules.d/00-mpm.conf
# Select the MPM module which should be used by uncommenting exactly # one of the following LoadModule lines: # prefork MPM: Implements a non-threaded, pre-forking web server # See: http://httpd.apache.org/docs/2.4/mod/prefork.html LoadModule mpm_prefork_module modules/mod_mpm_prefork.so # worker MPM: Multi-Processing Module implementing a hybrid # multi-threaded multi-process web server # See: http://httpd.apache.org/docs/2.4/mod/worker.html # #LoadModule mpm_worker_module modules/mod_mpm_worker.so # event MPM: A variant of the worker MPM with the goal of consuming # threads only for connections with active processing # See: http://httpd.apache.org/docs/2.4/mod/event.html # #LoadModule mpm_event_module modules/mod_mpm_event.so
查看httpd静态编译的模块:~]# httpd -l
查看httpd静态编译和动态编译的模块:~]# httpd -M
5、DSO:动态共享对象;
httpd-2.4可动态加载其他模块,配置文件为”/etc/httpd/conf.modules.d/00-proxy.conf”;
LoadModule proxy_module modules/mod_proxy.so LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so LoadModule proxy_ajp_module modules/mod_proxy_ajp.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_connect_module modules/mod_proxy_connect.so LoadModule proxy_express_module modules/mod_proxy_express.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so LoadModule proxy_fdpass_module modules/mod_proxy_fdpass.so LoadModule proxy_ftp_module modules/mod_proxy_ftp.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule proxy_scgi_module modules/mod_proxy_scgi.so LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
加载模块的书写格式:
LoadModule <mod_name> <mod_path>
模块文件路径可使用相对路径,相对于httpd主配置文件中定义的”ServerRoot”,默认为”/etc/httpd/”;
[root@KOU conf.d]# ll /etc/httpd total 0 drwxr-xr-x. 2 root root 37 Jun 1 13:42 conf drwxr-xr-x. 2 root root 123 Jun 1 13:34 conf.d drwxr-xr-x. 2 root root 146 May 24 04:04 conf.modules.d lrwxrwxrwx. 1 root root 19 May 24 04:04 logs -> ../../var/log/httpd lrwxrwxrwx. 1 root root 29 May 24 04:04 modules -> ../../usr/lib64/httpd/modules 'modules'目录是一个软连接; lrwxrwxrwx. 1 root root 10 May 24 04:04 run -> /run/httpd [root@KOU conf.d]#
6、ServerName
<中心主机、虚拟主机均可使用>;
格式: ServerName [scheme://]fully-qualified-domain-name[:port]
例如:ServerName lucifer.kouyuushinn.cn
ServerName 192.168.206.88
7、ServerAlias
<仅在虚拟主机中使用>;
格式:ServerAlias hostname [hostname] …
8、DocumentRoot
8.1 DocumentRoot指向的路径是本机的实际文件系统路径,是做为站点URL的根路径;
yum安装的httpd,默认路径:
DocumentRoot “/var/www/html”
编译安装的httpd,默认路径;
DocumentRoot “/usr/local/apache/htdocs”
8.2 DocumentRoot支持Alias<中心主机、虚拟主机均可使用>:
格式:Alias URL-path file-path|directory-path
举例:
DocumentRoot "/ftp/pub/image" Alias /image /ftp/pub/image <Directory "/ftp/pub/image"> Require all granted </Directory> 可以把"alias"定义的路径别名统一放在httpd主配置文件的固定的默认模块中: <IfModule alias_module> alias /image /ftp/pub/image </IfModule>
9、站点访问控制常见机制;
基于文件系统路径就进行定义,或基于站点URL进行定义;
9.1 基于文件系统路径:
<Directory “”>
…
</Directory>
<File “”>
…
</File>
<FileMatch “PATTERN”>
…
</FileMatch >
9.2 基于站点的URL进行定义:
<Location “”>
…
</Location>
<LocationMatch “PATTERN”>
…
</LocationMatch>
*****************************
httpd-2.2版本:
<Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory>
httpd-2.4版本:
<Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>
上面2个版本的<Directory>中”基于源地址”实现访问控制,其使用的参数选项的含义描述如下:
====================================================================================
1> Options:
后面跟1个或多个以空白字符分隔的”选项”列表;
Indexes :允许索引显示;指明的URL路径下不存在与定义的主页面资源相符的资源文件时,返回所有列表给用户;
FollowSymLinks:允许跟踪符号链接文件所指向的源文件;
None:不启用任何选项;
All:启用所有选项;
2> AllowOverride:
与访问控制相关的指令可以放在’.htaccess’文件中,(此文件每个目录下都可以有一个);
举例几个选项选项:All、None,AuthConfig,Indexes,Limit;
3> Order allow,deny <httpd-2.2>
Order :定义后面2项的生效顺序;写在后面的表示默认法则;
Allow from
Deny from
4> Require all granted <httpd-2.4>
常规用法:
Require all granted
Require all denied
Require group group-name [group-name] …
Require user userid [userid] …
Require valid-user
Require host host_name
Require ip ip_address
Require ip 10 172.20 192.168.2
举例1:
In the following example, all users in the alpha and beta groups are authorized, except for those who are also in the reject group. <Directory /www/docs> <RequireAll> Require group alpha beta Require not group reject </RequireAll> </Directory>
举例2:
<Directory /www/mydocs> <RequireAll> <RequireAny> Require user superadmin <RequireAll> Require group admins Require ldap-group cn=Administrators,o=Airius <RequireAny> Require group sales Require ldap-attribute dept="sales" </RequireAny> </RequireAll> </RequireAny> <RequireNone> Require group temps Require ldap-group cn=Temporary Employees,o=Airius </RequireNone> </RequireAll> </Directory> <RequireAny></RequireAny>: 被用来包含一组授权指令,为了<RequireAny>指令的成功,我们必须成功完成这些指令; 如果<RequireAny>指令中包含的一个或多个指令成功,则<RequireAny>指令成功;如果没有成功,也没有失败,那么它将返回一个中立的结果;在所有其他情况下,它都失败; 此容器中的指令不允许有否定授权指令; <RequireAll></RequireAll>: 用来封装一组授权指令,其中没有一个必须失败,至少有一个必须成功,才能让<RequireAll>指令成功; 如果<RequireAll>指令中没有一个指令失败,并且至少有一个成功,那么<RequireAll>指令成功;如果没有成功,也没有失败,那么它将返回一个中立的结果;在所有其他情况下,它都失败; <RequireNone></RequireNone>: 用于封装一组授权指令,其中没有一个必须成功,才能保证<RequireNone>指令不失败; 此容器中的指令不允许有否定授权指令;
10、定义站点主页面;
DirectoryIndex index.html index.html.var
11、设定默认字符集;
AddDefaultCharset UTF-8
中文字符集:GBK、GB2312、GB18030;
12、/etc/httpd/conf/httpd.conf主配置文件中关于日志格式的相关说明;
1.1 日志设定(日志类型:访问日志、错误日志);
1.2 日志存放目录:/etc/httpd/logs,此目录是链接目录,源目录为”/var/log/httpd”;
1> 错误日志:
ErrorLog “logs/error_log”
LogLevel warn
LogLevel的其他类型,可在httpd官网上查看:http://httpd.apache.org/docs/2.4/mod/core.html#loglevel
2> 访问日志:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined CustomLog "logs/access_log" combined
Log Format String 具体的描述(帮助文档):http://httpd.apache.org/docs/2.4/mod/mod_log_config.html#logformat
访问日志条目示例:
??.104.162.?? - - [14/Mar/2018:23:59:01 +0800] "GET /index.php/Dealsyn/index HTTP/1.1" 200 20 "-" "curl/7.29.0"
%h:客户端IP地址;
%l: Remote User,通常为一个减号;
%u:Remote user if the request was authenticated. May be bogus if return status (%s) is 401 (unauthorized);远程用户名,授权认证;非登录访问时为一个减号;
%t:服务器收到请求时的时间;
“%r:First line of request;请求报文的首行,请求的方法、URL、协议版本;
%>s:最后一次响应状态码;
%b: 响应报文的大小,单位是字节;不包括响应报文的http首部;
%{Referer}i:请求报文中首部”referer”的值;即从哪个页面的超链接跳转至当前页面的;
%{User-Agent}i:请求报文中首部”User-Agent”的值;即发出请求的应用程序(浏览器类型、版本,系统类型或者某个应用程序等);