今天試著把 ASP.NET Core 放在 Docker 再利用 IIS Reverse Proxy 來對外時偶然間發現 IP 取值錯誤,利用 Request.HttpContext.Connection.RemoteIpAddress.ToString()
總是取到 Docker Host 的 IP!?
心得筆記
[IIS][GitLab] 利用 IIS Reverse Proxy 將 GitLab 加上 Https
最近工作上有個需求要將 GitLab 掛上 SSL 憑證,原本就有一台 Windows Server 在管理所有的 SSL 憑證,於是乎就想到可以利用 IIS Reverse Proxy 的方式將憑證集中管理在這台,這樣管理起來也比較方便。
前置作業
首先需要安裝兩個模組
安裝 URL Rewrite 的時候有踩到一個地雷,詳情請見 [IIS] Windows Server 2016 安裝 URL Rewrite 憑證失效
開始設定
- 建立一個新站台並設定好 SSL 憑證 (由於這邊我只是測試用,所以我自己簽一個自我憑證)
-
選擇
URL Rewrite
-
選擇
新增規則
->反向 Proxy
-
選擇
確定
啟用 Proxy
-
1
的地方輸入 GitLab 的位置,2
的位置輸入欲對外的 Domain
-
當我以為簡單就完成開啟網頁試試時卻噴錯誤給我看
1 2 3 |
HTTP 錯誤 500.52 - URL Rewrite Module Error. 當 HTTP 回應的內容經過編碼 ("gzip") 時,無法套用輸出重寫規則。 |
原因是因為 GitLab 有開啟壓縮,但使用了反向 Proxy 無法將已壓縮的檔案進行修改,所以就噴了這個錯誤
- 解決方法是在
伺服器變數
做調整
-
新增兩個變數分別是
HTTP_ACCEPT_ENCODING
與HTTP_X_ORIGINAL_ACCEPT_ENCODING
-
編輯輸入規則
-
新增兩個伺服器變數,伺服器變數名稱選擇剛剛加入的
HTTP_X_ORIGINAL_ACCEPT_ENCODING
,值輸入{HTTP_ACCEPT_ENCODING}
-
我們已經將
HTTP_ACCEPT_ENCODING
放到HTTP_X_ORIGINAL_ACCEPT_ENCODING
裡面,原本的值就可以清空清空,但因為 UI 不允許輸入空值,所以這邊我就隨便打個值
如果跟我一樣很在意的話可以去站台底下的web.config
清空剛剛的值
-
終於可以開起來囉 ~
顯示調整
雖然已經可以正常開啟網頁了,但網址還是會顯示錯誤
- 新增兩個伺服器變數
HTTP_X_FORWARDED_HOST
與HTTP_X_FORWARDED_PROTO
HTTP_X_FORWARDED_HOST
輸入 Domain,HTTP_X_FORWARDED_PROTO
輸入 https
- 修改
/etc/gitlab/gitlab.rb
加上這段
1 2 3 |
external_url 'https://gitlab.exfast.me' nginx['listen_port'] = 80 nginx['listen_https'] = false |
這樣頁面顯示的網址就正常囉,不過 https://gitlab.exfast.me/profile/active_sessions
這邊顯示的 IP會是 IIS 那台的位址,所以還必須調整 /etc/gitlab/gitlab.rb
加上這段
1 2 3 4 5 |
# Each address is added to the the NGINX config as 'set_real_ip_from <address>;' nginx['real_ip_trusted_addresses'] = [ '127.0.0.1', '192.168.56.106' ] # other real_ip config options nginx['real_ip_header'] = 'X-Forwarded-For' nginx['real_ip_recursive'] = 'on' |
參考資料
[IIS] Windows Server 2016 安裝 URL Rewrite 憑證失效
2016 年的時候我筆記過一篇 [IIS] Windows Server 2016 無法安裝 URL Rewrite !?
想不到現在 2018 年了再次安裝 URL Rewrite 時又遇到新問題,微軟爸爸不要亂放地雷好嗎?
我是從 這邊 下載的 這個版本 安裝時會出現下面這張圖的狀況
經過千辛萬苦的餵狗後發現是微軟在 2018/09/12 發布的版本帶了時間戳,而這個時間戳卻造成無法安裝的問題,這邊我有找到一個舊版本給大家急救一下 點我下載 (密碼:https://blog.exfast.me/
)
參考資料:
URL rewrite installation failing due to signature verification failure
[C#][.NETCore] 壓縮檔案 & 解壓縮檔案
在 .NET Framework 的時候有好用的 DotNetZip 可以快速壓縮&解壓縮檔案
但在寫 .NET Core 的時候發現這個套件不能使用,那只好默默地自己寫了
這邊就用 System.IO.Compression
提供的 ZipArchive
來簡單寫個壓縮 & 解壓縮範例
壓縮檔案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
string rootPath = AppDomain.CurrentDomain.BaseDirectory; string saveFileName = Path.Combine(rootPath, "test.zip"); string zipFileName = Path.Combine(rootPath, "123.txt"); using (var fs = new FileStream(saveFileName, FileMode.OpenOrCreate)) { using (ZipArchive zipArchive = new ZipArchive(fs, ZipArchiveMode.Create)) { string fileName = Path.GetFileName(zipFileName); var zipArchiveEntry = zipArchive.CreateEntry(fileName); using (var zipStream = zipArchiveEntry.Open()) { byte[] bytes = File.ReadAllBytes(zipFileName); zipStream.Write(bytes, 0, bytes.Length); } } } |
解壓縮檔案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
string rootPath = AppDomain.CurrentDomain.BaseDirectory; string zipFileName = Path.Combine(rootPath, "test.zip"); string saveFolder = rootPath; using (var fs = new FileStream(zipFileName, FileMode.Open)) { using (ZipArchive zipArchive = new ZipArchive(fs, ZipArchiveMode.Read)) { foreach (ZipArchiveEntry zipArchiveEntry in zipArchive.Entries) { string temp = Path.Combine(saveFolder, zipArchiveEntry.FullName); zipArchiveEntry.ExtractToFile(temp); } } } |
解壓縮的時候最好可以將 zipArchiveEntry.FullName
再做一層處理,避免產生資安問題 (詳情)
[Docker][Linux] SQL Server 安裝筆記
環境:Window 7
- 安裝 Docker Toolbox
- 新增 Volume
1 |
docker volume create vol-mssql |
- 建立容器
1 2 3 4 5 6 7 8 9 10 |
docker run \ --restart=always \ --name mssql \ --mount "source=vol-mssql,target=/var/opt/mssql" \ -e "ACCEPT_EULA=Y" \ -e "SA_PASSWORD=1OCHWiY9O#RF" \ -e "MSSQL_PID=Express" \ -e "MSSQL_COLLATION=Chinese_Taiwan_Stroke_CI_AS" \ -p 1433:1433 \ -d microsoft/mssql-server-linux:latest |
- 更新時區
1 2 3 4 |
docker exec -ti mssql bash apt-get update apt-get install tzdata -y dpkg-reconfigure tzdata |
參考資料:
[Docker][NetCore] Win7 可以在 Docker 下 Debug ASP.NET Core 嗎?
今天心血來潮想試試看可不可以把 ASP.NET Core 部屬在 Docker 裡面 Debug,但我的測試機是 Win7 無法安裝新版 Docker for Windows
於是乎抱著僥倖下載 Docker Toolbox
來試試看
兩者最大差異在於 Docker Toolbox
是運行在 Oracle VM VirtualBox
,而 Docker for Windows
則是可以在微軟的虛擬化技術 Hyper-V
底下運行
肉身測試開始
如果想要讓現有專案支援 Docker 的話只要對 專案右鍵 -> 加入 -> Docker Support
強大的 Visual Studio 2017 就會搞定基礎設定
這邊的話要選擇 Linux
上面的 Windows
是給 Docker for Windows
選的
接著 Visual Studio 就會幫你下載與設定一些基本環境
接著直接按 F5
執行偵錯模式
噹噹… 他竟然說我沒有安裝 dotnet sdk
1 2 3 4 5 6 7 8 9 10 |
------------------------------------------------------------------- You may only use the Microsoft .NET Core Debugger (vsdbg) with Visual Studio Code, Visual Studio or Visual Studio for Mac software to help you develop and test your applications. ------------------------------------------------------------------- Did you mean to run dotnet SDK commands? Please install dotnet SDK from: http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409 The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core. The program '[24] dotnet' has exited with code 145 (0x91). '' 程式以返回碼 145 (0x91) 結束。 |
翻了很久才翻到一篇文章 Docker debugging for ASP.NET Core application is not working 國外也有個勇者想在 Win7 利用 Docker 來 Debug ASP.NET Core
最後被加上了 Closed - Won't Fix
的 Tag 看來這個問題應該是不會修正了
結論
想要在 Docker 上玩 ASP.NET Core 還是乖乖用 Win10 吧
哪天有空了來試試看在 Win10 安裝 Docker 玩玩看
不知道會不會又有其他地雷引爆
[C#][DI] 相互依賴造成循環注入死結
問題緣由
最近在寫 .NET Core 的時候遇到了一個問題
1 |
A circular dependency was detected for the service of type 'WebApplication1.Service.A1Service' |
A1Service
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 |
public class A1Service { private readonly string guid = null; private readonly A2Service _a2Service = null; public A1Service(A2Service a2Service) { _a2Service = a2Service; if (guid == null) { guid = Guid.NewGuid().ToString(); } } public void Run() { Console.WriteLine("A1Service: " + guid); } public void RunOther() { _a2Service.Run(); } } |
這是網路教學文章上常見的從建構子中依賴注入 A2Service
的程式
接著我們再來看看另外一支程式
A2Service
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 |
public class A2Service { private readonly string guid = null; private readonly A1Service _a1Service = null; public A2Service(A1Service a1Service) { _a1Service = a1Service; if (guid == null) { guid = Guid.NewGuid().ToString(); } } public void Run() { Console.WriteLine("A2Service: " + guid); } public void RunOther() { _a1Service.Run(); } } |
這裡就發生了一個循環依賴的問題 A1Service
在建構子中注入了 A2Service
,而 A2Service
也在建構子中注入了 A1Service
,造成兩個物件互相依賴造成 Exception
解決方法
改成不直接在建構子中注入 Service ,改注入 IServiceProvider
,再利用 GetService
這個方法來取得物件,這樣就可以順利解決這個問題囉。
A3Service
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 |
public class A3Service { private readonly string guid = null; private readonly IServiceProvider _serviceProvider = null; public A3Service(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; if (guid == null) { guid = Guid.NewGuid().ToString(); } } public void Run() { Console.WriteLine("A3Service: " + guid); } public void RunOther() { _serviceProvider.GetService<A4Service>().Run(); } } |
A4Service
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 |
public class A4Service { private readonly string guid = null; private readonly IServiceProvider _serviceProvider = null; public A4Service(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; if (guid == null) { guid = Guid.NewGuid().ToString(); } } public void Run() { Console.WriteLine("A4Service: " + guid); } public void RunOther() { _serviceProvider.GetService<A3Service>().Run(); } } |
[iOS] 越獄後 Whoscall 老是更新失敗無法使用?
不知道有沒有人跟我一樣手賤去點更新 Whoscall,來電辨識功能就掛掉了,無法開啟識別也無法更新資料庫,嘗試過無數次重新安裝與重開機都出現下圖狀況
今天剛好看到瘋先生 免電腦!直接用App Store替iOS App 將版/降回舊版教學 這篇文章,就趕快在我的手機上降級 App 版本看看還有沒有救,最後結果當然是…復活拉 ~
實測裝置
- Device:Apple iPhone 7
- Version:iOS 10.1.1
教學步驟
- 從 Cydia 安裝 App Admin (教學)
- 在 AppStore 找到 Whoscall
長按開啟
並點選Downgrade
-
選擇
2.2.16
(最後一版可以成功啟用的版本)
-
大功告成 !!
不知道 Whoscall 是為了防堵越獄還是程式本身的 Bug,有回報粉絲團許久都沒有修正這個問題,在程式修復以前目前只好用降版的方式了。
[Windows] 更新 Visual Studio 2017 後偵錯模式會自己停止?
最近更新了 Visual Studio 2017 到最新版本,發現在debug的時候 IIS Express 總是被停止運行,再找 bug 的時候發生這件事情實在是很惱人的一件事情,拜了 google 大神後發現微軟竟然在更新的時候偷偷把這個設定啟用,這實在是太坑了 !!
解決方法:將下面兩張圖的勾勾都取消就可以恢復正常囉。
[C#][LeetCode] 28. Implement strStr()
從傳入參數 haystack
中找出參數 needle
中的索引位置,非常基礎的一題
我的答案
1 2 3 4 5 6 7 8 9 10 11 |
public class Solution { public int StrStr(string haystack, string needle) { if(needle == null || needle == string.Empty){ return 0; } else if(haystack == null){ return -1; } return haystack.IndexOf(needle); } } |