一、httpd常见配置/etc/httpd/conf/httpd.conf
#检查/etc/httpd/conf/httpd.conf语法
httpd -t
#查看静态编译模块的命令
httpd -l
#查看静态编译及动态装载的模块
httpd -M
#
#指定加载模块配置文件
ServerRoot "/etc/httpd"
#包含other配置文件
Include conf.modules.d/*.conf
#当无匹配文件时,include会报错,IncludeOptional会忽略错误
IncludeOptional conf.d/*.conf
#动态模块所在路径: /usr/lib64/httpd/modules/
LoadModule access_compat_module modules/mod_access_compat.so
#隐藏服务器版本信息 ServerTokens Major | Minor | Min[imal] | Prod[uctOnly] | OS|Full
ServerTokens Prod[uctOnly] :Server: Apache
#禁止错误网页版本泄露
ServerSignature On | Off | EMail
#持久连接相关指令KeepAlive On|Off
KeepAlive On
#连接持续15s,可以以ms为单位,默认值为5s
#持久连接最大接收的请求数,默认值100
KeepAliveTimeout 15
#MPM多路处理模块,httpd 支持三种MPM工作模式:prefork, worker, event
#注:不要同时启用多个MPM模块
LoadModule mpm_event_module modules/mod_mpm_event.so
#prefork模式相关配置
StartServers 100
MinSpareServers 50
MaxSpareServers 80
ServerLimit 2560 #最多进程数,最大值 20000
MaxRequestWorkers 2560 #最大的并发连接数,默认256
#MaxConnectionsPerChild:子进程最多能处理的请求数量。
#在处理MaxRequestsPerChild个请求之后,子进程将会被父进程终止,此时子进程占用的内存就会释放(为0时永远不释放)
MaxConnectionsPerChild 4000
MaxRequestsPerChild 4000 #从 httpd.2.3.9开始被MaxConnectionsPerChild代替
#worker和event 模式相关的配置
ServerLimit 16 #最多worker进程数
StartServers 10 #创建子进程的进程数
MaxRequestWorkers 150 #最大的并发连接数
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25 #每个子进程创建的线程数
#监听的IP和Port
#Listen 12.34.56.78:80
Listen 80 #省略IP表示为本机所有IP
User apache
Group apache
ServerAdmin root@localhost
#指定服务器名
ServerName www.example.com:80
#定义Main server的文档页面路径,/var/www/html显式授权后才可访问
DocumentRoot "/var/www/html"
#定义路径别名 Alias /URL/ "/PATH/"
Alias /webpath /full/filesystem/path
###################实现访问控制的资源##############################
#格式:<><>
<Directory />
AllowOverride none
Require all denied
</Directory>
#
<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
# Further relax access to the default document root:
<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>
#基于扩展正则表达式
<FilesMatch ".+\.(gif|jpe?g|png)$">
...
</FilesMatch>
##########################针对目录和URL实现访问控制##############################
#(1)Options指令之后所跟的常见选项, +-表示增加或删除指定选项
#Indexes:指明的URL路径下不存在与定义的主页面资源相符的资源文件时,返回索引列表给用户
#FollowSymLinks:允许访问符号链接文件所指向的源文件
#None:全部禁用
#All: 全部允许
#(2)AllowOverride指令
#常见用法:
#AllowOverride All: .htaccess中所有指令都有效
#AllowOverride None: .htaccess 文件无效,此为httpd 2.3.9以后版的默认值
#AllowOverride AuthConfig .htaccess 文件中,除了AuthConfig 其它指令都无法生效
<Directory /web/docs>
Options -Indexes -FollowSymLinks
AllowOverride options=FollowSymLinks,indexes
</Directory>
<Directory /web/docs/spec>
Options -FollowSymLinks
</Directory>
########################IP访问控制##########################
#黑名单
<RequireAll>
Require all granted #允许所有主机访问
Require not ip 172.16.1.1 #拒绝特定IP
</RequireAll>
#白名单
<RequireAny>
Require all denied #拒绝所有主机访问
require ip 172.16.1.1 #允许特定IP
</RequireAny>
##########################日志设定###############################
#错误日志
#LogLevel debug | info | notice | warn | error | crit | alert | emerg
LogLevel warn
ErrorLog "logs/error_log"
#访问日志
LogFormat format nickname #定义日志格式
CustomLog file nickname #使用日志格式
#URL重定向,即将httpd 请求的URL转发至另一个的URL
Redirect [status] URL-path URL
2.基于用户访问控制配置
(1)基于用户账号进行认证
- 定义安全域
<Directory "/path">
Options None
AllowOverride None
AuthType Basic
AuthName "String" #浏览器不同,可能这字符不一定能显示出来
AuthUserFile "/PATH/HTTPD_USER_PASSWD_FILE"
Require valid-user #允许账号文件中的所有用户登录访问
#Require user username1 username2 ... 指定用户
</Directory>
- 提供账号和密码存储(文本文件),htpasswd命令完成此类文件的创建和用户管理
htpasswd [options] /PATH/HTTPD_PASSWD_FILE username password
-c 自动创建文件,仅应该在文件不存在时使用
-b 非交互方式创建用户,命令后面可以接密码
-p 明文密码
-d CRYPT格式加密,默认
-m md5格式加密
-s sha格式加密
-D 删除指定用户
#htpasswd命令新建/etc/httpd/conf.d/.httpuser
htpasswd -cb /etc/httpd/conf.d/.httpuser Ammon 123456
#新建配置文件,并输入定义的安全域
touch /etc/httpd/conf.d/test.conf
<Directory /var/www/html>
AuthType Basic
AuthName "FBI warning"
AuthUserFile "/etc/httpd/conf.d/.httpuser"
Require valid-user
</Directory>
3.status 状态页
LoadModule status_module modules/mod_status.so
<Location "/status">
SetHandler server-status
</Location>
ExtendedStatus On #显示扩展信息,httpd 2.3.6以后版默认为On
4.多虚拟主机
虚拟主机的基本配置方法:
<VirtualHost IP:PORT>
ServerName FQDN
DocumentRoot "/path"
</VirtualHost>
(1)基于IP的虚拟主机
- 创建三个网站站点相关
mkdir /var/www/web{1,2,3}
echo www.a.com on web1 > web1/index.html
echo www.b.com on web2 > web2/index.html
echo www.c.com on web3 > web3/index.html
ip a a 10.0.0.101/24 dev ens33 label ens33:1
ip a a 10.0.0.102/24 dev ens33 label ens33:2
ip a a 10.0.0.103/24 dev ens33 label ens33:3
#查看
ip -a
- 修改文件/etc/httpd/conf.d/test.conf,增加多虚拟主机配置相关
<VirtualHost 10.0.0.101:80>
DocumentRoot /var/www/web1
<Directory /var/www/web1>
#授权
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 10.0.0.102:80>
DocumentRoot /var/www/web2
<Directory /var/www/web2>
#授权
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 10.0.0.103:80>
DocumentRoot /var/www/web3
<Directory /var/www/web3>
#授权
Require all granted
</Directory>
</VirtualHost>
- 重启httpd服务,并在web端测试
!sys #重启httpd
(2)基于端口的虚拟主机
修改etc/httpd/conf.d/test.conf
#增加监听端口
listen 81
listen 82
listen 83
#将上述VirtualHost标签依次改为
<VirtualHost *:81>
<VirtualHost *:82>
<VirtualHost *:83>
重启并测试
(3)基于FQDN虚拟主机
- 修改etc/httpd/conf.d/test.conf
#修改全部改为<VirtualHost *:80>
#并在<VirtualHost *:80>标签下DocumentRoot之前增加ServerName
ServerName www.a.com
ServerName www.b.com
ServerName www.c.com
修改客户端/etc/hosts文件并测试
10.0.0.153 www.a.com www.b.com www.c.com
5.压缩
#可选项
SetOutputFilter DEFLATE
# 指定对哪种MIME类型进行压缩,必须指定项
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
#压缩级别 (Highest 9 - Lowest 1)
DeflateCompressionLevel 9
#排除特定旧版本的浏览器,不支持压缩
#Netscape 4.x 只压缩text/html
BrowserMatch ^Mozilla/4 gzip-only-text/html
#Netscape 4.06-08 三个版本 不压缩
BrowserMatch ^Mozilla/4\.0[678] no-gzip
#Internet Explorer标识本身为"Mozilla / 4”,但实际上是能够处理请求的压缩。如果用户代理首部匹
配字符串"MSIE”("B”为单词边界”),就关闭之前定义的限制
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
在学习中进步,如有错误,请多多批评指正