您的位置:首页 > 理论基础 > 计算机网络

linux笔记 第二十一天 http高级配置及ab工具使用及编译安装http2.4

2016-05-31 23:00 816 查看
httpd配置(2)
12、设定默认字符集
ASCII

字符集:GB2312, GB18030, GBK
UTF

AddDefaultCharset
13、CGI脚本
CGI脚本路径别名

/var/www/cgi-bin/ http://server/cgi-bin/bash写CGI脚本:所有文本都使用命令输出:echo, printf, cat 执行程序:命令引用

Content-Type: text/html
<pre>
</pre>
FastCGI: 协议

14、基于用户访问控制
用户认证:
基本认证: Basic
摘要认证:digest

虚拟用户:仅用于访问某服务或获取某资源的凭证;
文本文件:.htpasswd
SQL数据库
dbm: 数据库引擎,提供API
ldap:

authentication provider: 账号和密码的存储机制;
authn

authorization provider: 授权

案例:基于文件做访问控制

(1) 基于用户进行认证

<Directory "/var/www/html/admin">
Options none
AllowOverride AuthConfig
AuthType Basic
AuthName "Admin Area."
#AuthBasicProvider file
AuthUserFile /etc/httpd/conf/.htpasswd
Require valid-user
</Directory>

Require valid-user: 文件中所有用户均可访问
Require user USERNAME, ...

(2) 提供认证文件
htpasswd
-c: 如果此文件事先不存在,则创建;注意,只能在创建第一个用户时使用;
-m:以md5的格式编码存储用户的密码信息
-D:删除指定用户

(3) 组认证
<Directory "/var/www/html/admin">
Options none
AllowOverride AuthConfig
AuthType Basic
AuthName "Admin Area."
#AuthBasicProvider file
AuthUserFile /etc/httpd/conf/.htpasswd
AuthGroupFile /etc/httpd/conf/.htgroup
Require group GROUP_NAME
</Directory>

组文件: 如:
组名:user1 user2 user3

15、虚拟主机
虚拟主机:使用不同访问路径
基于端口
基于IP
基于主机名

(1) 使用虚拟的前提:取消主服务器
注释主服务器的站点根路径指定:DocumentRoot

(2) 定义虚拟主机
NameVirtualHost IP:PORT

<VirtualHost IP:PORT>
ServerName
DocumentRoot
ServerAlias
ErrorLog
CustomLog
</VirtualHost>

配置文件语法检查:
httpd -t
service httpd configtest

配置示例:
<VirtualHost 172.16.100.7:80>
ServerName www.mageedu.com
DocumentRoot "/web/hosta"
</VirtualHost>

<VirtualHost 172.16.100.8:80>
ServerName www.mageedu.com
DocumentRoot "/web/hostb"
</VirtualHost>

<VirtualHost 172.16.100.8:8080>
ServerName www.mageedu.com
DocumentRoot "/web/hostc"
</VirtualHost>

测试:elinks
-dump: 获取到页面数据后直接退出进程;

16、https协议

ssl(安全的套接字层), tls(传输层安全)

http协议:文本编码

验正:使用telnet发请求

# telnet 172.16.100.7 80
Trying 172.16.100.7...
Connected to 172.16.100.7.
Escape character is '^]'.
GET /index.html HTTP/1.0
Host: www.b.org

HTTP/1.1 200 OK
Date: Fri, 08 Aug 2014 03:03:51 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Fri, 08 Aug 2014 02:14:52 GMT
ETag: "e0009-12-50014c53e753f"
Accept-Ranges: bytes
Content-Length: 18
Connection: close
Content-Type: text/html; charset=UTF-8

<h1> Host B </h1>
Connection closed by foreign host.

httpd: ssl
ssl模块
单独成包

ssl会话基于IP地址创建,所以,每一个IP仅创建一个SSL会话;

ssl握手要完成的工作:
交换协议版本号
选择双方都支持的加密方式
客户端对服务器端实现身份验正
密钥交换

https协议: 基于SSL二进制编码, 443/tcp
openssl s_client

客户端验正服务器端证书:
有效性检测:证书是否仍然在有效期内
CA的可信度检测:
证书的完整性检测:
持有者的身份检测

配置httpd工作于https:
(1) 安装mod_ssl模块
# yum install mod_ssl

(2) 为服务端生成私钥,并为其提供证书;
# mkdir /etc/httpd/ssl && cd /etc/httpd/ssl
# (umask 077; openssl genrsa -out httpd.key 1024)
# openssl req -new -key httpd.key -out httpd.csr

签署后的证书为:/etc/httpd/ssl/httpd.crt

(3) 配置使用https的虚拟主机;

SSLCertificateFile
SSLCertificateKeyFile

<VirtualHost IP:443>
DocumentRoot
ServerName
</VirtualHost>

(4) 重新装载配置

(5) 测试
# openssl s_client -connect IP:PORT -CAfile /path/to/ca_certificate

17、status页面
httpd内嵌有handler,其中有一个handler用于输出当前httpd服务相关状态信息

handler: server-status

启用handler要使用SetHandler指令

handler: 当文件被调用时,apache内部表示形式;一般每种文件类型都有其隐式处理器

18、访问属性配置总结
配置文件系统访问路径:
<Directory [~] "">
</Directory>

<File [~] "">
</File>

配置URL访问路径:
<Location [~] "">
</Location>

<LocationMatch "">
</LocationMatch>

/var/www/html/
images/

19、curl命令

curl是基于URL语法在命令行方式下工作的文件传输工具,它支持FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE及LDAP等协议。curl支持HTTPS认证,并且支持HTTP的POST、PUT等方法, FTP上传, kerberos认证,HTTP上传,代理服务器, cookies, 用户名/密码认证, 下载文件断点续传,上载文件断点续传,,http代理服务器管道( proxy tunneling), 甚至它还支持IPv6, socks5代理服务器,,通过http代理服务器上传文件到FTP服务器等等,功能十分强大。

curl的常用选项:

-A/--user-agent <string> 设置用户代理发送给服务器
-basic 使用HTTP基本认证
--tcp-nodelay 使用TCP_NODELAY选项
-e/--referer <URL> 来源网址
--cacert <file> CA证书 (SSL)
--compressed 要求返回是压缩的格式
-H/--header <line>自定义头信息传递给服务器
-I/--head 只显示响应报文首部信息
--limit-rate <rate> 设置传输速度
-u/--user <user[:password]>设置服务器的用户和密码
-0/--http1.0 使用HTTP 1.0

20、使用mod_deflate模块压缩页面优化传输速度

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

# Level of compression (Highest 9 - Lowest 1)
DeflateCompressionLevel 9

# Netscape 4.x has some problems.
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

练习:

1、建立httpd服务器(基于编译的方式进行),要求:
提供两个基于名称的虚拟主机:
(a)www1.stuX.com,页面文件目录为/web/vhosts/www1;错误日志为/var/log/httpd/www1.err,访问日志为/var/log/httpd/www1.access;
(b)www2.stuX.com,页面文件目录为/web/vhosts/www2;错误日志为/var/log/httpd/www2.err,访问日志为/var/log/httpd/www2.access;
(c)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名;
(d)通过www1.stuX.com/server-status输出httpd工作状态相关信息,且只允许提供帐号密码才能访问(status:status);

2、为上面的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点;
(1)要求使用证书认证,证书中要求使用的国家(CN)、州(Henan)、城市(Zhengzhou)和组织(MageEdu);
(2)设置部门为tech,主机名为www2.stuX.com,邮件为admin@stuX.com;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: