Apache常用配置指北

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","marks":[{"type":"italic"}],"text":"Apache"}]},{"type":"text","marks":[{"type":"italic"}],"text":" 作为世界使用排名第一的Web服务器软件,它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相信前端的小伙伴对于 "},{"type":"codeinline","content":[{"type":"text","text":"apache"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"nginx"}]},{"type":"text","text":" 都有所了解,特别是前后端分离,独立开发部署后,使用web服务器做一些代理服务可以解决绝大多数因为前后端分离造成的跨域,认证等问题(PS:都2020年了,不会还有项目组主张使用weblogic或tomcat部署前端项目吧? 不会吧,不会吧,不会吧 😓)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面,我从实际项目角度出发,说一些apache常用配置,相信做到以下配置之后,基本可以解决前端项目部署对接时80%以上的问题 (😃 我骗你的,我怎么知道能解决多少,反正先说一个数嘛)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" 指令"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"示例:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"\n Options MultiViews\n AllowOverride All\n Order allow,deny\n Allow from all\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Directory指令用于在配置的目录(示例内'/apache/web'目录)内封装一组指令,配置的指令在该目录及子目录生效。任何可以在\"directory\"作用域中使用的指令都可以使用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"配置目录(示例内 "},{"type":"codeinline","content":[{"type":"text","text":"/apache/web/"}]},{"type":"text","text":" )目录,配置目录的写法很多,可以指定一个完整的目录,如 "},{"type":"codeinline","content":[{"type":"text","text":"/apache/web/"}]},{"type":"text","text":" ,也可以使用通配符进行匹配,例如,使用 "},{"type":"codeinline","content":[{"type":"text","text":"?"}]},{"type":"text","text":"匹配任何单个字符,使用 "},{"type":"codeinline","content":[{"type":"text","text":"*"}]},{"type":"text","text":" 匹配任意多个字符,也可以使用 "},{"type":"codeinline","content":[{"type":"text","text":"[]"}]},{"type":"text","text":"匹配字符区间。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"text","marks":[{"type":"italic"},{"type":"strong"}],"text":"唯一需要注意的是:"},{"type":"text","text":" 使用通配符进行匹配时,都不能匹配 "},{"type":"codeinline","content":[{"type":"text","text":"/"}]},{"type":"text","text":" 字符,例如:配置 "},{"type":"codeinline","content":[{"type":"text","text":"/apache/*/public"}]},{"type":"text","text":" 时, 不能匹配到 "},{"type":"codeinline","content":[{"type":"text","text":"/apache/web/www/public"}]},{"type":"text","text":",即 "},{"type":"codeinline","content":[{"type":"text","text":"*"}]},{"type":"text","text":" 不能匹配到 "},{"type":"codeinline","content":[{"type":"text","text":"web/www"}]},{"type":"text","text":", 只能匹配到 "},{"type":"codeinline","content":[{"type":"text","text":"apache/web/public"}]},{"type":"text","text":"类型的路径。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Options 配置。 Options选项用于定义配置目录使用那些特性,特性列表为:"}]}]}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/be/bee770b1bd385d183515551be92e0a80.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AllowOverride选项用于定义位于每个目录下.htaccess(访问控制)文件中的指令类型。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从安全性考虑,根目录的AllowOverride属性一般都配置成不允许任何Override, 即设置 "},{"type":"codeinline","content":[{"type":"text","text":"AllowOverride None"}]},{"type":"text","text":" ,此时,配置目录及子目录的.htaccess 文件将被完全忽略。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当此指令设置为 All 时,所有具有 “.htaccess” 作用域的指令都允许出现在 .htaccess 文件中"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Order指令 用于指定执行允许(allow)访问控制规则或者拒绝(deny)访问控制规则的顺序。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 1. Order allow,deny:缺省禁止所有客户机的访问,且 Allow 语句在 Deny 语句之前被匹配。如果某条件既匹配 Deny 语句又匹配 Allow 语句,则 Deny 语句会起作用(因为 Deny 语句覆盖了 Allow 语句)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 2. Order deny,allow:缺省允许所有客户机的访问,且 Deny 语句在 Allow 语句之前被匹配。如果某条件既匹配 Deny 语句又匹配 Allow 语句,则 Allow 语句会起作用(因为 Allow 语句覆盖了 Deny 语句)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Allow指令 指明允许访问的地址或地址序列。如"},{"type":"codeinline","content":[{"type":"text","text":"allow from al"}]},{"type":"text","text":" 指令表明允许所有IP来的访问请求。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Deny指令 指明禁止访问的地址或地址序列。如 "},{"type":"codeinline","content":[{"type":"text","text":"Deny from 197.35.*.*"}]},{"type":"text","text":" 指令表明禁止IP段为197.35的主机访问。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Alias配置"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"示例:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"Alias /public /extra/video/public"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"apache的Alias配置即虚拟路径配置,如示例中,假设apache服务器启动地址为 "},{"type":"text","marks":[{"type":"italic"}],"text":"http://localhost:3000"},{"type":"text","text":" ,当访问 "},{"type":"text","marks":[{"type":"italic"}],"text":"http://localhost:3000/public/test.mp4"},{"type":"text","text":" 时, 会返回服务器 "},{"type":"codeinline","content":[{"type":"text","text":"/extra/video/public/test.mp4"}]},{"type":"text","text":" 视频文件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Rewrite"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"示例:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":" RewriteEngine on\n RewriteCond %{HTTP_HOST} ^en.cmbc.com [NC]\n RewriteRule ^(.*) http://www.cmbc.com.cn/ [L]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"示例效果为将输入 "},{"type":"text","marks":[{"type":"italic"}],"text":"en.cmbc.com"},{"type":"text","text":" 时,跳转到 "},{"type":"codeinline","content":[{"type":"text","text":"http://www.cmbc.com.cn/"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Apache Rewrite规则修正符"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":" R 强制外部重定向\n F 禁用URL,返回403HTTP状态码。\n G 强制URL为GONE,返回410HTTP状态码。\n P 强制使用代理转发。\n L 表明当前规则是最后一条规则,停止分析以后规则的重写。\n N 重新从第一条规则开始运行重写过程。\n C 与下一条规则关联"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rewirte即apache的重写规则,主要的功能就是实现URL的跳转,匹配规则支持正则匹配。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RewriteEngine 为重写开关,默认关闭重写功能"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RewriteCond 为重写条件,可多个顺序配置,符合配置规则后将执行紧邻的 "},{"type":"codeinline","content":[{"type":"text","text":"RewriteRule\b"}]},{"type":"text","text":" 语句"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RewriteRule\b 为重写规则,符合 "},{"type":"codeinline","content":[{"type":"text","text":"RewriteCond"}]},{"type":"text","text":" 条件后将执行配置的重写规则"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"ProxyPass和ProxyPassReverse"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"示例:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":" ProxyPass /swaggerui http://197.0.35.4:7000\n ProxyPassReverse /swaggerui http://197.0.35.4:7000"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"apache中的mod_proxy模块用于url的转发,即具有代理的功能。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ProxyPass即转发功能,如示例中的配置,为将 "},{"type":"text","marks":[{"type":"italic"}],"text":"http://localhost:3000/swaggerui"},{"type":"text","text":" 的请求转发至 "},{"type":"text","marks":[{"type":"italic"}],"text":"http://197.0.35.4:7000/swaggerui"},{"type":"text","text":"服务器."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ProxyPassReverse 的配置和 ProxyPass 的配置一致,它用于反向代理,例如示例中请求"},{"type":"text","marks":[{"type":"italic"}],"text":"http://localhost:3000/swaggerui"},{"type":"text","text":"后,转发给配置的目标服务器("},{"type":"text","marks":[{"type":"italic"}],"text":"http://197.0.35.4:7000swaggerui"},{"type":"text","text":"),返回的处理结果为重定向redirect至 "},{"type":"text","marks":[{"type":"italic"}],"text":"http://197.0.35.4:7000/login.html"},{"type":"text","text":" 登录页,此时若未配置反向代理,浏览器会访问 "},{"type":"text","marks":[{"type":"italic"}],"text":"http://197.0.35.4:7000/login.html"},{"type":"text","text":" ,而配置反向代理后,apache服务器会在重定向时,修改redirect地址为 "},{"type":"text","marks":[{"type":"italic"}],"text":"http://localhost:3000/login.html"},{"type":"text","text":" 。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"apache负载均衡"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"示例:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":" \n BalancerMember http://197.0.35.104:31382/version-manager loadfactor=1 hcmethod=TCP hcinterval=30 hcpasses=1 hcfails=1\n BalancerMember http://197.0.35.105:31382/version-manager loadfactor=1 hcmethod=TCP hcinterval=30 hcpasses=1 hcfails=1\n ProxySet lbmethod=byrequests\n "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"负载均衡配置稍显复杂,后续会专门总结一下相关配置,以上示例配置所达到的效果为:请求 "},{"type":"codeinline","content":[{"type":"text","text":"http://localhost:3000/version_manager"}]},{"type":"text","text":" 时,会均衡负载发至 "},{"type":"codeinline","content":[{"type":"text","text":"http://197.0.35.105:31382/version-manager"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"http://197.0.35.105:31382/version-manager"}]},{"type":"text","text":" 两台服务器。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"loadfactor表示后台服务器负载到由Apache发送请求的权值,该值默认为1,可以将该值设置为1到100之间的任何值。 简明说法就是如果有2次请求,有1次发送到 "},{"type":"codeinline","content":[{"type":"text","text":"http://197.0.35.105:31382/version-manager"}]},{"type":"text","text":" ,有一次发给 "},{"type":"codeinline","content":[{"type":"text","text":"http://197.0.35.105:31382/version-manager"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"hcmethod,hcinterval,hcpasses,hcfails为健康检查策略。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"lbmethod 为负载均衡策略,可能的取值有: lbmethod=byrequests 按照请求次数均衡(默认) lbmethod=bytraffic 按照流量均衡 lbmethod=bybusyness 按照繁忙程度均衡(总是分配给活跃请求数最少的服务器)"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"apache调优"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"示例:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"\n DeflateCompressionLevel 6\n SetOutputFilter DEFLATE\n AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript text/x-js application/x-javascript application/javascript application/json\n AddOutputFilterByType DEFLATE text/*\n AddOutputFilterByType DEFLATE image/*\n AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/javascript application/x-javascript\n SetEnvIfNoCase Request_URI .(?:apk|ipa|go)$ no-gzip dont-vary\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"启用Apache的deflate模块,可以开启压缩功能,减小网站传输时的带宽。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SetOutputFilter DEFLATE 配置项,告诉apache对传输到浏览器的内容进行压缩"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DeflateCompressionLevel 压缩等级(1-9),等级越大效率越高,但同时消耗的CPU也高,根据具体情况调优,也可以设置为6来平衡CPU及压缩质量"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary #设置不对后缀gif,jpg,jpeg,png的图片文件进行压缩"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AddOutputFilterByType DEFLATE text/* #设置对文件是文本的内容进行压缩,例如text/html text/css text/plain等"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章