差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

兩邊的前次修訂版 前次修改
下次修改
前次修改
tech:apache [2011/02/20 22:14] jonathantech:apache [2021/12/04 11:35] (目前版本) jonathan
行 1: 行 1:
 +====== Apache WebServer 設定技巧 ======
 +===== 設定啟用 HSTS =====
 +  * [[https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security|點這裡了解 HSTS(HTTP Strict Transport Security)]]
 +  * 將 httpd.conf 內的 headers_module 啟用<file>
 +:
 +LoadModule headers_module modules/mod_headers.so
 +:</file>
 +  * VirtualHost 內增加 header 設定<file>
 +:
 +<VirtualHost www.example.com:80>
 +Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
 +</VirtualHost>
  
 +<VirtualHost www.example.com:443>
 +Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
 +</VirtualHost>
 +:
 +</file>
 +
 +**如果是 Debian 環境**
 +  * <cli>sudo a2enmod headers</cli>
 +  * 編輯 virtual host exp. <cli>vi sites-enabled/wordpress.conf</cli><file>
 +:
 +<VirtualHost *:80>
 +    UseCanonicalName Off
 +    ServerAdmin  webmaster@localhost
 +    DocumentRoot /var/www/wordpress
 +    Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
 +</VirtualHost>
 +
 +<VirtualHost *:443>
 +    SSLEngine on
 +    ServerAdmin  webmaster@localhost
 +    DocumentRoot /var/www/wordpress
 +    Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
 +</VirtualHost>
 +:
 +</file>
 +  * 重新啟動 apache <cli>sudo systemctl restart apache2</cli>
 +
 +
 +===== 改用 MPM worker 模組提升效能 =====
 +  * https://www.brilliantcode.net/877/apache-2-2-2-4-performance-tuning-mpm-engine/
 +
 +===== 設定多個不同網域(Domain Name)網站設定 =====
 +  * 假設要設定 
 +    * www.abc.com 目錄是 /var/www/www.abc.com/
 +    * www.xyz.com 目錄是 /var/www/www.xyz.com/
 +  * 主要是修改 /etc/httpd/conf/httpd.conf <file>
 +:
 +:
 +### Section 3: Virtual Hosts
 +:
 +NameVirtualHost *:80
 +:
 +<VirtualHost *:80>
 +ServerName www.abc.com
 +ServerAdmin [email protected]
 +DocumentRoot /var/www/www.abc.com
 +CustomLog logs/www.abc.com-access_log common
 +ErrorLog logs/www.abc.com-error_log
 +</VirtualHost>
 +
 +<VirtualHost *:80>
 +ServerName www.xyz.com
 +ServerAdmin [email protected]
 +DocumentRoot /var/www/www.xyz.com
 +CustomLog logs/www.xyz.com-access_log common
 +ErrorLog logs/www.xyz.com-error_log
 +</VirtualHost>
 +
 +</file>
 +===== 開啟與關閉顯示目錄檔案清單 =====
 +通常基於資訊安全因素會關閉顯示目錄檔案清單, 所以在 httpd.conf 內會設定所有目錄都不顯示 (也就是移除掉 **MultiViews** 的設定), 將 Options Indexes FollowSymLinks 前面加上 #
 +<code>
 +:
 +# Note that "MultiViews" must be named *explicitly* --- "Options All"
 +:
 +#   Options Indexes FollowSymLinks
 +
 +#
 +# AllowOverride controls what directives may be placed in .htaccess files.
 +</code>
 +
 +如果要顯示目錄檔案清單,可以針對特定目錄設定 Options 有 MultiViews 功能, Exp. /Stuff 以下可以針對特定 IP 來源存取並出現檔案目錄清單
 +<code>
 +<Location /Stuff>
 +  Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
 +    Order deny,allow
 +    Deny from all
 +    Allow from 127.0.0.1
 +    Allow from 192.168.11.0/24
 +</Location>
 +</code>
 +
 +===== - ProxyPass directive 抓遠端主機頁面 =====
 +我是用在將 VMWare 在內部運作的網頁能夠簡易的呈現到 Internet 的 WebServer 上.
 +
 +<ditaa name=ProxyPass>
 +
 +                +----------------------+   +--------------+
 +  +---------+   | webServer(ProxyPass) |     vm-mail    |
 +  | Browser +-->      /webmail       +--> /webmail    |
 +  +---------+     mail.ichiayi.com     |192.168.11.238|
 +                +----------------------+   +--------------+
 +
 +</ditaa>
 +
 +Browser 連上 webServer(http://mail.ichiayi.com/webmail), 出現的畫面是由 vm-mail(http://192.168.11.238/webmail) 所提供頁面
 +
 +==== WebServer 設定方式 ====
 +  * apache 要有安裝 mod_proxy (預設已經安裝)
 +  * 只要在 httpd.conf 內定義 :
 +<code>
 +ProxyPass /webmail http://192.168.11.238/webmail
 +</code>
 +這樣 http://mail.ichiayi.com/webmail/ 就可以正確轉讀至 http://192.168.11.238/webmail 網頁功能
 +
 +==== vm-mail 設定方式 ====
 +只要依據一般正常的 web server 設定即可
 +
 +===== 自訂找不到網頁的設定方式 =====
 +當某個網站移轉到新的網址, 原本網頁在舊網址內都移除掉, 希望只要在 Google 找到舊的網址都能出現已經移到新的網址的訊息.. 這時就可以將 404 找不到網頁的訊息, 自訂成說明網址移轉的網頁. 設定方式只要在 httpd.conf 內增加一行, 指定呈現的轉只說明頁面.
 +
 +<file>
 +:
 +<VirtualHost *:80>
 +ServerName sport.ichiayi.com
 +ServerAdmin [email protected]
 +DocumentRoot /backup/lintsai/sport
 +ErrorDocument 404 /err404.htm
 +:
 +
 +</file>
 +
 +然後編輯 /backup/lintsai/sport/err404.htm 
 +<code>
 +<html>
 +<head>
 +<title>嘉義市體育會~已經更新網址~</title>
 +
 +<meta http-equiv="content-type" content="text/html;charset=utf8">
 +<meta http-equiv="refresh" content="5; URL=http://sport.104es.com/">
 +</head>
 +
 +<body>
 +<center>
 +<a href=http://sport.104es.com/>按這邊直接連結到新網址</a><font color=red size=6><br />
 +</center>
 +</body>
 +</html>
 +</code>
 +
 +===== 設定特定IP存取目錄不需要輸入帳號密碼 =====
 +針對特定 IP 來存取目錄時, 可以免除輸入帳號密碼增加方便性的設定方式如下:
 +  * 只要來自 192.168.11.* 都不需要輸入帳號密碼, 就可直接存取 mydata
 +  * 其他的 IP 都需要輸入帳號密碼, 才可存取 mydata
 +<file>
 +
 +<Location /mydata>
 +      Order deny,allow
 +      Deny from all
 +      AuthType Basic
 +      AuthName "Authorization Realm"
 +      AuthUserFile /data/web-data/mydata.user
 +      Require valid-user
 +      Allow from 192.168.11.
 +      Satisfy Any
 +</Location>
 +
 +</file>
 +
 +
 +===== 參考資料 =====
 +  * http://httpd.apache.org/docs/1.3/mod/mod_proxy.html#proxypass
 +  * http://httpd.apache.org/docs/2.0/custom-error.html
 +  * http://www.askapache.com/htaccess/apache-authentication-in-htaccess.html#allow-conditional
 +  * http://httpd.apache.org/docs/2.2/mod/core.html#satisfy
 +  * https://docs.microfocus.com/SM/9.60/Hybrid/Content/security/concepts/support_of_http_strict_transport_security_protocol.htm
 +  * https://www.simplified.guide/apache/enable-hsts
 +
 +{{tag>apache httpd 密技}}