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

HTTP服务配置

2012-03-10 22:48 134 查看
Web服务:
http (超文本传输协议) 80/TCP
https 443/TCP

html:
http-->ascii 网页传输时转换为ASCII码传输
*.jpg *.mp3 转换为二进制文件,传输后需要特定的解码器才能还原
www,1995,http 0.9协议 诞生

将网页中的图片和文字和声音文件编码成文本协议才能传输!浏览器通过flash插件调用本地的图片和声音的应用程序才能显示图片和声音,浏览器本身只能显示超文本文件!

网页中的图片和数据是一个个object对象!

object: TCP ,每个请求需要三次握手
http:0.9 --> http 1.0(MIME) -- http 1.1(keepalive)
keepalive,为keepalive定义超时时间2秒钟,若2秒钟内没有请求,则断开与其连接。定义一次最大请求连接次数,若超过则断开连接,让其重新排队等待服务器响应

http:数据报报文格式
require请求
response响应

URI 统一资源标识符,说明在互联网上怎样标识资源
URL 统一资源定位符 protocol://server:port/path/to/file eg:http://www.magedu.com:80/logo.gif
URN 统一资源

http的方法:
GET 获取资源数据
POST 向服务器传输数据并要求服务器处理
HEAD 只看资源是否存在,获取头部信息即可
PUT 向指定资源位置上传最新内容
TRACE
OPTIONS
DELETE 删除指定资源
CONNECT

HTTP协议响应码:
1xx:信息
2xx:正常响应信息
3xx:重定向
4xx;客户端错误
5xx:服务器端错误

web服务器软件:
apache (httpd --> a patchy server -->apache(ASF:project,httpd)) 稳定
iis 应用程序服务
nginx 轻量级,静态响应快,时间响应机制巨快,响应请求消耗资源少
lighttpd 轻量级,配置文件语法简单易懂,性能出众

优化方案:
nginx前端接待用户请求,然后传至apache响应,此时apache 做反向代理服务器

具有交互能力的称为动态页面
CGI:comman gateway interface 通用网关接口

perl脚本比bash脚本更强大
php 面向对象,
jsp :java server page,与php相比性能好
jvm:java vmware(java 虚拟机)

应用一个动态网页,需要一个cgi接口进行交互

CGI响应方式:
apche接受一个用户请求时会立刻生成一个进程,如果用户请求一个动态页面则还需要再生成一个子进程!apache 中每一个用户的进程都是独立的!

模块化的响应方式:
apche接受一个用户请求时会立刻生成一个进程,如果用户请求一个动态页面则在这个进程中响应,把这个进程当成一个模块响应,当下一个用户申请同样的进程时,还调用这个模块

FASTCGI响应机制;
事先由服务器创建空闲进程等待响应,当接受一个用户请求时,则通过一个接口响应用户的请求在一个空闲进程中,当用户请求结束后服务器则结束这个进程!

JSP:
websphere (IBM)
weblogic (Oracle)
jboss
Tomcat

php:
apache ,php

asp ,asp.net
IIS

BBS:
app

RDBMS:关系型数据库管理系统
Oracle
mysql
sql server
NOSQL:非关系型数据库(处理海量数据)
mongoDB
IN-memory DB:内存数据库

LAMP:linux apache mysql php
LNMP:linux nginx mysql php
http://httpd.apache.org
apache 采用模块化设计;

安装http:
===============================================
[root@stu33 ~]# yum list all http*
Loaded plugins: rhnplugin
This system is not registered with RHN.
RHN support will be disabled.
Installed Packages
httpd.i386 2.2.3-31.el5 installed
Available Packages
httpd-devel.i386 2.2.3-31.el5 Server
httpd-manual.i386 2.2.3-31.el5 Server

由于selinux 限制,安装之前先关闭selinux
[root@stu33 ~]# getenforce
Enforcing
[root@stu33 ~]# setenforce 0 #临时关闭selinux
# vim /etc/sysconfig/selinux
修改内容为SELINUX=permissive #永久关闭selinux

[root@stu33 ~]# rpm -qc httpd #查看生成的文件
/etc/httpd/conf.d/proxy_ajp.conf
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/magic
/etc/logrotate.d/httpd
/etc/rc.d/init.d/httpd
/etc/sysconfig/httpd
.....

[root@stu33 ~]# rpm -ql httpd | grep ".*bin.*"
/usr/bin/ab
/usr/bin/htdbm
/usr/bin/htdigest
/usr/bin/htpasswd
....

[root@stu33 ~]# rpm -ql httpd | grep "www"
/var/www
/var/www/html #默认存放网页的位置
/var/www/icons
/var/www/icons/README
.....

MPM:多路处理模块
prefork
每一个请求用一个进程来响应
稳定性好、安全性好
worker
每一个请求用一个线程来响应,把一个进程生成一堆线程来响应
轻量级创建和销毁,资源消耗低

当多个用户连接进来时,怎样去响应连接请求的称为多路处理模块

[root@stu33 ~]# service httpd start #启动httpd服务
Starting httpd: httpd: apr_sockaddr_info_get() failed for stu33.magedu.com.localdomain
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName [OK]

#此时会报错,因为此时会解析主机,没有影响忽略此时错误

#yum install httpd-manual #安装httpd帮助文档
*************************************************
# vim /etc/httpd/conf/httpd.conf 配置文件
ServerTokens OS
ServerRoot "/etc/httpd"
PidFile run/httpd.pid
Timeout 120
KeepAlive Off #off状态对服务器性能有好处,on对用户请求体验好
MaxKeepAliveRequests 100
KeepAliveTimeout 15
# #prefork MPM 下面的是prefork MPM定义
<IfModule prefork.c>
StartServers 8 #启动进程数
MinSpareServers 5 #最小空闲进程数
MaxSpareServers 20 #最多空闲进程数
ServerLimit 256 # 定义maxclient超出上限
MaxClients 256 #最大进程数
MaxRequestsPerChild 4000 #定义一个进程最多响应几个请求
# #worker MPM 下面是 worker MPM定义
<IfModule worker.c>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0 #0表示不作限制
</IfModule>

Listen 80
Listen 12.34.56.78:80 #表示监听12.34.56.78的80端口,可以自定义监听IP的80端口

# Example: 下面的是加载模块
# LoadModule foo_module modules/mod_foo.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule auth_digest_module modules/mod_auth_digest.so
LoadModule authn_file_module modules/mod_authn_file.so
#
。。。。。

Include conf.d/*.conf
#ExtendedStatus On #显示状态信息 ,默认不启用

User apache
Group apache #指定服务的用户和组
ServerAdmin root@localhost
#ServerName www.example.com:80 #这里就是上面报错的原因,启用后修改为本机的主机名
DocumentRoot "/var/www/html" #网页文件保存的位置
<Directory /> #定义目录的访问属性
Options FollowSymLinks
AllowOverride None
</Directory>

<Directory "/var/www/html"> #下面定义网页文件的目录属性
Options Indexes FollowSymLinks #允许创建软链接
Options Indexes SymLinksifOwnerMatch #允许用户匹配的才能创建链接
Options Indexes ExecCGI #允许使用CGI接口
Options Indexes MultiViews #允许多视图
==============================================
AllowOverride AuthConfig/none #定义基于认证的允许
AuthName “wanggzhen” #认证名称
AuthType Basic #认证类型,还可以有MD5认证这里使用最简单的方式basic
AuthUserFile /etc/httpd/conf/.htpasswd #建立需要认证的用户的密码的配置文件我们自己建立
#htpasswd
用法:htpasswd [ -c ] [ -m ] [ -D ] passwdfile username
-c Create the passwdfile.
-m Use MD5 encryption for passwords.
-D Delete user.
#htpasswd -cm /etc/httpd/conf/.htpasswd gentoo
#htpasswd -c /etc/httpd/conf/.htpasswd centos #这里不需要再创建密码文件,第一次需要创建密码文件

[root@stu33 ~]# htpasswd -D /etc/httpd/conf/.htpasswd wz
Deleting password for user wz
AuthGroupFile /etc/httpd/conf/.htgroup #建立需要认证的用户组的密码文件
Require user gentoo #认证的用户gentoo
Require group developers 需要认证的组develpers
============================================
Order allow,deny #基于IP的ACL
Allow from all #接受所有
dengy from 192.168.0.38 #拒绝此ip的访问,,靠后的优先级高
</Directory>

<IfModule mod_userdir.c>
UserDir disable

#UserDir public_html #若启用这一项则下面给的注释符需要去掉
</IfModule>
#<Directory /home/*/public_html>
# AllowOverride FileInfo AuthConfig Limit
# Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
# <Limit GET POST OPTIONS>
# Order allow,deny
# Allow from all
# </Limit>
# <LimitExcept GET POST OPTIONS>
# Order deny,allow
# Deny from all
# </LimitExcept>
#</Directory>

DirectoryIndex index.html index.html.var
AccessFileName .htaccess #打算对某个目录的访问控制,则在那个目录中创建这个.htaccess文件,文件内容为:
AuthName "wangzhen"
AuthType Basic
AuthUserFile /home/wangzhen/.htpasswd
Require user centos

然后#htpasswd -cm /home/wangzhen/.htpasswd centos

<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>

TypesConfig /etc/mime.types
HostnameLookups Off #主机名解析可以关闭
ErrorLog logs/error_log #错误日志路径
LogLevel warn #定义错误日志级别

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
%b相应信息的大小 %{Referer}i引用 %{User-Agent}i用户请求
%h远端主机的IP地址 %l远程用户的用户名 %t时间 %r请求信息的第一行 %>状态码

#CustomLog logs/access_log common #访问日志,默认没有启用
CustomLog logs/access_log combined #访问日志的格式
ServerSignature On #服务器签署
Alias /error/ "/var/www/error/" #为/error/定义别名/var/www/error
AddDefaultCharset UTF-8 #默认使用的是UTF-8可能会导致中文乱码,可以注释掉
************************************************

#ps aux | grep "httpd" #显示与httpd有关的进程的信息

apache benchmark #对apache压力测试
ab:
-n 模拟要发起的请求数
-c 模拟多少个用户并发发起请求
eg:ab -n 1000 -c 2000 http://172.16.33.1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息