這陣子執行一個專案,其中VM使用Docker並架設多個Container,外層VM本體使用Apache(80 Port)並使用VirtualHost的技術,然後對針每個Container的連線Port作Proxy Pass。
設定VirtualHost的例子如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# webmail <VirtualHost *:80> ServerName a1.bbb.ccc ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://localhost:8000/ ProxyPassReverse / http://localhost:8000/ <Location /> Order allow,deny Allow from all </Location> </VirtualHost> # web1 <VirtualHost *:80> ServerName a2.bbb.ccc ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ <Location /> Order allow,deny Allow from all </Location> </VirtualHost> # web2 <VirtualHost *:80> ServerName a3.bbb.ccc ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://localhost:8880/ ProxyPassReverse / http://localhost:8880/ <Location /> Order allow,deny Allow from all </Location> </VirtualHost> # web3 <VirtualHost *:80> ServerName a4.bbb.ccc ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://localhost:8800/ ProxyPassReverse / http://localhost:8800/ <Location /> Order allow,deny Allow from all </Location> </VirtualHost> # 原來 web3 之設定, 其他設定類似不再例舉 <VirtualHost *:8800> DocumentRoot /www/a4.bbb.ccc ServerName a4.bbb.ccc </VirtualHost> |
設完完成後發現一件事情,就是在Laravel原生的URL Function會取得原ip位置而非我們定義的Domain,所以我們需要再各別設定Laravel中的Proxy設定。
Step1:設定TrustProxies ( app/Http/Middleware\TrustProxies.php )
1 2 3 |
protected $proxies = [ '**' ]; |
Step2:設定「.env」檔
1 2 |
PROXY_URL = http://www.mydomain.be/ PROXY_SCHEMA = http |
Step3:設定「routes/web.php」
1 2 3 4 5 6 7 8 9 |
$proxy_url = env('PROXY_URL'); $proxy_schema = env('PROXY_SCHEMA'); if (!empty($proxy_url)) { URL::forceRootUrl($proxy_url); } if (!empty($proxy_schema)) { URL::forceScheme($proxy_schema); } |
這樣作完後,就會啟用Laravel URL Proxy的設定。