httpd常用配置

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


  • 检查配置文件时,如下提示,则因为没有server的服务名称导致,故设置网站的服务server名称,若没有设置web服务名,主默认解析系统主机名(添加主机名解析)

    [root@danran html]# httpd -t
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using danran.com. Set the 'ServerName' directive globally to suppress this message
    Syntax OK

    在/etc/httpd/conf/httpd.conf设置servername名称
    vim /etc/httpd/conf/httpd.conf
    #ServerName www.example.com:80

    或 在主机名解析文件中,添加主机名解析
    [root@danran html]# cat /etc/hosts
    127.0.0.1 danran.com

  • 显示服务器版本信息

    ServerTokens Major|Minor|Min[imal]|Prod[uctOnly]|OS|Full \显示web服务的头部信息
    ServerTokens Prod[uctOnly]:Server: Apache
    ServerTokens Major: Server: Apache/2
    ServerTokens Minor: Server: Apache/2.0
    ServerTokens Min[imal]: Server: Apache/2.0.41
    ServerTokens OS: Server: Apache/2.0.41 (Unix)
    ServerTokens Full (or not specified): Server: Apache/2.0.41(Unix) PHP/4.2.2 MyMod/1.2
    This setting applies to the entire server and cannot be enabled or disabled on a virtualhost-by-virtualhost basis.
    After version 2.0.44, this directive also controls the information presented by the
    ServerSignature directive.

    建议使用:ServerTokens Prod

    示例
    [root@danran mysql]# curl -I 192.168.198.134
    HTTP/1.1 200 OK
    Date: Mon, 31 Jul 2017 04:30:38 GMT
    Server: Apache/2.4.6 (CentOS) \显示web服务的版本信息
    Last-Modified: Mon, 31 Jul 2017 03:02:48 GMT
    ETag: "b-55594440a26a5"
    Accept-Ranges: bytes
    Content-Length: 11
    Content-Type: text/html; charset=UTF-8

      [root@danran conf]# vim /etc/httpd/conf/httpd.conf 
      
          ServerTokens prod
      [root@danran conf]# service httpd restart
      
      [root@danran conf]# curl -I 192.168.198.136
      HTTP/1.1 403 Forbidden
      Date: Sun, 02 Jul 2017 10:49:03 GMT
      Server: Apache
      Accept-Ranges: bytes
      Content-Length: 4961
      Connection: close
      Content-Type: text/html; charset=UTF-8
    
  • 修改监听的IP和Port

    Listen [IP:]PORT
    (1) 省略IP表示为0.0.0.0;
    (2) Listen指令可重复出现多次
    Listen 80
    Listen 8080
    Listen 172.168.198.134:80 \监听IP为172.168.198.134的80端口
    (3) 修改监听socket,重启服务进程方可生效
    service httpd restart

  • 持久连接

    KeepAlive On|Off \允许/禁止持久连接
    KeepAliveTimeout 15 \最长等待连接时间
    MaxKeepAliveRequests 100 \最大连接请求次数

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

    prefork, worker, event(试验阶段)
    httpd-2.2不支持同时编译多个模块,所以只能编译时选定一个;rpm安装的包提供三个二进制程序文件,分别用于实现对不同MPM机制的支持
    确认方法:
    ps aux | grep httpd
    默认为/usr/sbin/httpd,即prefork 模式

    [root@danran mysql]# vim /etc/sysconfig/httpd
    HTTPD=/usr/sbin/httpd.worker

    查看静态编译的模块
    httpd.work -l
    查看静态编译及动态装载的模块
    httpd.work –M

    prefork的默认配置:

    StartServers 8 \开启启动开启的进程
    MinSpareServers 5 \最小的空闲进程个数
    MaxSpareServers 20 \最大的空闲进程数
    ServerLimit 256 \最多进程数, 最大20000
    MaxClients 256 \最大并发
    MaxRequestsPerChild 4000 \子进程最多能处理的请求数量。在处理MaxRequestsPerChild个请求之后, 子进程将会被父进程终止,这时候子进程占用的内存就会释放(为0时永远不释放)

      worker的默认配置:
          <IfModule worker.c>
          StartServers 4  \\开启自启进程数为4
          MaxClients 300 \\最大并发数
          MinSpareThreads 25  \\最小空闲线程个数
          MaxSpareThreads 75 \\最大空闲线程
          ThreadsPerChild 25 \\每个进程开启的线程数
          MaxRequestsPerChild 0  \\无限制
          </IfModule>
    
  • DSO:Dynamic Shared Object
    加载动态模块配置

    /etc/httpd/conf/httpd.conf
    配置指定实现模块加载格式:
    LoadModule <mod_name> <mod_path>
    模块文件路径可使用相对路径:
    相对于ServerRoot(默认/etc/httpd) )
    示例:
    LoadModule auth_basic_module modules/mod_auth_basic.so

  • 定义 'Main' server的文档页面路径

      DocumentRoot “/path”
      文档路径映射:
      DocumentRoot 指向的路径为URL路径的起始位置
      
      示例:
          DocumentRoot "/app/data“
          http://HOST:PORT/test/index.html --> /app/data/test/index.html
          注意:SELinux和iptables的状态
    
  • 定义默认站点主页面

    DirectoryIndex index.html index.html.var \当web目录下没有主页文件index.html时,默认显示页面文件为index.html.var页面,存放路径为/var/www/error/noindex.html,通过文件/etc/httpd/conf.d/welcome.conf文件配置

  • 站点访问控制常见机制

    可基于两种机制指明对哪些资源进行何种访问控制访问控制机制有两种:客户端来源地址,用户账号
    文件系统路径:
    <Directory “/path">
    ...

    <File “/path/file”>
    ...

    <FileMatch "PATTERN">
    ...

      URL路径:
          <Location "">
          ...
          </Location>
          <LocationMatch "">
          ...
          </LocationMatch>
      示例:
          <FilesMatch "\.(gif|jpe?g|png)$">
          <Files “?at.*”> 通配符
          <Location /status>
          <LocationMatch "/(extra|special)/data">
    
  • 中“基于源地址”实现访问控制

    (1) Options:后跟1个或多个以空白字符分隔的选项列表
    在选项前的+,-表示增加或删除指定选项
    常见选项:
    Indexes:指明的URL路径下不存在与定义的主页面资源相符的资源文件时,返回索引列表给用户,index默认关闭
    FollowSymLinks:允许访问符号链接文件所指向的源文件,FollowSymLinks默认开启
    None:全部禁用
    All:全部允许
    注意:index选项默认关闭,FollowSymLinks默认开启

      示例:
          <Directory "/web/docs">                    
              Options Indexes FollowSymLinks
          </Directory>
          
          <Directory /web/docs/spec>  \\子目录的设置不受父目录的影响,可单独设置独立生效
              Options FollowSymLinks
          </Directory>
    
          <Directory /web/docs>
              Options Indexes FollowSymLinks
          </Directory>
          
          <Directory /web/docs/spec>
              Options +Includes -Indexes
          </Directory>
    

    (2) AllowOverride
    与访问控制相关的哪些指令可以放在指定目录下的.htaccess(由AccessFileName 指定)文件中,覆盖之前的配置指令
    只对语句有效
    AllowOverride All:所有指令都有效
    AllowOverride None:.htaccess 文件无效
    AllowOverride AuthConfig Indexes 除了AuthConfig和Indexes的其它指令都无法覆盖
    示例:
    vim /etc/httpd/conf/httpd.conf
    AccessFileName .htaccess

              DocumentRoot "/var/www/html"
              <Directory />
                  AllowOverride All
              </Directory>
          
          [root@danran error]# vim /var/www/html/.htaccess   
              option indexes followSymlinks  \\"/var/www/html"目录的访问权限以此设置生效 
    

    (3) order和allow、deny
    order:定义生效次序;写在后面的表示默认法则
    Order allow,deny
    Order deny,allow
    Allow from, Deny from
    来源地址:
    IP
    网络:
    172.16
    172.16.0.0
    172.16.0.0/16
    172.16.0.0/255.255.0.0

      示例:
          <files "*.txt">
          order deny,allow
          deny from 172.16. 100.100
          allow from 172.16
          </files>
    
          <files "*.txt">
          order allow,deny
          deny from 172.16.100.100
          allow from 172.16
          </files>
      
          DocumentRoot "/app/web"
          <Directory />
              order allow,deny \\deny覆盖allow,最后的生效
              deny from 192.168.198.134 \\拒绝134的访问
              allow from 192.168.198.134 \\运行134访问
          </Directory>
    
  • 日志设定

    日志类型:
    访问日志
    错误日志
    错误日志:
    ErrorLog logs/error_log
    LogLevel warn
    loglevel 可选值:
    debug,info,notice,warn,error crit,alert,emerg

    日志记录等级:(/etc/httpd/conf/httpd.conf)
    LogLevel warn

    访问日志:(/etc/httpd/conf/httpd.conf)
    定义日志格式:LogFormat format strings
    LogFormat "%h %l %u %t "%r" %>s %b
    "%{Referer}i" "%{User-Agent}i"" cdanran
    使用日志格式:
    CustomLog logs/access_log danran
    参考帮助:
    http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats
    %h 客户端IP地址
    %l 远程用户,启用mod_ident才有效,通常为减号“-” ”
    %u 验证(basic,digest)远程用户,非登录访问时,为一个减号“-”
    %t 服务器收到请求时的时间
    %r First line of request,即表示请求报文的首行;记录了此次请求的“方法”,“URL”以及协议版本
    %>s 响应状态码
    %b 响应报文的大小,单位是字节;不包括响应报文http首部
    %{Referer}i 请求报文中首部“referer” 的值;即从哪个页面中的超链接跳转至当前页面的
    %{User-Agent}i 请求报文中首部“User-Agent”的值;即发出请求的应用程序

  • 设定默认字符集

    AddDefaultCharset UTF-8

    中文字符集:GBK,GB2312, GB18030

  • 定义路径别名

    格式:Alias /URL/ "/PATH/"
    DocumentRoot "/www/htdocs"
    http://www.magedu.com/download/bash.rpm ==>/www/htdocs/download/bash.rpm

      Alias /download/ "/rpms/pub/"
          http://www.magedu.com/download/bash.rpm==>/rpms/pub/bash.rpm
          http://www.magedu.com/images/logo.png==>/www/htdocs/images/logo.png
    

    示例:
    alias "/download" "/app/download/" \/app/download/原路径必须有/,若/app/download则不能正确访问

      正则表达式匹配
      AliasMatch ^/danran/(.*)$ /app/danran$1  \\通过访问danran目录下的文件则将对应/app/danran$1对应的文件
    
  • 基于用户的访问控制

    认证质询:WWW-Authenticate:响应码为401,拒绝客户端请求,并说明要求客户端提供账号和密码
    认证:Authorization:客户端用户填入账号和密码后再次发送请求报文;认证通过时,则服务器发送响应的资源

    认证方式两种:
    basic:明文
    digest:消息摘要认证,兼容性差
    安全域:需要用户认证后方能访问的路径;应该通过名称对其进行标识,以便于告知用户认证的原因
    用户的账号和密码
    虚拟账号:仅用于访问某服务时用到的认证标识
    存储:文本文件,SQL数据库,ldap目录存储,nis等

    basic认证配置示例:
    (1) 定义安全域
    <Directory “/path">
    Options None
    AllowOverride None \启用.htaccess文件编辑配置信息
    AuthType Basic \验证方式
    AuthName "String“ \验证信息描述
    AuthUserFile "/PATH/HTTPD_USER_PASSWD_FILE" \认证文件路径
    Require user username1 username2 ... \允许文件的指定用户访问
    Require valid-user \允许文件中所有用户均可访问

          允许账号文件中的所有用户登录访问:
              Require valid-user        
           
      (2) 提供账号和密码存储(文本文件)
          使用专用命令完成此类文件的创建及用户管理
          htpasswd [options] /PATH/HTTPD_PASSWD_FILE username
              -c:自动创建文件,仅应该在文件不存在时使用
              -m:md5格式加密
              -s:sha格式加密
              -D:删除指定用户
    

    基于组账号进行认证
    (1) 定义安全域
    <Directory “/path">
    AuthType Basic
    AuthName "String“
    AuthUserFile "/PATH/HTTPD_USER_PASSWD_FILE"
    AuthGroupFile "/PATH/HTTPD_GROUP_FILE"
    Require group grpname1 grpname2 ...

    (2) 创建用户账号和组账号文件;
    组文件:每一行定义一个组
    GRP_NAME: username1 username2 ...

    示例 (基于用户和组账号认证):
    htpasswd -c /etc/httpd/conf.d/.htuser httpuser1 \创建http服务的账号及密码,默认加密类型为md5加密,初次创建http用户需使用-c新建/etc/httpd/conf.d/.htuser用户账号密码的存储文件,/etc/httpd/conf.d/.htuser存储文件路径,可自定义其他路径
    htpasswd -s /etc/httpd/conf.d/.htuser httpuser2 \-s指定加密类型为sha

      echo '<h1>/app/web/admin/</h1>' > /app/web/admin/index.html  新建/app/web/admin目录,指定访问此目录时需使用账号登录
      setfacl -m u:apache:r /etc/httpd/conf.d/.htuser  \\授权apache读文件的权限
          
      1. 基于/etc/httpd/conf/httpd.conf配置文件验证
          vim /etc/httpd/conf/httpd.conf
              DocumentRoot "/app/web/"
              <Directory /app/web/admin>
                  authtype basic
                  authname "admin directory"
                  authuserfile "/vim /etc/httpd/conf.d/.htuser"
                  require user httpuser1 httpuser2 
                  Require valid-user \\允许文件中所有均可访问
              </Directory>
          
      2. 基于.htaccess文件验证,文件可存放在web访问路径下
          vim /etc/httpd/conf/httpd.conf
              DocumentRoot "/app/web/"
              <Directory /app/web/admin>
                  allowoverride authconfig
              </Directory>
          
          vim /app/web/admin/.htaccess
              authtype basic
              authname "admin directory"
              authuserfile "/etc/httpd/conf.d/.htuser"
              Require valid-user
    
      3. 基于组文件用户验证
          vim /etc/httpd/conf.d/.htgroup
              g1:httpuser1 httpuser2
              g2:httpuser3
          
          vim /app/web/admin/.htaccess 
              authuserfile "etc/httpd/conf.d/.htuser"
              authtype basic
              authname "admin directory"
              authuserfile "/etc/httpd/conf.d/.htuser"
              authgroupfile "etc/httpd/conf.d/.htgroup"
              Require group g1 \\允许g1访问
    

    远程客户端和用户验证的控制
    Satisfy ALL|Any
    ALL客户机IP 和用户验证都需要通过才可以
    Any客户机IP和用户验证, 有一个满足即可
    示例:
    Require valid-user
    Order allow,deny
    Allow from 192.168.1
    Satisfy Any

  • 虚拟主机

    站点标识:socket
    IP相同,但端口不同
    IP不同,但端口均为默认端口
    FQDN不同;
    请求报文中首部
    Host: www.danran.com
    有三种实现方案:
    基于ip:为每个虚拟主机准备至少一个ip地址
    基于port:为每个虚拟主机使用至少一个独立的port
    基于FQDN:为每个虚拟主机使用至少一个FQDN
    注意:一般虚拟机不要与main 主机混用;因此,要使用虚拟主机,一般先禁用main主机
    禁用方法:注释中心主机DocumentRoot指令即可

    虚拟主机的配置方法:

    ServerName FQDN
    DocumentRoot “/path"

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

      其它可用指令:
      ServerAlias:虚拟主机的别名;可多次使用
      ErrorLog:错误日志
      CustomLog:访问日志
      <Directory “/path">
      </Directory>
      Alias
    

    基于IP的虚拟主机示例:
    <VirtualHost 172.16.100.6:80>
    ServerName www.a.com
    DocumentRoot "/www/a.com/htdocs"

      <VirtualHost 172.16.100.7:80>
          ServerName www.b.net
          DocumentRoot "/www/b.net/htdocs"
      </VirtualHost>
    
      <VirtualHost 172.16.100.8:80>
          ServerName www.c.org
          DocumentRoot "/www/c.org/htdocs"
      </VirtualHost>
    

    基于端口的虚拟主机:可和基于IP的虚拟主机混和使用
    listen 808
    listen 8080
    <VirtualHost 172.16.100.6:80>
    ServerName www.a.com
    DocumentRoot "/www/a.com/htdocs"

      <VirtualHost 172.16.100.6:808>
          ServerName www.b.net
          DocumentRoot "/www/b.net/htdocs"
      </VirtualHost>
    
      <VirtualHost 172.16.100.6:8080>
          ServerName www.c.org
          DocumentRoot "/www/c.org/htdocs"
      </VirtualHost>
    

    基于FQDN的虚拟主机:
    NameVirtualHost *:80 httpd2.4 不需要此指令
    <VirtualHost *:80>
    ServerName www.a.com
    DocumentRoot "/www/a.com/htdocs"

      <VirtualHost *:80>
          ServerName www.b.net
          DocumentRoot "/www/b.net/htdocs"
      </VirtualHost>
    
      <VirtualHost *:80>
          ServerName www.c.org
          DocumentRoot "/www/c.org/htdocs"
      </VirtualHost>
    

    示例(三个基于IP的虚拟主机):
    新建三个目录/app/web1、/app/web2、/app/web3用来用虚拟主机的根目录
    mkdir /app/web1/ /app/web2/ /app/web3/
    echo 'welcome /app/web1/danran' > /app/web1/index.html
    echo 'welcome /app/web2/danran' > /app/web2/index.html
    echo 'welcome /app/web3/danran' > /app/web3/index.html
    setenfore 0
    iptables -F

      新建vhosts配置文件,用于编辑虚拟主机的配置信息(也可在/etc/httpd/conf/httpd.conf文件中直接修改)
          vim /etc/httpd/conf.d/vhosts.conf \\(复制/etc/httpd/conf/httpd.conf文件中的最后几行虚拟主机的配置)
              
              <VirtualHost 192.168.198.136:80> \\指定虚拟主机的IP和端口
                  ServerAdmin danran.com  \\指定web主机的邮件地址
                  DocumentRoot /app/web1 \\虚拟主机的根目录
                  ServerName  www.web1.com \\对应web服务的全称域名
                  ErrorLog logs/web1_error_log \\虚拟主机web服务的错误日志记录
                  CustomLog  logs/web1_access_log common \\\\虚拟主机web服务的访问日志记录
              </VirtualHost>
    
    
              <VirtualHost 192.168.198.137:80> \\指定虚拟主机的IP和端口
                  DocumentRoot /app/web2  \\虚拟主机的根目录
                  ServerName www.web2.com
                  ErrorLog logs/web2_error_log
                  CustomLog logs/web2_access_log common
              </VirtualHost>
    
    
              <VirtualHost 192.168.198.138:80>
                  DocumentRoot /app/web3
                  ServerName www.web3.com
                  ErrorLog logs/web3_error_log
                  CustomLog logs/web3_access_log common
              </VirtualHost>
    

    示例(基于端口的虚拟主机)
    新建三个目录/app/web1、/app/web2、/app/web3用来用虚拟主机的根目录
    mkdir /app/web1/ /app/web2/ /app/web3/
    echo 'welcome /app/web1/danran' > /app/web1/index.html
    echo 'welcome /app/web2/danran' > /app/web2/index.html
    echo 'welcome /app/web3/danran' > /app/web3/index.html
    setenfore 0
    iptables -F

      新建vhosts配置文件,用于编辑虚拟主机的配置信息(也可在/etc/httpd/conf/httpd.conf文件中直接修改)
          vim /etc/httpd/conf.d/vhosts.conf \\(复制/etc/httpd/conf/httpd.conf文件中的最后几行虚拟主机的配置)
              Listen 192.168.198.136:8001 \\监听192.168.198.136主机的8001端口
              Listen 192.168.198.136:8002 \\\\监听192.168.198.136主机的8002端口
              Listen 192.168.198.136:8003 \\\\监听192.168.198.136主机的8003端口
    
              <VirtualHost 192.168.198.136:8001>
                  DocumentRoot /app/web1
                  ServerName www.web1.com
                  ErrorLog logs/web1_error_log
                  CustomLog logs/web1_access_log common
              </VirtualHost>
    
    
              <VirtualHost 192.168.198.136:8002>
                  DocumentRoot /app/web2
                  ServerName www.web2.com
                  ErrorLog logs/web2_error_log
                  CustomLog logs/web2_access_log common
              </VirtualHost>
    
    
              <VirtualHost 192.168.198.136:8003>
                  DocumentRoot /app/web3
                  ServerName www.web3.com
                  ErrorLog logs/web3_error_log
                  CustomLog logs/web3_access_log common
              </VirtualHost>
      
      访问测试
          192.168.198.136:8001
          192.168.198.136:8002
          www.web1.com(需配置域名DNS解析实现)
    

    示例(基于FQDN名称的虚拟主机)
    新建三个目录/app/web1、/app/web2、/app/web3用来用虚拟主机的根目录
    mkdir /app/web1/ /app/web2/ /app/web3/
    echo 'welcome /app/web1/danran' > /app/web1/index.html
    echo 'welcome /app/web2/danran' > /app/web2/index.html
    echo 'welcome /app/web3/danran' > /app/web3/index.html
    setenfore 0
    iptables -F

      新建vhosts配置文件,用于编辑虚拟主机的配置信息(也可在/etc/httpd/conf/httpd.conf文件中直接修改)
          vim /etc/httpd/conf.d/vhosts.conf \\(复制/etc/httpd/conf/httpd.conf文件中的最后几行虚拟主机的配置
          
              # Use name-based virtual hosting.
    
              NameVirtualHost *:80 \\基于虚拟主机的名称,对应所有主机的IP的80端口
              <VirtualHost *:80>
                  DocumentRoot /app/web1
                  ServerName www.web1.com
                  ErrorLog logs/web1_error_log
                  CustomLog logs/web1_access_log common
              </VirtualHost>
    
    
              <VirtualHost *:80>
                  DocumentRoot /app/web2
                  ServerName www.web2.com
                  ErrorLog logs/web2_error_log
                  CustomLog logs/web2_access_log common
              </VirtualHost>
    
    
              <VirtualHost *:80>
                  DocumentRoot /app/web3
                  ServerName www.web3.com
                  ErrorLog logs/web3_error_log
                  CustomLog logs/web3_access_log common
              </VirtualHost>
    
      
      
      客户端测试:
          vim /etc/hosts \\编辑主机名解析,通常利用DNS来解析
              192.168.198.136 www.web1.com www.web2.com www.web3.com 
      
          curl www.web1.com
          curl www.web2.com
          curl www.web3.com
      
      注意:定义了基于主机名称的虚拟主机,则其他主机失效,若访问其他的web主机,则默认访问第一个web的虚拟主机
    
  • status页面

    LoadModule status_module modules/mod_status.so

    vim /etc/httpd/conf.d/status.conf

      ExtendedStatus On \\显示扩展信息
      <Location /server-status>
          SetHandler server-status \\系统内部指令
          Order deny,allow \\设置权限顺序
          Deny from all
          Allow from 172.16 \\除了172.16允许其他都拒绝
      </Location>
    

    ExtendedStatus On 显示扩展信息

    访问:
    192.168.198.136/server-status

      结果如下
      Apache Server Status for 192.168.198.136
    
      Server Version: Apache/2.2.15 (Unix) DAV/2 SVN/1.6.11
      Server Built: Mar 22 2017 06:52:55
      Current Time: Tuesday, 04-Jul-2017 20:52:00 CST
      Restart Time: Tuesday, 04-Jul-2017 20:52:00 CST
      Parent Server Generation: 0
      Server uptime:
      2 requests currently being processed, 0 idle workers
      WWSSSSSS........................................................
      ................................................................
      ................................................................
      ................................................................
      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
    
      PID Key: 
          6426 in state: W ,   6427 in state: W ,   6428 in state: S 
          6429 in state: S ,   6430 in state: S ,   6431 in state: S 
          6432 in state: S ,   6433 in state: S ,
      To obtain a full report with current status information you need to use the ExtendedStatus On directive.
      Apache Server at 192.168.198.136 Port 80
    
  • 使用mod_deflate模块压缩页面优化传输速度

    适用场景:
    (1) 节约带宽,额外消耗CPU;同时,可能有些较老浏览器不支持
    (2) 压缩适于压缩的资源,例如文本文件

          LoadModule deflate_module modules/mod_deflate.so  \\指定加载模块
          
          SetOutputFilter DEFLATE \\开始默认压缩
          # Restrict compression to these MIME types
          DeflateCompressionLevel 9 \\压缩比
          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
    
              InternetMatch Explorer标识本身为“Mozilla / 4”,但实际上是能够处理请求的压缩。如果用户代理首部匹配字符串“MSIE”(“B”为单词边界),就关闭之前定义的限制
              # MSIE masquerades as Netscape, but it is fine
              BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
    

    示例(数据压缩后传输):
    vim /etc/httpd/conf.d/vhosts.conf
    <VirtualHost *:80>
    DocumentRoot /app/web2
    ServerName www.web2.com
    ErrorLog logs/web2_error_log
    CustomLog logs/web2_access_log common
    LoadModule deflate_module modules/mod_deflate.so

          SetOutputFilter DEFLATE
          DeflateCompressionLevel 9
          # 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
      
      客户端访问
          curl --compressed -I www.web2.com
    
  • http重定向https

    将http请求转发至https的URL
    重定向
    Redirect [status] URL-path URL

    status状态:
    Permanent:Returns a permanent redirect status (301) indicating that the resource has moved permanently
    Temp:Returns a temporary redirect status (302). This is the default

    示例:
    vim /etc/httpd/conf.d/vhosts.conf

          <VirtualHost *:80>
              DocumentRoot /app/web3
              ServerName www.web3.com
              ErrorLog logs/web3_error_log
              CustomLog logs/web3_access_log common
              Redirect temp / https://www.web1/com
          </VirtualHost>
      访问测试:
          http://www.web3/com  \\将会自动跳转到https://web1.com
    
  • HSTS

    HSTS:HTTP Strict Transport Security
    服务器端配置支持HSTS后,会在给浏览器返回的HTTP首部中携带HSTS字段。浏览器获取到该信息后,会将所有HTTP访问请求在内部做307跳转到HTTPS。而无需任何网络过程
    HSTS preload list 是Chrome浏览器中的HSTS预载入列表,在该列表中的网站,使用Chrome浏览器访问时,会自动转换成HTTPS。Firefox、Safari、Edge浏览器也会采用这个列表

    实现HSTS
    Header always set Strict-Transport-Security "max-age=15768000"
    RewriteEngine on
    RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]

    示例:
    vim /etc/httpd/conf.d/vhosts.conf
    <VirtualHost :80>
    DocumentRoot /app/web3
    ServerName www.web3.com
    ErrorLog logs/web3_error_log
    CustomLog logs/web3_access_log common
    Header always set Strict-Transport-Security "max-age=15768000"
    RewriteEngine on
    RewriteRule ^(/.
    )$ https://%{HTTP_HOST}$1 [redirect=301]

    访问测试:
    http://www.web3/com \将会自动跳转到https://web3.com

posted @ 2017-08-02 20:15  JevonWei  阅读(605)  评论(0编辑  收藏  举报