实验环境:VMware Workstation Pro 14(试用版)系统平台:CentOS release 6.9 (Final) ????????????内核 ?2.6.32-696.el6.x86_64Server version: Apache/2.2.15 (Unix)
模块文件路径:
/etc/httpd/modules/usr/lib64/httpd/modules
主程序文件:
/usr/sbin/httpd/usr/sbin/httpd.work/usr/sbin/httpd.even
主进程文件:
/etc/httpd/run/httpd
日志文件目录:
/var/log/httpd ???access_log: 访问日志 ???error_log:错误日志
配置文件:
/etc/httpd/conf/httpd.conf/etc/httpd/conf.d/*.conf
检查配置语法:
httpd –tservice httpd configtest
配置格式
directive value ???directive: 不区分字符大小写 ???value: 为路径时,是否区分大小写,取决于文件系统
例:
DocumentRoot "/var/www/html"
Apache HTTP 服务器 2.2 文档
http://httpd.apache.org/docs/2.2/
查看静态编译的模块
httpd -l
? 查看静态编译及动态装载的模块
httpd -M
? 动态模块加载:不需重启即生效
? 动态模块路径
/usr/lib64/httpd/modules/
Httpd 2.2常见配置
显示服务器版本信息
默认:ServerTokens OScurl -I localhost
可选值 | 效果 |
---|---|
Major | Server: Apache/2 |
Minor | Server: Apache/2.2 |
Min[imal] | Server: Apache/2.2.15 (CentOS) DAV/2 |
Min | Server: Apache/2.2.15 |
Prod | Server: Apache |
OS | Server: Apache/2.2.15 (CentOS) |
Full | Server: Apache/2.2.15 (CentOS) DAV/2 |
修改监听的IP和Port
Listen [IP:]PORT(1) 省略IP表示为本机所有IP(2) Listen指令至少一个,可重复出现多次默认:Listen 80修改指定IP会,必须重启服务才能生效,reload是不能生效。
定义服务器名称
默认未开启ServerName hunk.tech:80如果没有DNS解析,这里可以设置为IP地址。一般建议使用站点对外发布的FQDN名称。
KeepAlive持久连接
Persistent Connection:连接建立,每个资源获取完成后不会断开连接,而是继续等待其它的请求完成,默认关闭
断开条件:数量限制:100
时间限制:以秒为单位, httpd-2.4 支持毫秒级
副作用:对并发访问量较大的服务器,持久连接功能会使用有些请求得不到响应
折衷:使用较短的持久连接时间
设置:
默认KeepAlive OffKeepAliveTimeout 15MaxKeepAliveRequests 100
MPM( Multi-Processing Module)多路处理模块
prefork, worker, event(2.2试验阶段,不建议使用)
要求更高伸缩性的站点可以选择使用线程的 MPM,即 worker 或 event; 需要可靠性或者与旧软件兼容的站点可以使用 prefork。
httpd-2.2不支持同时编译多个模块,所以只能编译时选定一个;rpm安装的包提供三个二进制程序文件,分别用于实现对不同MPM机制的支持
确认方法:
ps aux | grep httpd默认为/usr/sbin/httpd, 即prefork模式/usr/sbin/httpd.worker 默认为/usr/sbin/worker,
修改为worker模式/etc/sysconfig/httpdHTTPD=/usr/sbin/httpd.worker另外,/etc/httpd/conf/httpd.conf中以下模块配置与此相关<IfModule worker.c>
httpd-2.4 MPM模式修改在以下文件
/etc/httpd/conf.modules.d/00-mpm.conf
prefork的默认配置:
<IfModule prefork.c>StartServers 8MinSpareServers 5MaxSpareServers 20ServerLimit 256 最多进程数,最大20000 ?。ulimit值会同时限制此项设置MaxClients 256 最大并发MaxRequestsPerChild 4000 子进程最多能处理的请求数量。在处理MaxRequestsPerChild 个请求之后,子进程将会被父进程终止,这时候子进程占用的内存就会释放(为0时永远不释放)</IfModule>
worker的默认配置:
<IfModule worker.c>StartServers 4MaxClients 300MinSpareThreads 25MaxSpareThreads 75ThreadsPerChild 25MaxRequestsPerChild 0 无限制</IfModule>
定义网站目录
默认DocumentRoot "/var/www/html"记得修改目录的属主与权限此项修改只需要reload即可
定义默认搜索页面
默认DirectoryIndex index.html index.html.var从左至右顺序查找
定义启动httpd服务的账户与组
默认User apacheGroup apache
站点访问控制类型
目录
<Directory “/path">控制选项</Directory>
文件
1.特定单个文件
<File “/path/file”>控制选项</File>
2.文件通配符模式
<Files "?at.*">控制选项</Files>
3.正则匹配模式
<FileMatch "\.(gif|jpe?g|png)$">控制选项</FileMatch>或者这种写法<files ~ "\.(gif|jpe?g|png)$">控制选项</files>
URL路径
1.特定URL
<Location "/status">控制选项</Location>
2.URL正则匹配模式
<LocationMatch "/(extra|special)/data">控制选项</LocationMatch>
站点访问控制机制
语法格式:
Options:后跟1个或多个以空白字符分隔的选项列表在选项前的+,- 表示增加或删除指定选项常见选项:Indexes: ??????指明的URL路径下不存在与定义声明中的默认搜索相符的资源文件时,返回索引列表给用户FollowSymLinks:允许访问符号链接文件所指向的源文件None: ?????????全部禁用All: ??????????全部允许
示例:
#ll-rw-r--r-- 1 root root 21 Feb 25 10:23 HTTP权威指南.pdflrwxrwxrwx 1 root root 11 Feb 25 10:27 ip.txt -> /app/ip.txt-rw-r--r-- 1 root root 41 Feb 25 10:25 Python3程序开发指南(第二版).pdf 设置如下:<Directory /app/www> ?> 指定控制类型Options Indexes FollowSymLinks ??> 字符串不区分大小写。 控制机制选项:允许列出目录索引,允许访问软链接指向的源文件</Directory> ??[ICO] ?????????????Name ????????????????Last modified ??Size Description ?????????????????????????????????????????????????????????????????????????????═══════════════════════════════════════════════════════ ???????????????????????????????????????????????????????????????????????????[DIR] Parent Directory ???????????????? ???????????????????- ? ???????????????????????????????????????????????????????????????????????????????????????[ ??] HTTP权威指南.pdf ???????????????25-Feb-2018 10:23 ??21 ? ???????????????????????????????????????????????????????????????????????????????????????[ ??] Python3程序开发指南(第二版).pdf 25-Feb-2018 10:25 ??41 ? ??????????????????????????????????????????????????????????????????????????????????????[TXT] ip.txt ?????????????????????????03-Feb-2018 10:07 ??51 ? ?????<Directory /app/www>Options -Indexes FollowSymLinks ?> 禁止列出目录索引</Directory>#elinks http://192.168.5.102/books 403 Forbidden ?Forbidden ?????????????????????????????????????????????????????????????????????????You don‘t have permission to access /books/ on this server. ??????═════════════════════════════════════════════════ ?????????????????Apache Server at 192.168.5.102 Port 80
AllowOverride
与访问控制相关的哪些指令可以放在指定目录下的.htaccess(默认文件名)文件中,覆盖之前的配置指令
一般都应该尽可能地避免使用.htaccess文件。任何希望放在.htaccess文件中的配置,都可以放在主配置文件的<Directory>段中,而且更高效。
htaccess文件是Apache服务器中的一个配置文件,它负责相关目录下的网页配置。通过htaccess文件,可以帮我们实现:网页301重定向、自定义404错误页面、改变文件扩展名、允许/阻止特定的用户或者目录的访问、禁止目录列表、配置默认文档、文件夹密码保护等功能Unix、Linux系统或者是任何版本的Apache Web服务器都是支持.htaccess的启用.htaccess,需要修改httpd.conf,启用AllowOverride,并可以用AllowOverride限制特定命令的使用。如果需要使用.htaccess以外的其他文件名,可以用AccessFileName指令来改变。例如,需要使用.config ,则可以在服务器主配置文件中按以下方法配置:AccessFileName .config.htaccess文件很容易被非授权用户得到,安全性不高。主要应用场景:一般情况下,不应该使用.htaccess文件,除非你对主配置文件没有访问权限。如果服务器管理员不愿意频繁修改配置,则可以允许用户通过.htaccess文件自己修改配置,尤其是ISP在同一个机器上运行了多个用户站点,而又希望用户可以自己改变配置的情况下。
配置指令只对<directory>段有效AllowOverride All: 所有指令都有效AllowOverride None:.htaccess 文件无效AllowOverride AuthConfig Indexes 只覆盖特定指令AuthConfig和Indexes,其它指令都无法覆盖一个示例:#vim /app/www/books/.htaccessoptions -indexes
优先级
<directory>段中的AllowOverride ?> ?主配置文件中的AllowOverride
更加精细的控制方法:
1.客户端来源地址
order和allow、 deny
放在directory, .htaccess中
order:定义生效次序;写在后面的表示默认,当前面的不匹配时生效Allow from:允许访问的客户端地址Deny from:禁止访问的客户端地址客户端地址可以是以下格式: ???apache.org ?????> 一个域 ???foo.apache.org ?> 一个域之中的某台主机 ???172.18.0.200 ???> 一个IP 地址 ???172.16.0.0/16 ??> 一个IP地址段 ???172.16.0.0/255.255.0.0 ?> 一个IP地址段 ???all ????????????> 全部
匹配情形 | 配置为Allow,Deny | 配置为Deny,Allow |
---|---|---|
仅匹配Allow | 允许访问 | 允许访问 |
仅匹配Deny | 拒绝访问 | 拒绝访问 |
没有匹配 | 拒绝访问 | 允许访问 |
Allow和Deny都匹配 | 匹配默认的Deny(拒绝访问) | 匹配默认的Allow(允许访问) |
例子:
设置如下:<Directory /app/www>options indexes followsymlinksorder allow,deny ???????????????> 定义了规则顺序deny from 192.168.5.0/24 ???????> 明确定义了拒绝列表</Directory>上面的规则意思为:其他的任何主机都拒绝访问#curl -I http://192.168.5.102/books/ip.txtHTTP/1.1 403 Forbidden ?????????????????????> 拒绝访问#curl -I http://192.168.7.201/books/ip.txtHTTP/1.1 403 Forbidden---设置如下:<Directory /app/www>options indexes followsymlinksorder deny,allow ???????????????> 定义了规则顺序deny from 192.168.5.0/24 ???????> 明确定义了拒绝列表</Directory>上面的规则意思为:只有明确在拒绝列表的主机禁止访问,其他的任何主机将允许访问#curl -I http://192.168.5.102/books/ip.txtHTTP/1.1 403 Forbidden ?????????????????????> 拒绝访问#curl -I http://192.168.7.201/books/ip.txtHTTP/1.1 200 OK ????????????????????????????> 允许访问---设置如下<Directory /app/www>options indexes followsymlinksorder allow,denydeny from 192.168.5.0/24 ???????> 明确了拒绝列表allow from 192.168.7.201 ???????> 明确了允许访问主机列表</Directory>#curl -I http://192.168.5.102/books/ip.txtHTTP/1.1 403 Forbidden#curl -I http://192.168.7.201/books/ip.txt ?> 访问的当前主机IP为192.168.7.202HTTP/1.1 403 Forbidden ?????> 拒绝访问#curl -I http://192.168.7.201/books/ip.txt ?> 访问的当前主机IP为192.168.7.201HTTP/1.1 200 OK ????????????> 允许访问#curl -I http://172.18.103.79/books/ip.txt ?> 访问的当前主机IP为172.18.103.80HTTP/1.1 403 Forbidden ?????> 拒绝访问因为,访问的IP没有明确匹配allow和deny,就执行了默认的拒绝规则配置如下:定义了不允许访问.conf结尾的文件<files "*.conf">order allow,denydeny from all</files>allow from 127.0.0.1 localhost ?> 仅仅允许本机访问
2.用户账号
####基于用户的basic认证
语法:<Directory "/路径">AuthType Basic ?????> 认证方法AuthName "String" ?> 认证提示字符串AuthUserFile "认证用户数据库路径"Require user username1 username2 ... ?> 允许访问的认证用户</Directory>其中,允许账号文件中的所有用户登录访问:Require valid-user
创建使用文本文件作为数据库
htpasswd [ -c ] [ -m ] [ -D ] passwdfile usernamehtpasswd -b [ -c ] [ -m | -d | -p | -s ] [ -D ] passwdfile username password-c:自动创建文件,仅在文件不存在时使用-m:md5格式加密,默认方式-s: sha格式加密-D:删除指定用户-b: 批处理时使用,可以通过命令行直接读取密码而不是交互。-n: 不更新文件,仅仅屏幕输出命令执行结果。交互方式:#htpasswd -c /etc/httpd/conf.d/.htpasswd hunk1New password: Re-type new password: Adding password for user hunk1非交互方式:#htpasswd -bs /etc/httpd/conf.d/.htpasswd hunk2 1234567Adding password for user hunk2生成的密码是经过加密的#cat .htpasswd hunk1:xLhgTub5K6Csshunk2:{SHA}IOq+XWSw4hZ5boNPUtYf0LcDMvw=仅仅显示命令执行效果#htpasswd -nbs hunk3 1234567hunk3:{SHA}IOq+XWSw4hZ5boNPUtYf0LcDMvw=删除指定用户#htpasswd -D /etc/httpd/conf.d/.htpasswd hunk2Deleting password for user hunk2
示例:<Directory "/app/www/books">options indexes followsymlinksAuthType BasicAuthName "请输入密码才允许访问"AuthUserFile "/etc/httpd/conf.d/.htpasswd"Require user hunk1 hunk2</Directory>#cat .htpasswd hunk1:k5HXZqDz4BLE.hunk2:{SHA}IOq+XWSw4hZ5boNPUtYf0LcDMvw=hunk3:{SHA}IOq+XWSw4hZ5boNPUtYf0LcDMvw=
#links http://192.168.5.102/books
日志中也会有记录
#tail /var/log/httpd/access_log192.168.5.103 - hunk2 [27/Feb/2018:15:44:39 +0800] "GET /books/ HTTP/1.1" 200 1636 "http://192.168.5.102/books" "ELinks/0.12pre6 (textmode; Linux; 150x27-2)"
使用hunk3进行访问是没有权限的
#curl -I 192.168.5.102/booksHTTP/1.1 401 Authorization RequiredDate: Tue, 27 Feb 2018 07:40:14 GMTServer: ApacheWWW-Authenticate: Basic realm="请输入密码才允许访"Connection: closeContent-Type: text/html; charset=iso-8859-1日志中也会有记录#tail /var/log/httpd/error_log[error] [client 192.168.5.1] access to /books failed, reason: user ‘hunk3‘ does not meet ‘require‘ments for user/valid-user to be allowed access
####基于组的basic认证
语法:<Directory "/路径">AuthType Basic ?????> 认证方法AuthName "String" ?> 认证提示字符串AuthUserFile "认证用户数据库路径"AuthGroupFile "认证组数据库路径"Require group grpname1 grpname2 ... > 允许访问的认证用户,组内的用户必须是AuthUserFile存在的用户</Directory>定义组和用户对应关系组名:用户1 用户2#cat .htgroup admin:hunk1 hunk3webgroups:hunk2一行定义一个组
示例:
<Directory "/app/www/books">options indexes followsymlinksAuthType BasicAuthName "请输入密码才允许访"AuthUserFile "/etc/httpd/conf.d/.htpasswd"AuthGroupFile "/etc/httpd/conf.d/.htgroup" ?????> 指定组文件Require group admin ?????????????????????????????> 允许访问的组</Directory>
远程客户端和用户验证的控制
Satisfy ALL|Any ???ALL 客户机IP和用户验证都需要通过才可以 ???Any 客户机IP和用户验证,有一个满足即可
示例:
<Directory "/app/www/books">options indexes followsymlinksAuthType BasicAuthName "请输入密码才允许访"AuthUserFile "/etc/httpd/conf.d/.htpasswd"Require valid-user ??????????????????????????> 允许所有定义好的用户访问Order allow,deny ???????Allow from 192.168.1.0/24 ???> 定义允许访问的客户端IP范围Satisfy All ????????????????> ?定义了只有192.168.1.0网段的IP,且通过了账户验证的才可以访问</Directory>
日志设定
错误日志设置
ErrorLog logs/error_log ?????> 这里是相对路径
记录日志级别设置
LogLevel warn可用值:debug, info, notice, warn,error,crit, alert, emerg
定义日志格式
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedLogFormat "%h %l %u %t \"%r\" %>s %b" commonLogFormat "%{Referer}i -> %U" refererLogFormat "%{User-agent}i" agent最后的单词就是定义的格式名称,给其他语句块调用的
使用日志格式
CustomLog logs/access_log combine%h 客户端IP地址%l 远程用户,启用mod_ident才有效,通常为减号“-”%u 验证(basic,digest)远程用户,非登录访问时,为一个减号“-”%t 服务器收到请求时的时间%r First line of request,即表示请求报文的首行;记录了此次请求的“方法”,“URL”以及协议版本%>s 响应状态码%b 响应报文的大小,单位是字节;不包括响应报文http首部%{Referer}i 请求报文中首部“referer”的值;即从哪个页面中的超链接跳转至当前页面的%{User-Agent}i 请求报文中首部“User-Agent”的值;即发出请求的应用程序更多的格式说明:http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats
设定默认字符集
默认AddDefaultCharset UTF-8中文字符集:GBK, GB2312, GB18030
定义路径别名
语法:alias /URL /本地磁盘实际路径/
未定义别名前#curl 192.168.5.102/books/wwww/books/index.html定义别名后alias /books /app/www2/#curl 192.168.5.102/books/wwww2/index.html
正则表达式 语法:aliasmatch 正则表达式 "实际访问路径"aliasmatch ^/books/(.*)? /app/www2/new$1 ?> $1表示的是前面( )内匹配的内容aliasmatch ^.*/(.*\.(jpe?g|gif|png|bmp))$ "/app/www2/imgs/$1" ???> 自动将访问的图片目录跳转
#curl 192.168.5.102/books/index.html -lv* Connected to 192.168.5.102 (192.168.5.102) port 80 (#0)> GET /books/index.html HTTP/1.1看,确实跳转到了新的文件名wwww2/newindex.html* Closing connection 0
实现用户家目录的http共享
基于模块mod_userdir.so实现
主配置文件中启用LoadModule userdir_module modules/mod_userdir.so#httpd -M|grep userdir userdir_module (shared)Syntax OK
语法:<IfModule mod_userdir.c> ???UserDir 共享的目录 ?????> 注意,这里的目录指的是系统账号家目录中的子目录。如:/home/hunk/userdir</IfModule>
注意事项:
如果配置文件语句是写在/etc/httpd/conf.d/目录下的话,需要将主配置文件中以下行注释掉。# ???UserDir disabled
示例:
<IfModule mod_userdir.c> ???UserDir userhome</IfModule>#tree /home/hunk/home/hunk└── userhome ???└── index.html注意添加httpd服务账户访问权限 ??#setfacl -m u:apache:x /home/hunk/ ?#curl 192.168.5.102/~hunk/index.html/home/hunk/userhome
ServerSignature错误信息显示
默认 on当客户请求的网页并不存在时,服务器将产生错误文档,错误文档的最后一行将包含服务器的名字、 Apache的版本等信息如果不对外显示这些信息,就可以将这个参数设置为Off设置为Email,将显示ServerAdmin 的Email提示可选值:On | Off | EMail
on 的显示:#links http://192.168.5.102/a.html404 Not Found Not Found ?????????????????????????????????????????????????????????????????????????The requested URL /a.html was not found on this server. ??????????????????????????????????????????????????????????????????????????????????????????????════════════════════════════════════════════════════════ ???Apache Server at 192.168.5.102 Port 80off 的显示#links http://192.168.5.102/a.html404 Not Found Not Found ?????????????????????????????????????????????????????????????????????????The requested URL /a.html was not found on this server.
ServerType 服务器运行模式
standalone 独立服务模式inetd 非独立服务模式只适用于Unix平台
status页面
基于模块LoadModule status_module modules/mod_status.so实现
注意控制允许访问的用户
<Location /访问目录,不是实际的磁盘路径> ???SetHandler server-status</Location>ExtendedStatus On 开启扩展信息,这一条不能写入具体的配置块里面,在配置文件里加下即可。
ServerName 绑定名称
ServerName hunk.tech如果不绑定一个名称,启动服务时会有提示警告。实际并不影响正常启动服务。
ServerAlias 绑定多个域名
ServerAlias web.hunk.tech shop.hunk.tech > 多个域名,用空格隔开只能通过指定的名称访问
VirtualHost 虚拟主机
所谓虚拟主机是指在一台服务器里运行几个网站,提供WEB、FTP等服务。
注意:一般虚拟机不要与main主机混用;因此,要使用虚拟主机,一般先注释主配置文件中的以下选项
DocumentRootListen
在使用虚拟主机的时候,建议不要在主配置文件中配置,为了管理上更加清晰,可以独立在/etc/httpd/conf.d目录下以虚拟主机站点名进行区分,如:
#tree ../conf.d../conf.d├── basic.conf├── README├── user.conf├── www2.hunk.tech.conf├── www3.hunk.tech.conf└── www.hunk.tech.conf
有三种实现虚拟主机方案:
记得先把主配置中的Listen注释掉,在每个虚拟主机配置文件独立指定。
基于ip
为每个虚拟主机准备至少一个ip地址
应用场景:多用于内部网络
Listen 80<VirtualHost 192.168.5.102:80> ????????> 定义了以主机IP为访问方法 ???ServerAdmin webmaster@dummy-host.example.com ???DocumentRoot /app/www/virtuahost/www.hunk.tech ???ServerName www.hunk.tech ???ErrorLog logs/dummy-host.example.com-error_log ???CustomLog logs/dummy-host.example.com-access_log common</VirtualHost>
基于端口port
为每个虚拟主机使用至少一个独立的端口port
应用场景:多用于内部网络
Listen 80Listen 8080<VirtualHost 192.168.5.102:80> ???????????ServerAdmin webmaster@dummy-host.example.com ???DocumentRoot /app/www/virtuahost/www.hunk.tech ???ServerName www.hunk.tech ???ErrorLog logs/dummy-host.example.com-error_log ???CustomLog logs/dummy-host.example.com-access_log common</VirtualHost><VirtualHost 192.168.5.102:8080> ???ServerAdmin webmaster@dummy-host.example.com ???DocumentRoot /app/www/virtuahost/www2.hunk.tech ???ServerName www2.hunk.tech ???ErrorLog logs/dummy-host.example.com-error_log ???CustomLog logs/dummy-host.example.com-access_log common</VirtualHost>IP相同,但端口不同IP不同,但端口均为默认端口
基于FQDN
为每个虚拟主机使用至少一个FQDN
应用场景:多用于公网,生产环境
NameVirtualHost *:80 ??> 这一行在2.2版本是必须添加的。2.4之后的版本,官方则去掉了这一个选项,但是功能是相同的。<VirtualHost *:80> ???ServerAdmin webmaster@dummy-host.example.com ???DocumentRoot /app/www/virtuahost/www3.hunk.tech ???ServerName www.hunk.tech ???ErrorLog logs/dummy-host.example.com-error_log ???CustomLog logs/dummy-host.example.com-access_log common</VirtualHost>区分在于 请求报文中首部
配置了3个虚拟主机之后,可以看到监听的端口号也变了
#ss -nltState ?????Recv-Q Send-Q ??????????????????????????????????????????Local Address:Port ????????????????????????????????????????????Peer Address:Port LISTEN ????0 ?????128 ????????????????????????????????????????????????????????:::8080 ??????????????????????????????????????????????????????:::* ????LISTEN ????0 ?????128 ????????????????????????????????????????????????????????:::80 ????????????????????????????????????????????????????????:::* ????LISTEN ????0 ?????128 ????????????????????????????????????????????????????????:::8081 访问各自己的站点也是正常的#curl 192.168.5.102www.hunk.tech#curl 192.168.5.102:8080www2.hunk.tech#curl 6-web-1.hunk.tech:8081www3.hunk.tech
基于FQDN的模式配置示例:
注意,必须在主配置文件中设置以下2项,这个与上面的其他模式有些不同。
NameVirtualHost *:80Listen 80
#cat /etc/httpd/conf.d/www*站点1:<VirtualHost *:80> ???DocumentRoot /app/www/virtuahost/www2.hunk.tech ???ServerName www2.hunk.tech ??> 这里设置通过网络访问的FQDN</VirtualHost>站点2:<VirtualHost *:80> ???DocumentRoot /app/www/virtuahost/www3.hunk.tech ???ServerName www3.hunk.tech ??> 这里设置通过网络访问的FQDN</VirtualHost>站点3:<VirtualHost *:80> ???DocumentRoot /app/www/virtuahost/www.hunk.tech ???ServerName www.hunk.tech ???> 这里设置通过网络访问的FQDN</VirtualHost>
在充当测试客户机的主机上设置/etc/hosts
192.168.7.201 www.hunk.tech www2.hunk.tech www3.hunk.tech
可以实现通过不同的域名分别对应不同的虚拟主机。很多共享主机提供商用的就是这些技术啦。
#curl www.hunk.techwww.hunk.tech#curl www2.hunk.techwww2.hunk.tech#curl www3.hunk.techwww3.hunk.tech
仅允许通过域名访问站点,禁止使用IP方式访问的实现
方法1:
#vim denyip-www.hunk.tech.conf<VirtualHost 192.168.7.201:80> ?????> 指定IP ???ServerName 192.168.7.201 ???????> 绑定访问方式 ???<Location /> ?????order deny,allow ?????deny from all ????????????????> 拒绝所有 ???</Location></VirtualHost>#curl 192.168.7.201403 ForbiddenYou don‘t have permission to access / on this server
方法2:
建一个首页文件,如index.hmtl,首面文件内容可以是一个声明不允许以IP访问,如果想更友好点,可以设置跳转。#echo ‘deny from ip ‘ > empty/index.html#curl 192.168.7.201deny from ip
Centos 6 apache httpd 2.2 主要配置详解( 一 )
原文地址:http://blog.51cto.com/191226139/2073716