Linux初学者学习笔记我爱编程

20170927 http服务和apache(二)

2017-10-09  本文已影响20人  哈喽别样
  • http-2.2常见配置
  • http协议
  • curl和elinks工具

一、http-2.2常见配置

### Section 1: Global Environment              //全局配置
### Section 2: 'Main' server configuration     //主服务器配置
### Section 3: Virtual Hosts                   //虚拟主机配置

(一)显示服务器版本信息

(二)修改监听的IP和Port

vim /etc/httpd/conf/httpd.conf
Listen 192.168.136.229:8080

httpd -t
service httpd reload
curl -I 192.168.136.229:80
curl -I 192.168.136.229:8080
curl -l 172.18.250.44:80

(三)持久连接

vim /etc/httpd/conf/httpd.conf
KeepAlive On
KeepAliveTimeout 150
MaxKeepAliveRequests 100

telnet 192.168.136.229 80
GET /index.html HTTP/1.1
HOST:3.3.3.3     //目前服务器只管理一个域名,故可以随便写
GET /hello.txt HTTP/1.1
HOST:5.5.5.5

(四)MPM(Multi-Processing Module)多路处理模块

(1)MPM分类:prefork, worker, event
(2)模块
(3)切换使用的httpd程序
//切换至work模式
vim /etc/sysconfig/httpd
HTTPD=/usr/sbin/httpd.worker     //将行前的注释符#删除

httpd -t
service httpd restart
(4)prefork的默认配置
vim /etc/httpd/conf/httpd.conf

<IfModule prefork.c>
StartServers       8       //启动开启的进程数
MinSpareServers    5       //最少空闲进程数
MaxSpareServers   20       //最大空闲进程数
ServerLimit      256       //最多进程数,不能超过MaxClient,最大能设置为20000
MaxClients       256       //最大并发数
MaxRequestsPerChild  4000  //子进程最多能处理的请求,达到设置值子进程被父进程终止,释放内存
</IfModule>
(5)worker的默认配置
vim /etc/httpd/conf/httpd.conf

<IfModule worker.c>
StartServers         4
MaxClients         300
MinSpareThreads     25
MaxSpareThreads     75
ThreadsPerChild     25    
MaxRequestsPerChild  0     //无限制
</IfModule>

启动时开启4个进程,每个进程开启25个线程,共100个线程,但最多空闲75个线程,需要结束1个进程共25个线程,故开启服务后实际只能开到3个进程共75个线程

(五)DSO(Dynamic Shared Object):动态加载模块

vim /etc/httpd/conf/httpd.conf
#LoadModule auth_basic_module modules/mod_auth_basic.so     //行前增加注释符#
httpd -t

service httpd reload
httpd -M | grep auth_basic_module 

(六)定义Main server的文档页面路径

vim /etc/httpd/conf/httpd.conf
DocumentRoot "/app"     //修改此行的值为/app

httpd -t
service httpd reload
echo "/app/index.html" > /app/index.html     //在/app目录下创建html文件
curl 192.168.136.229

(七)定义站点主页面

mkdir /app/bbs
echo "/bbs/index" > /app/bbs/index
curl 192.168.136.229/bbs/                         //403 Forbidden 错误
vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.html index    //行尾添加index

httpd -t
service httpd reload
curl 192.168.136.229/bbs/ 

设置DirectoryIndex的值包含index前

设置DirectoryIndex的值包含index后

(八)站点访问控制常见机制

访问控制机制有两种:客户端来源地址,用户账号

(1)被访问控制的资源描述方式:
(2)基于源地址的访问控制:
1)Options:后跟1个或多个以空白字符分隔的选项列表
2)AllowOverride
echo "Options FollowSymLinks" > /app/bbs/.htaccess
ln -s /etc/issue /app/bbs/issue.link
httpd -t
service httpd reload
curl 192.168.136.229/bbs/issue.link     //403错误,因为还没有AllowOverride授权
vim /etc/httpd/conf.d/bbs.conf
<Directory "/app/bbs">
        Options Indexes
        AllowOverride FollowSymLinks    
</Directory>

httpd -t
service httpd reload
curl 192.168.136.229/bbs/issue.link     //成功

AllowOverride授权前

AllowOverride授权后

3)Order和Allow、Deny
vim /etc/httpd/conf.d/allowdeny.conf
<files "*.html">
        order deny,allow
        allow from 192.168.136.230
        deny from 192.168.136
</files>

httpd -t
service httpd reload

当192.168.136.230的配置出现冲突,默认以order行中靠后的allow为默认法则
故允许192.168.136.230访问服务器的html文件

192.168.136.0的配置没有出现冲突,故不允许网段的其他主机访问服务器的html文件

不允许IP为192.168.136.230的主机访问服务器的html文件

vim /etc/httpd/conf.d/allowdeny.conf
<files "*.html">
        order allow,deny
        allow from 192.168.136.230
        deny from 192.168.136
</files>

httpd -t
service httpd reload

当192.168.136.230的配置出现冲突,默认以order行中靠后的deny为默认法则
故不允许192.168.136.230访问服务器的html文件

192.168.136.0的配置没有出现冲突,故仍旧不允许网段的其他主机访问服务器的html文件

(九)日志设置

/etc/httpd/conf/httpd.conf
LogFormat "%h %f %t %U"  hello
CustomLog logs/access_log hello

httpd -t
service httpd reload

(十)设定默认字符集

vim /etc/httpd/conf/httpd.conf
AddDefaultCharset GB2312
httpd -t
service httpd reload

(十一)定义路径别名

vim /etc/httpd/conf.d/bbs.conf
Alias /bbs /app/forum

mkdir /app/forum
echo "/app/forum/index.html" > /app/forum/index.html
httpd -t
service httpd reload
curl 192.168.136.229/bbs/

(十二)基于用户的访问控制

(1)认证的相关概念
(2)basic认证
//1. 定义安全域
mkdir /app/secret
echo "/app/secret/index.html" > /app/secret/index.html
vim /etc/httpd/conf.d/auth.conf
<Directory /app/secret>
        AuthType basic
        AuthName "secret zone"
        AuthUserFile "/etc/httpd/conf.d/authuser"
        Require user tom jerry
</Directory>

//2. 生成存储用户密码的文件
htpasswd  -c /etc/httpd/conf.d/authuser tom     //-c:第一次创建文件用
htpasswd  -s /etc/httpd/conf.d/authuser jerry   //-s:SHA加密
htpasswd  -s /etc/httpd/conf.d/authuser john

//3. 测试
httpd -t
service httpd reload
使用浏览器, curl, links测试
(3)基于组账号认证
(4)远程客户端和用户验证的控制
//1. 定义安全域
mkdir /app/admin
echo "/app/admin/index.html" > /app/admin/index.html
vim /etc/httpd/conf.d/auth.conf
<Directory /app/admin>
        AuthType basic
        AuthName "admin zone"
        AuthUserFile "/etc/httpd/conf.d/authuser"
        AuthGroupFile "/etc/httpd/conf.d/authgrp"
        Require group webgrp1 
        Order deny,allow
        allow from 192.168.136.230
        deny from 192.168.136
        Satisfy Any
</Directory>

//2. 定义组账号文件
/etc/httpd/conf.d/authgrp
webgrp1: tom jerry
webgrp2: john

//3. 测试
httpd -t
service httpd reload
使用浏览器, curl, links测试

(十三)虚拟主机

<VirtualHost IP:PORT>
    ServerName FQDN
    DocumentRoot "/path"
</VirtualHost>

建议:上述配置存放在独立的配置文件中

//1. 编辑独立配置文件
vim /etc/httpd/conf.d/virtualhost.conf
<VirtualHost 192.168.136.10:80>
        DocumentRoot "/app/website1"
</VirtualHost>
<VirtualHost 192.168.136.20:80>
        DocumentRoot "/app/website2"
</VirtualHost>
<VirtualHost 192.168.136.30:80>
        DocumentRoot "/app/website3"
</VirtualHost>

//2. 建立网页文件目录
mkdir /app/website{1..3}
echo "/app/website1/index.html" > /app/website1/index.html
echo "/app/website2/index.html" > /app/website2/index.html
echo "/app/website3/index.html" > /app/website3/index.html

//3. 建立相应的IP地址
ip a a 192.168.136.10 dev eth1
ip a a 192.168.136.20 dev eth1
ip a a 192.168.136.30 dev eth1

//4. 测试
service httpd reload
curl 192.168.136.10
curl 192.168.136.20
curl 192.168.136.30
//1. 编辑独立配置文件
vim /etc/httpd/conf.d/virtualhost.conf
Listen 81
Listen 82
Listen 83
<VirtualHost 192.168.136.40:81>
        DocumentRoot "/app/website1"
</VirtualHost>
<VirtualHost 192.168.136.40:82>
        DocumentRoot "/app/website2"
</VirtualHost>
<VirtualHost 192.168.136.40:83>
        DocumentRoot "/app/website3"
</VirtualHost>

//2. 建立相应的IP地址
service network restart     //清空上一个实验临时增加的IP
ip a a 192.168.136.40 dev eth1

//3. 测试
service httpd reload
curl 192.168.136.40:81
curl 192.168.136.40:82
curl 192.168.136.40:83
//1. 编辑独立配置文件
vim /etc/httpd/conf.d/virtualhost.conf
NameVirtualHost *:80
<VirtualHost *:80>
        ServerName www.hello.com
        DocumentRoot "/app/website1"
        CustomLog logs/www.hello.com-access_log common
</VirtualHost>
<VirtualHost *:80>
        ServerName www.hi.cn
        DocumentRoot "/app/website2"
        CustomLog logs/www.hi.cn-access_log common
</VirtualHost>
<VirtualHost *:80>
        ServerName www.bye.net
        DocumentRoot "/app/website3"
        CustomLog logs/www.bye.net-access_log common
</VirtualHost>

httpd -t
service httpd reload

//2. 配置DNS服务器或者编辑hosts文件

//3. 测试
curl www.hello.com
curl www.hi.cn
curl www.bye.net

(十四)status页面

LoadModule status_module modules/mod_status.so
<Location /server-status>
SetHandler server-status
Order allow,deny
Allow from 172.16     //只允许172.16.0.0网段主机访问status页面
</Location>
ExtendedStatus On 显示扩展信息
vim /etc/httpd/conf.d/status.conf
LoadModule status_module modules/mod_status.so    //默认在/etc/httpd/conf/httpd.conf文件中开启
<Location /status>
        SetHandler server-status
        Order allow,deny
        Allow from 192.168.136.
</Location>
ExtendedStatus On

httpd -t
service httpd reload

二、http协议

(一)http协议特点

(二)http报文

(三)method方法

(四)status(状态码)

(四)首部

三、curl和elinks工具

(一)curl工具

curl -A "Internet Explorer 12" 192.168.136.229
curl -e "www.baidu.com" 192.168.136.229
curl -H host: www.hello.com 192.168.136.229
curl -D head.txt 192.168.136.229
curl --limit-rate 2048 -O ftp://172.18.0.1/pub/ISOs/CentOS-7-x86_64-Everything-1708.iso
curl -I -L www.360buy.com 

-A选项:伪造浏览器和-e选项:伪造转发地址

-H选项:存在虚拟主机时,指定host首部信息返回不同结果

-D选项:保存相应报文首部信息到文件

--limit-rate选项:限制下载速度,单位B/s; -O 按照默认文件名存储

-L选项:强制重定向,下面的例子可以看到两次重定向跳转过程

(二)elinks工具:

上一篇下一篇

猜你喜欢

热点阅读