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}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章