基本設定
/etc/apache2/sites-available/test.conf :
<VirtualHost *:80>
ServerName example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/test
<Directory /var/www/test>
RewriteEngine on
RewriteCond $1 !^(index\.php|static|crossdomain\.xml|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
ExpiresActive On
ExpiresByType image/jpg "access plus 10 years"
ExpiresByType image/jpeg "access plus 10 years"
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
LogLevel warn
</VirtualHost>
啟動 conf
sudo a2ensite test
Options
- All: 以下諸功能皆有
- None: 以下諸功能皆無
- Indexes: 自動產生目錄的索引, 將Indexes拿掉, 點選連結目錄就不會秀出目錄來, 會改成403 forbidden的訊息頁面, 如果網站下的 public 下有放 symbolic link 就要將此開啟, 才可以被 access
- Includes: 提供 SSI (Server-Side Inclues) 功能, 即使用Apache的指令在html檔中寫程式, 須先載入 includes_module
- FollowSymLinks: 遵循符號鏈接, 即能夠連到其它的目錄去執行, 會壓過 SymLinksIfOwnerMatch。
- SymLinksIfOwnerMatch: 對符號鏈接及其每一層父資料夾, 都進行權限檢查, 當連結檔本身的owner跟連結目的地的owner不同時拒絕存取, 比 FollowSymLinks 更安全
- ExecCGI: 可以執行CGI程式。
- MultiViews: 送出多國語言支援的頁面. 此功能必須被明確指定, Options All並不會提供這個功能
打開 Rewrite 模組
cd /etc/apache2
a2enmod
rewrite
設置轉向自訂的404頁面
ErrorDocument 404 /404.php
建立多個virtual host
- 在
/etc/apache2/sites-available
建立virtual host, ex: test
sudo a2ensite test
sudo service apache2 restart
設定環境變數
多用在不同的virtual host使得每個都擁有不同的環境變數例如:
不同的virtual host要共用同一個環境變數可以在/etc/apache2/apache2.conf設定
SetEnv BRAND test
各別的virtual host變數可在/etc/apache2/sites-available/ 設定:
<VirtualHost *:80>
<Directory /var/www>
...
SetEnv BRAND test[n]
...
</Directory>
</VirtualHost>
php取得apache環境變數:
echo getenv('BRAND');
如果網址只指到資料夾,不顯示目前資料夾的檔案列表
<VirtualHost *:80>
Options -Indexes FollowSymLinks
</VirtualHost>
apache2: Could not reliably determine the server’s fully qualified domain name
$ sudo service apache2 restart
[sudo] password for test:
* Restarting web server apache2
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
在/etc/apache2/sites-available/default:
ServerName test.com
<VirtualHost *:80>
ServerName test.com
(...略...)
</VirtualHost>
將ServerName
宣告為global
或
在/etc/apache2/httpd.conf加上:
ServerName test.com
但apache2.conf要記得Include httpd.conf
不存在的virtual host會自動導向(問題發生在將default不存在)
問題: 原本的default被我刪掉了,而加上兩個virtual host
如此一來 w3.test.com 因為不存在,所以會導向w1.test.com
解決方法:
將 /etc/apache2/sites-available/default
加回來, 即可解決此問題, 當不存在的domain會導向到default
後記 :
- ServerName變數不可以在每一個virtual host都宣告成global,不然apache會誤判domain
- 建議ServerName只在default宣告為global變數
apache log
在/etc/apache2/sites-available/default可以找到apache的log存放位置,例如:
ErrorLog ${APACHE_LOG_DIR}/error.log
預設存放在/var/log/apache2/error.log
讓主機上的帳號都有自己的 public 資料夾 - public_html
load /etc/apache2/mods-enabled :
userdir.load
/etc/apache2/mods-enabled/userdir.conf:
<IfModule mod_userdir.c>
UserDir public_html
UserDir disabled root
<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes FollowSymLinks IncludesNoExec
Indexes、FollowSymLinks
Options -Indexes +FollowSymLinks
-Indexes
: 不允許顯示檔案目錄結構
+FollowSymLinks
: 允許使用 .htaccess
檔案 override 預設的設定
特定副檔名忽略同源政策
在 <Directory>
裡加上
<FilesMatch "\.(ttf|otf|eot|woff|svg)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
Reduce requests from browser
當瀏覽器第一次讀取圖檔
status : 200 OK
request header:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:test.com
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
response header:
Accept-Ranges:bytes
Connection:Keep-Alive
Content-Length:45860
Content-Type:image/jpeg
Date:Thu, 11 Jul 2013 08:09:03 GMT
ETag:"bc003e-b324-4e136f4c01a37"
Keep-Alive:timeout=5, max=100
Last-Modified:Thu, 11 Jul 2013 07:00:57 GMT
Server:Apache/2.2.22 (Ubuntu)
內容會有圖檔
第二次讀取讀檔會跟cache要所以
status : 304 Not Modified
request header:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Host:test.com
If-Modified-Since:Thu, 11 Jul 2013 07:00:57 GMT
If-None-Match:"bc003e-b324-4e136f4c01a37"
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
response header:
Connection:Keep-Alive
Date:Thu, 11 Jul 2013 08:09:51 GMT
ETag:"bc003e-b324-4e136f4c01a37"
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.22 (Ubuntu)
內容不會有圖檔,只會送出request然候apache比對ETag及Last-Modified,如果檔案有被修改才是status:200
調整apache設定,當網址一樣時讓瀏覽器連304都不送直接取cache
[1] 打開模組
- 到
/etc/apache2/mods-available
ls | grep expire 及 ls | grep header
確定有沒有這兩個模組,如果沒有就開啟
- Once you install the module, the module will be available in the
/etc/apache2/mods-available
directory. You can use the a2enmod command to enable a module. You can use the a2dismod command to disable a module. Once you enable the module, the module will be available in the the /etc/apache2/mods-enabled
directory.
[2] 在 /etc/apache2/sites-available/default
的<Directory {你的path}> 加上
ExpiresActive On
ExpiresByType image/jpg "access plus 10 years"
ExpiresByType image/jpeg "access plus 10 years"
[3] sudo service apache2 restart
設定後的測試:
[1] 第一次load圖
status : 200 OK
request header:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Host:test.com
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
response header:
Accept-Ranges:bytes
Cache-Control:max-age=315360000
Connection:Keep-Alive
Content-Length:45860
Content-Type:image/jpeg
Date:Thu, 11 Jul 2013 08:11:46 GMT
ETag:"bc003e-b324-4e136f4c01a37"
Expires:Sun, 09 Jul 2023 08:11:46 GMT
Keep-Alive:timeout=5, max=99
Last-Modified:Thu, 11 Jul 2013 07:00:57 GMT
Server:Apache/2.2.22 (Ubuntu)
有加上expire了
[2] 第二次load圖
status : 304 Not Modified
request header:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Host:test.com
If-Modified-Since:Thu, 11 Jul 2013 07:00:57 GMT
If-None-Match:"bc003e-b324-4e136f4c01a37"
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
response header:
Cache-Control:max-age=315360000
Connection:Keep-Alive
Date:Thu, 11 Jul 2013 08:25:53 GMT
ETag:"bc003e-b324-4e136f4c01a37"
Expires:Sun, 09 Jul 2023 08:25:53 GMT
Keep-Alive:timeout=5, max=99
Server:Apache/2.2.22 (Ubuntu)
[3] 當從外面直接連進來後會直接從cache拿了
Request URL:http://test.com/static/a/ce/ce9600a437486fbfd9b13856f2e1c55f.jpg?1373516123
Request Method:GET
Status Code:200 OK (from cache)
Query String Parametersview sourceview URL encoded
1373516123:
並不會有request header及response header,因為根本沒送request
chrome也會顯示是 (from cache)
大頭貼檔名後加上從DB取的timestamp(ex: avatar.jpg?1373530721),當更新大頭貼時要更新DB的欄位(紀錄修改時間),才能讓瀏覽器判斷是否是修改過的圖
Conclusion
快取機制還是要看瀏覽器自已的實作, 如果到已造訪過的網站, chrome第一次可能拿快取的圖片,
但reload後chrome還是會去問一次得到304才去拿cache的檔案, 所以快取機制沒有一定, 或許chrome 再下一版又換方法了
ref : 夯哥