httpd2.4常用配置

author:JevonWei
版权声明:原创作品


httpd 2.4配置

  • 切换使用的MPM

      Centos7:/etc/httpd/conf.modules.d/00-mpm.conf
      启用要启用的MPM相关的LoadModule指令即可
    	centos 6编译安装:
      vim /etc/httpd24/httpd.conf
          Include /etc/httpd24/extra/httpd-mpm.conf
          LoadModule mpm_event_module
          modules/mod_mpm_event.so
    示例:
      vim /etc/httpd/conf.modules.d/00-mpm.conf
          LoadModule mpm_event_module modules/mod_mpm_event.so \\启用event工作模式。并关闭原来的perfork工作模式
      
      systemctl restart httpd
    
      另一台主机测试访问速度
          ab -c 100 -n 1000 http://172.16.253.105/
    
  • 主目录 (修改主目录时,还需打开目录的访问权限,默认权限是关闭的)

      DocumentRoot /path
    
      示例:
      vim /etc/httpd/conf/httpd.conf
          DocumentRoot "/app/web1"
          <DocumentRoot "/app/web1"> \\打开访问权限允许所有人访问
              require all granted
          </Directory>
    
  • 基于IP的访问控制:

      依赖模块mod_authz_core
    

    官方文档:http://httpd.apache.org/docs/2.4/de/mod/mod_authz_core.html

      无明确授权的目录,默认拒绝
      允许所有主机访问:Require all granted
      拒绝所有主机访问:Require all denied
      控制特定的IP访问:
      	Require ip IPADDR:授权指定来源的IP访问
      	Require not ip IPADDR:拒绝特定的IP访问
      控制特定的主机访问:
      	Require host HOSTNAME:授权特定主机访问
      	Require not host HOSTNAME:拒绝
      	HOSTNAME:
          	FQDN:特定主机
          	domin.tld:指定域名下的所有主机
                  
      不能有失败,至少有一个成功匹配
      	<RequireAll>
          	Require all granted
          	Require not ip 172.16.100.2  拒绝特定IP
      	</RequireAll>
      多个语句有一个成功,即成功
      	<RequireAny>
          	……
      	</RequireAny>
    
      示例 (仅允许某主机或host访问时必须放在<RequireAll>中) 
      <Directory "/app/web1">
          <RequireAll>
           require ip 192.168.198.1 \\仅允许192.168.198.1访问
          </RequireAll>
      </Directory>
      
      <Directory "/app/web1">
          <RequireAll>
          require all granted \\允许所有主机访问
           require not ip 192.168.198.1 \\但拒绝192.168.198.1访问 \\仅允许192.168.198.1访问
          </RequireAll>
      </Directory>
    
  • 虚拟主机

      基于FQDN的虚拟主机也不再需要	NameVirutalHost指令
      <VirtualHost *:80>
          ServerName www.b.net
          DocumentRoot "/apps/b.net/htdocs"
          ErrorLog logs/web1_error_log \\虚拟主机web服务的错误日志记录
          CustomLog  logs/web1_access_log common \\\\虚拟主机web服务的访问日志记录
          <Directory "/apps/b.net/htdocs">
              Options None
              AllowOverride None
              Require all granted
          </Directory>
              </VirtualHost>
    

    注意:任意目录下的页面只有显式授权才能被访问

      示例 (两个基于FQDN的虚拟主机)
      	vim /etc/httpd/conf.d/vhost.conf
          	<Virtualhost *:80>
          	DocumentRoot /app/web1
          	servername www.danran.com
              	<Directory /app/web1>
                  	require all granted
              	</Directory>
          	</Virtualhost>
    
    
          	<Virtualhost *:80>
          	DocumentRoot /app/web2
          	servername www.jevon.com
              	<Directory /app/web2>
                 	 	require all granted
              	</Directory>
          	</Virtualhost>
      访问:(有域名解析的前提)
      	http://www.danran.com
      	http://www.jevon.com
    
  • ssl:安装mod_ssl,和httpd-2.2 相同配置(也可以使用CA证书同httpd2.2一样)

      yum -y install mod_ssl
      ll /etc/httpd/conf.modules.d/00-ssl.conf   \\生成mod_ssl配置文件
      https://192.168.198.124
    
  • 持久连接

      官方文档:http://httpd.apache.org/docs/2.4/de/mod/core.html#keepalive
    
      KeepAlive on
      KeepAliveTimeout #ms
      MaxKeepAliveRequests 100
      毫秒级持久连接时长定义
    
      示例:
      	vim /etc/httpd/conf.d/vhost.conf
          	<Virtualhost *:80>
          	DocumentRoot /app/web1
          	servername www.danran.com
          	KeepAlive on
          	KeepAliveTimeout 15000 \\毫秒为单位
          	MaxKeepAliveRequests 100
          	<Directory /app/web1>
              	require all granted
          	</Directory>
          	</Virtualhost>
    
  • Sendfile机制

      不用sendfile的传统网络传输过程:
      	read(file,tmp_buf,len)
      	write(socket,tmp_buf,len)
    
      硬盘 >> kernel buffer >> user buffer >> kernel socket buffer >>  协议栈
    
      一般网络应用通过读硬盘数据,写数据到socket来完成网络传输,底层执行过程:
      	1. 系统调用read()从产生一个上下文切换:从user mode切换到kernel mode,然后DMA个执行拷贝,把文件数据从硬盘读到一个 kernel buffer  里。
      	2. 数据从kernel buffer到拷贝到user buffer用,然后系统调用read()返回,这时又产生一个上下文切换:从kernel mode切换到user mode
      	3. 系统调用write()从产生一个上下文切换:从user mode切换到kernel mode,然后把步骤2读到user buffer到的数据拷贝到kernel buffer(数据第2次拷贝到kernel buffer的),不过这次是个不同的kernel buffer个,这个buffer和socket相关联。
      	4. 系统调用write()从返回,产生一个上下文切换:从kernel mode到切换到user mode(第4次切换), 然后DMA从 从kernel buffer拷贝数据到协议栈(第4 次拷贝)
      	上面4个步骤有4次上下文切换,有4次拷贝,如果能减少切换次数和拷贝次数将会有效提升性能
    
      在kernel2.0+用版本中,系统调用sendfile()就是用来简化上面步骤提升性能的。sendfile()不但能减少切换次数而且还能减少拷贝次数
    
      用sendfile()来进行网络传输的过程:
      	sendfile(socket, file, len);
      	硬盘 >> kernel buffer ( 快速拷贝到kernel socket buffer)协议栈
      1 系统调用sendfile()通过DMA到把硬盘数据拷贝到kernel buffer被 ,然后数据被kernel与直接拷贝到另外一个与socket相关的kernel buffer有 。这里没有user mode和kernel mode  之间的在切换,在kernel个中直接完成了从一个buffer到另一个buffer的拷贝。
      2 DMA把数据从kernel buffer直接拷贝给协议栈,没有切换,也不需要数据从user mode拷贝到kernel mode,因为数据就在kernel里
    
  • 反向代理服务器

      启用反向代理
      	ProxyPass "/" "http://www.danran.com/" \\将访问web时自动调转到http://www.danran.com/
      	ProxyPassReverse "/" "http://www.danran.com/"
    

    特定URL反向代理
    ProxyPass "/danran" "http://www.danran.com/" \访问danran目录时跳转
    ProxyPassReverse "/danran" http://www.danran.com/

      示例:
      	vim /etc/httpd/conf.d/vhost.conf
          	<Virtualhost *:80>
              	DocumentRoot /app/web2
              	servername www.jevon.com
              	ProxyPass / http://172.16.251.4/
              	ProxyPassReverse / http://172.16.251.4
              	<Directory /app/web2>
                  	require all granted
              	</Directory>
          	</Virtualhost>
          
          	vim /etc/httpd/conf.d/vhost.conf
          	<Virtualhost *:80>
              	DocumentRoot /app/web2
              	servername www.jevon.com
              	ProxyPass /danran http://172.16.251.4/danran
              	ProxyPassReverse /danran http://172.16.251.4/danran
              	<Directory /app/web2>
                  require all granted
              	</Directory>
          	</Virtualhost>
      
      	访问测试:
          	[root@danran ~]# curl http://172.16.251.4
          	welcome danran
          
          	[root@danran ~]# curl http://www.jevon.com
          	welcome danran
    

其他常用配置同 httpd2.2一致

	httpd2.2常用配置
	http://119.23.52.191/httpd%E5%B8%B8%E7%94%A8%E9%85%8D%E7%BD%AE/
posted @ 2017-08-04 22:07  JevonWei  阅读(334)  评论(0编辑  收藏  举报