apache_技术研究报告

Apache_技术研究报告php

1.   Apache 启动

1.1 编译安装

1.   下载apr-1.5.0.tar.gzapr-util-1.5.3.tar.gzhttpd-2.4.7.tar.gzhtml

2.   解压apr-1.5.0.tar.gz包,进入apr-1.5.0文件夹,执行前端

./configure  --prefix=/usr/local/aprjava

makenode

make installlinux

3.   解压apr-util-1.5.3.tar.gz包,进入apr-util-1.5.3文件夹,执行web

./configure  --prefix=/usr/local/apr-util  --with-apr=/usr/local/aprapache

makewindows

make install后端

4.   解压httpd-2.4.7.tar.gz包,进入httpd-2.4.7文件夹,执行

./configure --prefix=/usr/local/apache2/ --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/  --with-pcre=/usr/local/pcre

make

make install

1.2 启动命令

  1. :进入安装目录的bin下,输入:

apachectl  start     #启动

apachectl  stop     #中止

apachectl  restart   #从新启动

apachectl  graceful #在不中断当前链接的状况下重启apache

  1. 若是apache安装成为linux的服务的话,能够用如下命令操做:

service httpd start     #启动

service httpd restart   #从新启动

service httpd stop     #中止服务

1.   Apache 监控

2.1 经过server-status查看监控信息

1)加载mod_status.so

Apache的状态管理的模块是LoadModule status_module modules/mod_status.so ,因此须要...apache2\conf'\httpd.conf配置文件中修改。

找到LoadModule status_module modules/mod_status.so ,去掉其前面的# 号(默认是没有# 号的)

2)下面有修改配置文件有两种方式:

方式一:在httpd.conf文件底部分添加如下内容:

<location/c-server-status>

         SetHandler server-status

         Order Deny,Allow

         Deny from nothing

         Allow from all

</location>

ExtendedStatus On

方法二:在httpd.conf中找到如下内存,修改http-info.conf

# Real-time info on requests and configuration

#Include conf/extra/httpd-info.conf   //去掉这一行前面的#

打开...apache2\conf\extra\http-info.conf 配置文件进行修改。修改内容与方式一相同。

其实,两种无方法是相同的,只是第二种方法更为优雅一点。

修改信息解释:

<location /c-server-status>  : c-server-status 这个名字能够任意的取,但最好不要被别人猜到。

Deny from   表示禁止的访问地址;nothing 表示没有禁止访问的地址。

Allow from   表示容许的地址访问;all 表示全部的地址均可以访问。

ExtendedStatus On   表示的是待会访问的时候能看到详细的请求信息,另外该设置仅能用于全局设置,不能在特定的虚拟主机中打开或关闭。启用扩展状态信息将会致使服务器运行效率下降。

3)重启apache

[root@localhost /]# cd /usr/local/apache/bin/

[root@localhost bin]# apachectl start  启动

[root@localhost bin]# apachectl stop  中止

4)访问

http://你的IP地址:端口/c-server-status

http://你的IP地址:端口/c-server-status ?refresh=N

N将表示访问状态页面能够每N秒自动刷新一次

5)显示结果:

Apache Server Status for localhost

Server Version: Apache/2.2.19 (Win64)

Server Built: May 28 2011 15:18:56

Current Time: Sunday, 11-Nov-2012 17:44:21 Öйú±ê׼ʱ¼ä

Restart Time: Sunday, 11-Nov-2012 17:36:28 Öйú±ê׼ʱ¼ä

Parent Server Generation: 1

Server uptime: 7 minutes 52 seconds

Total accesses: 0 - Total Traffic: 0 kB

0 requests/sec - 0 B/second -

1 requests currently being processed, 63 idle workers

______________________________________________________________W_

................................................................

 

Scoreboard Key:"_" Waiting for Connection, "S" Starting up, "R" Reading Request,"W" Sending Reply, "K" Keepalive (read), "D" DNS Lookup,"C" Closing connection, "L" Logging, "G" Gracefully finishing,"I" Idle cleanup of worker, "." Open slot with no current process

Srv    PID    Acc    M    SS    Req    Conn    Child    Slot    Client    VHost    Request

0-1    4140    0/0/0    W    0    287636364    0.0    0.00    0.00    127.0.0.1    192.168.0.100    GET /c-server-status HTTP/1.1

 

Srv    Child Server number - generation

PID    OS process ID

Acc    Number of accesses this connection / this child / this slot

M    Mode of operation

SS    Seconds since beginning of most recent request

Req    Milliseconds required to process most recent request

Conn    Kilobytes transferred this connection

Child    Megabytes transferred this child

Slot    Total megabytes transferred this slot

参数分析:

参数分析:

字段说明

Server Version       Apache 服务器的版本。

Server Built         Apache 服务器编译安装的时间。

Current Time        目前的系统时间。

Restart Time         Apache 从新启动的时间。

Parent Server Generation        Apache 父程序 (parent process) 的世代编号,就是 httpd 接收到 SIGHUP 而从新启动的次数。

Server uptime         Apache 启动后到如今通过的时间。

Total accesses         到目前为此 Apache 接收的联机数量及传输的数据量。

CPU Usage           目前 CPU 的使用情形。

_SWSS....            全部 Apache process 目前的状态。每个字符表示一个程序,最多能够显示 256 个程序的状态。

Scoreboard Key         上述状态的说明。如下为每个字符符号所表示的意义:

    * _:等待连结中。

    * S:启动中。

    * R:正在读取要求。

    * W:正在送出回应。

    * K:处于保持联机的状态。

    * D:正在查找DNS

    * C:正在关闭连结。

    * L:正在写入记录文件。

    * G:进入正常结束程序中。

    * I:处理闲置。

    * .:尚无此程序。

Srv        本程序与其父程序的世代编号。

PID        本程序的process id

Acc        分别表示本次联机、本程序所处理的存取次数。

M         该程序目前的状态。

CPU        该程序所耗用的CPU资源。

SS         距离上次处理要求的时间。

Req        最后一次处理要求所耗费的时间,以千分之一秒为单位。

Conn       本次联机所传送的数据量。

Child       由该子程序所传送的数据量。

Slot        由该 Slot 所传送的数据量。

Client       客户端的地址。

VHost       属于哪个虚拟主机或本主机的IP

Request     联机所提出的要求信息。

2.2linux下经过命令查看监控信息

1、用 ps 来看 httpd 进程数

# ps -ef | grep httpd | wc -l

用这个命令统计当前的httpd进程数,固然这结果包含 grep httpd 的进程输出,通常来讲实际进程数比输出结果少1Apache启动的时候,默认就起来几个进程,若是链接数多了,它就会生出更多的进程来处理请求。

2、用 netstat 来看当前的链接数

# netstat -ant | grep ":80 " | wc -l

链接数目并不等于httpd线程数目,固然链接数目越多,httpd进程数就有可能数会增多。上面的返回结果数目,有可能包括多种链接状态,好比 LISTENESTABLISHEDTIME_WAIT等等,能够加入状态关键字进一步过滤,获得想要的结果。

2.   Apache 负载均衡

3.1 proxy模式负载均衡策略

3.1.1按请求(byrequests

设置lbmethod = byrequests,可不写。

  1. 轮询方式

打开对应的模块,在httpd.conf文件中添加以下代码:

ProxyRequests Off

<Proxy balancer://mycluster/> 

    BalancerMember http://192.168.51.50:8080/

    BalancerMember http://192.168.51.106:8080/

</Proxy>

ProxyPass / balancer://mycluster/

  1. 权重方式(以请求数为权重)

  参数”loadfactor”表示后台服务器负载到由Apache发送请求的权值,该值默认为1,能够将该值设置为1100之间的任何值。

ProxyRequests Off

<Proxy balancer://mycluster/> 

    BalancerMember http://192.168.51.50:8080/ loadfactor=2

    BalancerMember http://192.168.51.106:8080/ loadfactor=1

</Proxy>

ProxyPass / balancer://mycluster/

 

以上面的配置为例,介绍如何实现按权重分配的负载均衡,现假设Apache收到http://myserver/aaa 4次这样的请求,该请求分别被负载到后台服务器,则有2次连续的这样请求被负载到BalancerMemberhttp://192.168.51.50:8080的服务器,有1次这样的请求被负载BalancerMemberhttp://192.168.51.106:8080后台服务器。实现了按照权重连续分配的均衡策略。

3.1.2 按流量(bytraffic

参数“lbmethod=bytraffic”表示后台服务器负载请求和响应的字节数,处理字节数的多少是以权值的方式来表示的。 loadfactor”表示后台服务器处理负载请求和响应字节数的权值,该值默认为1,能够将该值设置在1100的任何值。

ProxyRequests Off

ProxyPass /hello  balancer://mycluster/  lbmethod=bytraffic

#ProxyPassReverse /hello balancer://mycluster/

<Proxy balancer://mycluster> 

    BalancerMember http://192.168.51.50:8080 loadfactor=2

    BalancerMember http://192.168.51.106:8080 loadfactor=1

</Proxy>

3.1.2按照繁忙程度均衡(bybusyness

老是分配给活跃请求数最少的服务器

ProxyRequests Off

ProxyPass /hello  balancer://mycluster/  lbmethod= bybusyness

#ProxyPassReverse /hello balancer://mycluster/

<Proxy balancer://mycluster> 

    BalancerMember http://192.168.51.50:8080

    BalancerMember http://192.168.51.106:8080

    ProxySet lbmethod=bybusyness

</Proxy>

3.2 proxy模式热备份

热备份的实现很简单,只需添加 status=+H 属性,就能够把某台服务器指定为备份服务器,配置文件以下:

Listen 202.x.xx.xxx:80        (别人访问的IP和端口)

<VirtualHost 202.x.xx.xxx:80>

     ServerAdmin admin@bit.com

     ProxyRequests Off

     Proxypass / balancer://proxy/ 

 ProxySet lbmethod=bytraffic  (加上这句)

<Proxy balancer://proxy>

         Order Deny,Allow

         Allow from all

         BalancerMember http://10.0.0.1  

         BalancerMember http://10.0.0.2   status=+H

</Proxy>

</VirtualHost>

 

从配置中能够看出请求老是流向http://10.0.0.1,一旦http://10.0.0.1挂掉, Apache会检测到错误并把请求分流给 http://10.0.0.2Apache会每隔几分钟检测一下http://10.0.0.1的情况,若是http://10.0.0.1恢复,就继续使用http://10.0.0.1,这样就能够实现热备份了

2.3   mod_jk模式负载均衡策略

mod_jkASF的一个项目,是一个工做于apache端基于AJP协议与应用服务器通讯的链接器,它是apache的一个模块,是AJP协议的客户端(服务端是应用服务器的AJP链接器)。

比较经常使用的负载均衡模式为以下三种:

  1. 按请求(method =Request)

默认状况是按照请求负载

workers.properties配置以下

worker.list=loadBalancer

worker. server1.port=8009

worker. server1.host=100.100.10.22

worker. server1.type=ajp13

worker. server1.lbfactor=1

。。。。

worker. server2.port=8009

worker. server2.host=100.100.10.23

worker. server2.type=ajp13

worker. server2.lbfactor=1

。。。。

worker.loadBalancer.type=lb

worker.loadBalancer.balance_workers= server1, server2

worker.loadbalancer.method= Request

如上配置中,负载均衡策略是Request,按照请求个数进行负载,lbfactor是权重值,其范围是1-100

  1. 按流量(method= Traffic)

workers.properties配置以下

worker.list=loadBalancer

worker. server1.port=8009

worker. server1.host=100.100.10.22

worker. server1.type=ajp13

worker. server1.lbfactor=1

。。。。

worker. server2.port=8009

worker. server2.host=100.100.10.23

worker. server2.type=ajp13

worker. server2.lbfactor=1

。。。。

worker.loadBalancer.type=lb

worker.loadBalancer.balance_workers= server1, server2

worker.loadbalancer.method= Traffic

  1. 按繁忙程度均衡(method =Busyness)

配置以下:

worker.list=loadBalancer

worker. server1.port=8009

worker. server1.host=100.100.10.22

worker. server1.type=ajp13

。。。。

worker. server2.port=8009

worker. server2.host=100.100.10.23

worker. server2.type=ajp13

。。。。

worker.loadBalancer.type=lb

worker.loadBalancer.balance_workers= server1, server2

worker.loadbalancer.method= Busyness

 

经过查找apache官网,有5种均衡策略,以下:

1.   Request

2.   Session

3.   Next

4.   Traffic

5.   Busyness

Method参数的具体说明以下:

Specifies what method load balancer is using for electing the best worker. Please note, that session stickiness and perfect load balancing are conflicting targets, especially when the number of sessions is small, or the usage of sessions is extremely varying For huge numbers of sessions this usually is not a problem.

Some methods note, that they aggregate in a sliding time window. They add up accesses, and on each run of the global maintain method, the load counters get divided by 2. Usually this happens once a minute, depending on the setting of worker.maintain. The value of the load counters can be inspected using the status worker.

If method is set to R[equest] the balancer will use the number of requests to find the best worker. Accesses will be distributed according to the lbfactor in a sliding time window. This is the default value and should be working well for most applications.

If method is set to S[ession] the balancer will use the number of sessions to find the best worker. Accesses will be distributed according to the lbfactor in a sliding time window. This method should be used, if sessions are your limiting resource, e.g. when you only have limited memory and your sessions need a lot of memory. Because the balancer does not keep any state, it actually does not know the number of sessions. Instead it counts each request without a session cookie or URL encoding as a new session. This method will neither know, when a session is being invalidated, nor will it correct its load numbers according to session timeouts or worker failover. If you know request URLs, that will be called without a session ID but should not be counted as new sessions, you should add them to the stateless mapping rule extension or set the Apache HTTPD environment variable JK_STATELESS for them.

If method is set to N[ext] the balancer will again use the number of sessions to find the best worker. All remarks concerning the Sessionmethod apply as well. The difference to the Session method is how the session count is handled in the sliding time window. The Next method does not divide by 2, instead it subtracts the current minimum number. This should effectively result in a round-robin session balancing, thus the name Next. Under high load, the two session balancing methods will result in a similar distribution, but Next will be better if you need to distribute small numbers of sessions.

If set to T[raffic] the balancer will use the network traffic between JK and Tomcat to find the best worker. Accesses will be distributed according to the lbfactor in a sliding time window. This method should be used, if network to and from the backends is your limiting resource.

If set to B[usyness] the balancer will pick the worker with the lowest current load, based on how many requests the worker is currently serving. This number is divided by the workers lbfactor, and the lowest value (least busy) worker is picked. This method is especially interesting, if your request take a long time to process, like for a download application.

This feature has been added in version 1.2.9. The Session method has been added in version 1.2.20, the Next method in version 1.2.33.

3.   Apache正向代理和反向代理

4.1 正向代理

. 正向代理配置:

<VirtualHost *:80>

    ServerAdmin prograsliu@gmail.com

    DocumentRoot "D:/www/test"

    ServerName www.test.com

    ServerAlias test.com

    ErrorLog "logs/test.com-error.log"

    CustomLog "logs/test.com-access.log" common   

    Alias /sublook "D:/www/test/look/sublook/"

    <Directory "D:/www/test">

        Options FollowSymLinks

        AllowOverride All

        Order allow,deny

        Allow from all

    </Directory>

    #正向代理设置

    ProxyRequests On

    ProxyVia On

    <Proxy *>

        Order deny,allow

        Deny from all

        Allow from 127.0.0.1

    </Proxy>

</VirtualHost>

    ProxyRequests On:开启Apache正向代理

    ProxyVia On:控制位于代理服务器链中的代理请求的流向

引用Apache2.2官方文档中对ProxyVia的解释以下:

1.   若是设置为默认值Off ,将不会采起特殊的处理。若是一个请求或应答包含"Via:"头,将不进行任何修改而直接经过。

2.   若是设置为On每一个请求和应答都会对应当前主机获得一个"Via:"头。

3.   若是设置为Full ,每一个产生的"Via:"头中都会额外加入Apache服务器的版本,以"Via:"注释域出现。

4.   若是设置为Block ,每一个代理请求中的全部"Via:"头行都将被删除。且不会产生新的"Via:"头。

    <Proxy *>...</Proxy>:用来控制谁能够访问你的代理

. 浏览器设置:(以FireFox为例)

三.访问效果:

访问 www.sina.com,观察HTTP请求Response

能够看到,Viawww.test.com,正向代理成功了。

3.2   反向代理

反向代理配置以下:

Listen 202.xxx.xxx.xxx:80     (别人访问的IP和端口)
<VirtualHost 202.xxx.xxx.xxx:80> (
同上
)
ServerAdmin admin@bit.net
ProxyRequests Off            
(说明开启的是反向代理)

ProxyMaxForwards 100
ProxyPreserveHost On
ProxyPass / http://10.x.xx.xxx/
(转发到url上的请求)
ProxyPassReverse / http://10.x.xx.xxx/
 <Proxy *>                (
这段是访问的控制)
 Order Deny,Allow
 Allow from all
 </Proxy>
</VirtualHost>

而后反向代理就开启了

当别人输入http://202.xxx.xxx.xxx时就会经过反向代理转到http://10.x.xx.xxx上,这样简单的反向代理功能就开启了。

4.    Apache 会话保持

5.1 mod_jk负载均衡模式的会话保持

1.环境:

一台服务器装apache,做负载均衡器

地址100.100.11.110

操做系统:windows2003

另外两台服务器装应用服务器V9,做站点1和站点2

地址为:100.100.10.22 100.100.10.23

操做系统:window2003

2.安装软件:

apache_2.2.4

应用服务器V9

mod_jk-apache-2.2.4.so

jdk-1_5_0_06

3.配置:

3.1 apache服务器:

100.100.11.110上安装jdk,配置环境变量,执行java -version查看是否正常

100.100.11.110上安装apache

mod_jk-apache-2.2.4.so拷贝到/modules

编辑httpd.conf,在最后添加:Include conf/mod_jk.conf

conf目录下新建一个mod_jk.conf文件,内容以下:

# Load mod_jk module

LoadModule jk_module modules/mod_jk-apache-2.2.4.so

# Where to find workers.properties

JkWorkersFile conf/workers.properties

# Where to put jk logs

JkLogFile logs/mod_jk.log

# Set the jk log level [debug/error/info]

JkLogLevel info

# Select the log format

JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# JkOptions indicate to send SSL KEY SIZE,

JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format

JkRequestLogFormat "%w %V %T"

# Send JSPs for context / to worker named loadBalancer

JkMount /*.jsp loadBalancer

 

conf目录里新建一个名称为workers.properties的文件,内容以下:

worker.list=loadBalancer

worker.server1.port=8009

worker.server1.host=100.100.10.22

worker. server1.type=ajp13

worker. server1.lbfactor=1

#worker. server1.cachesize=60

#worker. server1.cache_timeout=300

worker. server1.socket_keepalive=1

worker. server1.socket_timeout=180

worker. server1.connect_timeout=0

worker. server1.reply_timeout=0

worker. server1.recovery_options=1

worker. server1.retries=1

worker. server2.port=8009

worker. server2.host=100.100.10.23

worker. server2.type=ajp13

worker. server2.lbfactor=1

#worker. server2.cachesize=60

#worker. server2.cache_timeout=300

worker. server2.socket_keepalive=1

worker. server2.socket_timeout=180

worker. server2.connect_timeout=0

worker. server2.reply_timeout=0

worker. server2.recovery_options=1

worker. server2.retries=1

worker.loadBalancer.type=lb

worker.loadBalancer.balance_workers=server1, server2

#worker.loadbalancer.method=Session

worker.loadBalancer.sticky_session=true

#worker.loadBalancer.sticky_session_force=false

worker.list=loadbalancer:设定工做的负载平衡器。

worker.<work_name>.lbfactor:负载平衡的权重比,若是此权重比越大,则分配到此节点的请求越多,如以上两个节点的权重比为1:1,则为平均分配。

worker.loadbalancer.balanced_workers= server1, server2:指定此负载平衡器负责的2个节点。

worker.loadbalancer.sticky_session =true:若是设为true1,则代表为会话粘性,不进行会话复制,当某用户的请求第一次分发到哪台server后,后继的请求会一直分发到此server服务器上处理;若是后端的各server之间支持session复制,则能够将此属性值设为false0。默认值为true

worker.loadbalancer.sticky_session_force:假设sticky_session设为true,用户会话具备了粘性,当当前server服务器中止服务后,若是sticky_session_forcetrue也就是强制会话与当前server关联,那么会报500错误;若是设为false则当前server出现故障时,请求会转移到其余的server上,此时会session丢失。

 

3.2 配置应用服务器V9:

须要注意的是,worker.propeties配置文件中hostporthost就是应用服务器所在主机的ip,端口就是应用服务器打开jk支持的Network Listener所监听的端口。应用服务器如何打开jk支持下文会讲述。

若是须要将某个域名的动态请求转发给jk,那么就在httpd.conf里加上:

NameVirtualHost *:80

<VirtualHost *:80>

ServerName  www.test.com

JkMount /* loadBalancer

</VirtualHost>

这样,www.test.com的请求都会转发到jk上,而且由jk转发到应用服务器节点上了(就是workers.properties配置的hostport上)。

下面配置应用服务器V9

经过浏览器进入应用服务器的管理工具界面,展开 配置管理->网络配置->网络监听,页面会列出现有的全部监听的端口。例如默认的http-listener1监听8080http-listener2监听8181admin-listener监听4848

点击listener1,进入listener1 的设置,勾选JK 监听复选框。

这样,应用服务器的8080端口就会处理jk发送过来的请求了。

应用服务器的jvm参数配置以下:

asadmin create-jvm-options --target cluster1 "-DjvmRoute=\${AJP_INSTANCE_NAME}"

asadmin create-jvm-options --target cluster1

"-Dcom.sun.enterprise.web.connector.enableJK=\${AJP_PORT}"

asadmin create-system-properties --target server1 AJP_INSTANCE_NAME=server1 

asadmin create-system-properties --target server1 AJP_PORT=8019

asadmin create-system-properties --target server2 AJP_INSTANCE_NAME=server2 

asadmin create-system-properties --target server2 AJP_PORT=8029

5.2 proxy模式的会话保持

基于用户的负载均衡: 该种方式下,当用户发出第一个request后,负载均衡器动态的把该用户分配到某个节点,并记录该节点的jvm路由,之后该用户的全部request都会被绑定这个jvm路由,用户只会与该server发生交互,这种策略被称为粘性session(session sticky)

优势是响应速度快,多个节点之间无须通讯。缺点也很明显,某个node死掉之后,它负责的全部用户都会丢失session

首先在apache conf目录下找到httpd.conf(apache 的配置文件)文件

在该文件里找到

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

LoadModule proxy_balancer_module modules/mod_proxy_balancer.so

LoadModule proxy_connect_module modules/mod_proxy_connect.so

LoadModule proxy_ftp_module modules/mod_proxy_ftp.so

LoadModule proxy_http_module modules/mod_proxy_http.so

而且把上面的#号去掉

而后找到

Include conf/extra/httpd-vhosts.conf

把上面的#号去掉

在最后一行加上

ProxyRequests Off

<proxy balancer://cluster>

BalancerMember http://192.168.10.11:8080 loadfactor=1 route=server1(与该应用服务器里的jvmRote名字同样)

BalancerMember http://192.168.10.12:8080 loadfactor=1 route=server2

</proxy>

第二步找到\conf\extra包下面的httpd-vhosts.conf

在下面加上

<VirtualHost *:80>

ServerAdmin yancheng100088@163.com

ServerName localhost

ServerAlias   localhost

ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=On

ProxyPassReverse / balancer://cluster/

ErrorLog "logs/dummy-host2.163.com-error.log"

    CustomLog "logs/dummy-host2.163.com-access.log" common

</VirtualHost>

 

 

第三步对应用服务器参数的配置

loong服务器上增长jvmRoute参数以下:

1.  asadmin create-jvm-options --target cluster1 "-DjvmRoute=\${INSTANCE_NAME}"

2. asadmin create-system-properties --target instance1 INSTANCE_NAME=server1

3. asadmin create-system-properties --target instance2 INSTANCE_NAME=server2

5.   用户受权和访问控制

访问控制权限步骤以下:

1、首先对httpd.conf文件进行设置以下:

<Directory /home/httpd/html>

# AllowOverride FileInfo AuthConfig Limit

# Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec

Options Includes FollowSymLinks Indexes

AllowOverride All //*注意AllowOverride 必定要设置为All,这样后面的.htaccess文件才会起

#做用容许存在于.htaccess文件中的指令类型(.htaccess文件名是能够改变的,其文件名由#AccessFileName指令决定) 
#None:
AllowOverride被设置为None时。不搜索该目录下的.htaccess文件(能够减少服务器开销)。
 
#All:
.htaccess文件中可使用全部的指令

<Limit GET POST OPTIONS PROPFIND>

Order allow,deny

Allow from all

</Limit>

# <Limit PUT DELETE PATCH PROPPATCH MKCOL COPY MOVE LOCK UNLOCK>

# Order deny,allow

# Deny from all

# </Limit>

</Directory>

#Order:控制在访问时AllowDeny两个访问规则哪一个优先: 
#Allow
:容许访问的主机列表(可用域名或子网,例如:Allow from 192.168.0.0/16)
 
#Deny
:拒绝访问的主机列表。

#指定配置存取控制权限的文件名称

AccessFileName .htaccess

2、建立.htaccess文件內容

要控制某目录的访问权限必须创建一访问控制文件,文件名前面指定的“.htaccess”,其内容格式以下:

AuthUserFile    用户账号密码文件名

AuthGroupFile   群组账号密码文件名

AuthName       画面提示文字

AuthType       验证方式

<Limit GET>

密码验证方式

</Limit>

用户验证方式AuthType目前提供了BasicDigest两种。 密码检验设定方法与httpd.conf中的相关设定相同。 具体例子以下:

AuthUserFile /etc/secure.user

AuthName 安全认证中心

AuthType Basic

<Limit GET>

require valid-user

</Limit>

3       创建用户密码文件

若是你是第一次建立用户密码,命令格式以下:

htpasswd -c 密码文件名 用户名称

在上面的例子中,咱们将用户密码文件放到了/etc/secure.user文件中,因此这里应按照以下进行操做:

htpasswd -c /etc/secure.user sword

程序会提示你输入两次用户的口令,而后用户密码文件就已经建立sword这个用户也添加完毕了。

若是要向密码文件中添加新的用户,按照以下命令格式进行操做:

htpasswd 密码文件 用户名称

这样,从新启动httpd后,进行该WEB目录时就会有一个对话框弹出,要求输入用户名及用户口令了。

4       如何减小访问控制对Apache性能的影响

频繁的使用访问控制会对Apache的性能产生较大的影响,那么,如何才能减小这种影响呢?最简单也是最有效的方法之一就是减小.htaccess文件的数目,这样能够避免Apache对每个请求都要按照.htaccess文件的内容进行受权检查。它不只在当前的目录中查找.htaccess文件,它还会在当前目录的父目录中查找。

/

/usr

/usr/local

/usr/local/etc

/usr/local/etc/httpd

/usr/local/etc/httpd/htdocs

/usr/local/etc/httpd/htdocs/docs

一般在根目录下没有htaccess文件,但Apache仍然会进行例行检查以肯定该文件确实不存在。这是影响很影响服务器工做效率的事情。下面的方法能够消除这个讨厌的过程:将AllowOverride选设置为None,这样Apache就会检查.htaccess文件了。将/根目录的 AllowOverride选项设为None,只将须要进行访问控制的目录下的AllowOverride选项设置为all,以下面的例子中将/根目录的 AllowOverride 选项关闭了,只打开了/usr/local/etc/httpd/htdocs目录下的AllowOerride选项,这样,系统就只在 /usr/local/etc/httpd/htdocs中检查.htaccess文件,达到的提升服务效率的目的。

<Directory />

AllowOverride None

</Directory>

<Directory /usr/local/etc/httpd/htdocs>

AllowOverride All

</Directory>

若是除了根目录之外,还有其它存放WWW文件的目录,你也能够采起一样的方法进行设置。好比:若是你使用UserDir来容许用户访问本身的目录,AllowOverride的设置以下:

<Directory /home/*/public_html>

AllowOverride FileInfo Indexes IncludesNOEXEC

</Directory>

5、防止用户访问指定的文件

系统中有一些文件是不适宜提供给WWW用户的,如:.htaccesshtpasswd*.pl等,能够用达到这个目的:

<Files .htaccess>

order allow,deny

deny from all

</Files>

用户访问控制三个.htaccess文件、.htpasswd.htgroup(用于用户受权) ,为了安全起见,应该防止用户浏览其中内容,能够在httpd.conf中加入如下内容阻止用户对其进行访问:

<Files ~”/.ht”>

Order deny, allow

Deny from all

</Files>

这样这三个文件就不会被用户访问了。

6、限制某些用户访问特定文件

能够对目录进行约束,要限制某些用户对某个特定文件的访问可使用,好比:不容许非domain.com域内的用户对/prices/internal.html进行访问,能够用以下的设置:

<Location /prices/internal.html>

order deny,allow

deny from all

allow from .domain.com

</Location>

若是你要授于相应权限的机器没有公开的域名,请在你的/etc/hosts文件中,将其IP地址映射到某个指定的名称,而后在Location中对其进行设置,不然该选项是不起做用的。

7、只接受来自特定连接的访问

例如,只让全部来自 http://www.cvicse.com.cn/* 的连接的用户进入此目录,由其它连接来的访客都不得进入; " * "表示此网站底下全部的连接。其中的 http://www.cvicse.com.cn/* 也能够是:http://192.168.2.23/* 或是指定文件 http://www.cvicse.com.cn/news.html

.htaccess文件的内容以下:

AuthUserFile /dev/null

AuthGroupFile /dev/null

AuthName ExampleAllowFromSpecificURL

AuthType Basic

<Limit GET>

order deny,allow

deny from all

referer allow from http://www.cvicse.com.cn/*

</Limit>

6.   性能优化

#服务端口,缺省是8080,建议将整个Apache配置调整好后再将服务端口改到正式服务的端口
Port 8080 => 80
#服务器名:缺省没有
ServerName name.example.com
#
最大服务进程数:根据服务容量预测设置
MaxClients 256 => 800
#
缺省启动服务后的服务进程数:等服务比较平稳后,按平均负载下的httpd个数设置就能够
StartServers 5 => 200
#
不要修改:
MinSpareServers 5 => 100
MaxSpareServers 10 => 200
#
特别修改:
#solaris或一些比较容易出现内存泄露的应用上:
MaxRequestsPerChild 0 =>3000

工做机制优化:

apache主要的优点就是对多处理器的支持更好,在编译时经过使用--with-mpm选项来决定apache的工做模式。若是知道当前的apache使用什么工做机制,能够经过httpd -l命令列出apache的全部模块,就能够知道其工做方式: 
prefork
:若是httpd -l列出prefork.c,则须要对下面的段进行配置:

<IfModule prefork.c> 

StartServers 5 #启动apache时启动的httpd进程个数。 

MinSpareServers 5 #服务器保持的最小空闲进程数。

MaxSpareServers 10 #服务器保持的最大空闲进程数。 

MaxClients 150 #最大并发链接数。

MaxRequestsPerChild 1000 #每一个子进程被请求服务多少次后被kill掉。0表示不限制,推荐设置为1000 

</IfModule> 

在该工做模式下,服务器启动后起动5httpd进程(加父进程共6个,经过ps -ax|grep httpd命令能够看到)。当有用户链接时,apache会使用一个空闲进程为该链接服务,同时父进程会fork一个子进程。直到内存中的空闲进程达到MaxSpareServers。该模式是为了兼容一些旧版本的程序。我缺省编译时的选项。

 

worker:若是httpd -l列出worker.c,则须要对下面的段进行配置: 

<IfModule worker.c> 

StartServers 2 #启动apache时启动的httpd进程个数。 

MaxClients 150 #最大并发链接数。 

MinSpareThreads 25 #服务器保持的最小空闲线程数。 

MaxSpareThreads 75 #服务器保持的最大空闲线程数。 

ThreadsPerChild 25 #每一个子进程的产生的线程数。 

MaxRequestsPerChild 0 #每一个子进程被请求服务多少次后被kill掉。0表示不限制,推荐设置为1000 

</IfModule> 

该模式是由线程来监听客户的链接。当有新客户链接时,由其中的一个空闲线程接受链接。服务器在启动时启动两个进程,每一个进程产生的线程数是固定的(ThreadsPerChild决定),所以启动时有50个线程。当50个线程不够用时,服务器自动fork一个进程,再产生25个线程。 

perchild:若是httpd -l列出perchild.c,则须要对下面的段进行配置:

<IfModule perchild.c> 

NumServers 5 #服务器启动时启动的子进程数 

StartThreads 5 #每一个子进程启动时启动的线程数 

MinSpareThreads 5 #内存中的最小空闲线程数 

MaxSpareThreads 10 #最大空闲线程数 

MaxThreadsPerChild 2000 #每一个线程最多被请求多少次后退出。0不受限制。 

MaxRequestsPerChild 10000 #每一个子进程服务多少次后被从新fork0表示不受限制。 

</IfModule> 

该模式下,子进程的数量是固定的,线程数不受限制。当客户端链接到服务器时,又空闲的线程提供服务。 若是空闲线程数不够,子进程自动产生线程来为新的链接服务。该模式用于多站点服务器。

 

应用模块和工具的安装配置优化:

因为使用模块动态加载的模式,因此能够方便的经过简单的配置调整来把Apache定制成你须要的:最好把不经常使用模块所有清除(不管处于安全仍是效率)。
好比:对于静态页面服务器:就什么其余子模块都不加载,对于PHP应用就加上PHP模块,对于JAVA应用就把Resin模块加载上。并且各类模块的插拔很是简单,这样调试过程当中就能够简单的经过注释掉不须要的模块,而不用从新编译。
通常说来,能够不须要的模块包括:
#LoadModule env_module libexec/mod_env.so
#LoadModule negotiation_module libexec/mod_negotiation.so
#LoadModule status_module libexec/mod_status.so
#server side include
已通过时了
#LoadModule includes_module libexec/mod_include.so
#
不须要将没有缺省index文件的目录下全部文件列出
#LoadModule autoindex_module libexec/mod_autoindex.so
#
尽可能不使用CGI:一直是Apache安全问题最多的地方
#LoadModule cgi_module libexec/mod_cgi.so
#LoadModule asis_module libexec/mod_asis.so
#LoadModule imap_module libexec/mod_imap.so
#LoadModule action_module libexec/mod_actions.so
#
不使用安全认证能够大大提升访问速度
#LoadModule access_module libexec/mod_access.so
#LoadModule auth_module libexec/mod_auth.so
#LoadModule setenvif_module libexec/mod_setenvif.so
最好保留的有:
#用于定制log格式
LoadModule config_log_module libexec/mod_log_config.so
#
用于增长文件应用的关联
LoadModule mime_module libexec/mod_mime.so
#
用于缺省index文件:index.php
LoadModule dir_module libexec/mod_dir.so
可用可不用的有:
#好比:须要在~/username/下调试php能够将
LoadModule userdir_module libexec/mod_userdir.so
#
好比:须要将之前的URL进行转向或者须要使用CGI script-alias
LoadModule alias_module libexec/mod_alias.so
经常使用的模块:

    最经常使用的可能就是phpJAVA应用服务器的前端,此外,从性能上讲利用mod_gzip能够减小40%左右的流量,减小机器用于传输的负载,而 mod_expires能够减小10%左右的重复请求,让重复的用户对指定的页面请求结果都CACHE在本地,根本不向服务器发出请求。

建议将全部MODULE的配置都放到相应模块的配置内部:some_module config

7.   Apache多应用配置

httpd.conf文件中作以下配置:

ProxyRequests Off

ProxyPass /helloworld  balancer://mycluster/helloWorld

ProxyPass /jspDS  balancer://myserver/jspDS

#ProxyPassReverse /hello balancer://mycluster/

<Proxy balancer://mycluster> 

    BalancerMember http://192.168.51.50:8080 loadfactor=1

    #ProxySet lbmethod=bybusyness

</Proxy>

<Proxy balancer://myserver> 

    BalancerMember http://192.168.51.106:8080 loadfactor=1

    #ProxySet lbmethod=bybusyness

</Proxy>

8.   mod_proxy VS  mod_jk

何时使用哪个呢?这依赖于你的架构。若是你已经有了或者须要apache 2.2的功能,那么你能够再mod_proxymod_jk直接选择。mod_jkapache2.2上容许得很好。关键看你须要什么样的功能:

mod_proxy

优点:

不须要编译和维护一个对立的模块。mod_proxy,mod_proxy_http,mod_proxy_ajp,mod_proxy_balancer已是apache 2.2+的标准集成部分;

可使用httphttpsAJP协议,即使是在同一个balancer中。

劣势:

mod_proxy_ajp不支持大于8k的数据包;

只有最基本的负载均衡器;

不支持域模型集群(domain model clustering

mod_jk

优点:

先进的负载均衡器;

先进的节点失败侦察功能;

支持大型AJP 数据包

劣势:

须要单独维护一个独立的模块

相关文章
相关标签/搜索