Files
Obsidian-Main/03. 資料收集/HTTP Server/Nginx.md
Awin Huang be8ed59169 vault backup: 2022-09-26 18:19:43
Affected files:
.obsidian/workspace
01. 個人/00. Informations/Datas.md
01. 個人/01. Daily/2018/2018-10-05(週五).md
01. 個人/01. Daily/2018/2018-10-06(週六).md
01. 個人/01. Daily/2018/2018-10-08(週一).md
01. 個人/01. Daily/2018/2018-10-12(週五).md
01. 個人/01. Daily/2018/2018-10-13(週六).md
01. 個人/01. Daily/2018/2018-11-30(週五).md
01. 個人/01. Daily/2018/2018-12-03(週一).md
01. 個人/01. Daily/2018/2018-12-07(週五).md
01. 個人/01. Daily/2019/2019-09-12(週四).md
01. 個人/01. Daily/2020/02/2020-02-07(週五).md
01. 個人/01. Daily/2020/02/2020-02-14(週五).md
01. 個人/01. Daily/2020/03/2020-03-06(週五).md
01. 個人/01. Daily/2020/03/2020-03-12(週四).md
01. 個人/01. Daily/2020/03/2020-03-22(週日).md
01. 個人/01. Daily/2020/03/2020-03-23(週一).md
01. 個人/01. Daily/2020/03/2020-03-24(週二).md
01. 個人/01. Daily/2020/03/2020-03-25(週三).md
01. 個人/01. Daily/2020/03/2020-03-29(週日).md
01. 個人/01. Daily/2020/04/2020-04-02(週四).md
01. 個人/01. Daily/2020/04/2020-04-15(週三).md
01. 個人/01. Daily/2020/04/2020-04-18(週六).md
01. 個人/01. Daily/2020/04/2020-04-19(週日).md
01. 個人/01. Daily/2020/04/2020-04-22(週三).md
01. 個人/01. Daily/2020/04/2020-04-27(週一).md
01. 個人/01. Daily/2020/05/2020-05-09(週六).md
01. 個人/01. Daily/2020/05/2020-05-10(週日).md
01. 個人/01. Daily/2020/05/2020-05-11(週一).md
01. 個人/01. Daily/2020/05/2020-05-13(週三).md
01. 個人/01. Daily/2020/05/2020-05-14(週四).md
01. 個人/01. Daily/2020/05/2020-05-15(週五).md
01. 個人/01. Daily/2020/05/2020-05-16(週六).md
01. 個人/01. Daily/2020/05/2020-05-17(週日).md
01. 個人/01. Daily/2020/05/2020-05-18(週一).md
01. 個人/01. Daily/2020/05/2020-05-19(週二).md
01. 個人/01. Daily/2020/05/2020-05-20(週三).md
01. 個人/01. Daily/2020/07/2020-07-10(週五).md
01. 個人/01. Daily/2020/07/2020-07-24(週三).md
01. 個人/01. Daily/2020/08/2020-08-13(週四).md
01. 個人/01. Daily/2020/09/2020-09-12(週六).md
01. 個人/01. Daily/2020/10/2020-10-06(週二).md
01. 個人/01. Daily/2020/10/2020-10-22(週四).md
01. 個人/01. Daily/2020/11/2020-11-10(週二).md
01. 個人/01. Daily/2020/12/2020-12-02(週三).md
01. 個人/01. Daily/2020/12/2020-12-03(Thu).md
01. 個人/01. Daily/2020/12/2020-12-04(Fri).md
01. 個人/01. Daily/2020/12/2020-12-05(Sat).md
01. 個人/01. Daily/2020/12/2020-12-06(Sun).md
01. 個人/01. Daily/2020/12/2020-12-09(Wed).md
01. 個人/01. Daily/2020/12/2020-12-10(Thu).md
01. 個人/01. Daily/2020/12/2020-12-11(Fri).md
01. 個人/01. Daily/2020/12/2020-12-12(Sat).md
01. 個人/01. Daily/2020/12/2020-12-13(Sun).md
01. 個人/01. Daily/2020/12/2020-12-16(Wed).md
01. 個人/01. Daily/2020/12/2020-12-17(Thu).md
01. 個人/01. Daily/2020/12/2020-12-18(Fri).md
01. 個人/01. Daily/2020/12/2020-12-19(Sat).md
01. 個人/01. Daily/2020/12/2020-12-21(Mon).md
01. 個人/01. Daily/2020/12/2020-12-22(Tue).md
01. 個人/01. Daily/2020/12/2020-12-24(Thu).md
01. 個人/01. Daily/2020/12/2020-12-25(Fri).md
01. 個人/01. Daily/2020/12/2020-12-26(Sat).md
01. 個人/01. Daily/2020/12/2020-12-27(Sun).md
01. 個人/01. Daily/2020/12/2020-12-30(Wed).md
01. 個人/01. Daily/2021/01/2021-01-06(Wed).md
01. 個人/01. Daily/2021/01/2021-01-09(Sat).md
01. 個人/01. Daily/2021/01/2021-01-10(Sun).md
01. 個人/01. Daily/2021/01/2021-01-11(Mon).md
01. 個人/01. Daily/2021/01/2021-01-13(Wed).md
01. 個人/01. Daily/2021/01/2021-01-14(Thu).md
01. 個人/01. Daily/2021/01/2021-01-15(Fri).md
01. 個人/01. Daily/2021/01/2021-01-16(Sat).md
01. 個人/01. Daily/2021/01/2021-01-17(Sun).md
01. 個人/01. Daily/2021/01/2021-01-18(Mon).md
01. 個人/01. Daily/2021/01/2021-01-21(Thu).md
01. 個人/01. Daily/2021/01/2021-01-22(Fri).md
01. 個人/01. Daily/2021/01/2021-01-23(Sat).md
01. 個人/01. Daily/2021/01/2021-01-24(Sun).md
01. 個人/01. Daily/2021/01/2021-01-25(Mon).md
01. 個人/01. Daily/2021/01/2021-01-26(Tue).md
01. 個人/01. Daily/2021/01/2021-01-27(Wed).md
01. 個人/01. Daily/2021/01/2021-01-28(Thu).md
01. 個人/01. Daily/2021/01/2021-01-30(Sat).md
01. 個人/01. Daily/2021/02/2021-02-01(Mon).md
01. 個人/01. Daily/2021/02/2021-02-02(Tue).md
01. 個人/01. Daily/2021/02/2021-02-03(Wed).md
01. 個人/01. Daily/2021/02/2021-02-04(Thu).md
01. 個人/01. Daily/2021/02/2021-02-08(Mon).md
01. 個人/01. Daily/2021/02/2021-02-13(Sat).md
01. 個人/01. Daily/2021/02/2021-02-15(Mon).md
01. 個人/01. Daily/2021/02/2021-02-16(Tue).md
01. 個人/01. Daily/2021/02/2021-02-20(Sat).md
01. 個人/01. Daily/2021/02/2021-02-21(Sun).md
01. 個人/01. Daily/2021/02/2021-02-22(Mon).md
01. 個人/01. Daily/2021/03/2021-03-01(Mon).md
01. 個人/01. Daily/2021/03/2021-03-13(週六).md
01. 個人/01. Daily/2021/03/2021-03-15(週一).md
01. 個人/01. Daily/2021/03/2021-03-17(週三).md
01. 個人/01. Daily/2021/03/2021-03-20(週六).md
01. 個人/01. Daily/2021/03/2021-03-21(週日).md
01. 個人/01. Daily/2021/03/2021-03-27(週六).md
01. 個人/01. Daily/2021/03/2021-03-28(週日).md
01. 個人/01. Daily/2021/04/2021-04-05(週一).md
01. 個人/01. Daily/2021/04/2021-04-06(週二).md
01. 個人/01. Daily/2021/04/2021-04-08(週四).md
01. 個人/01. Daily/2021/04/2021-04-10(週六).md
01. 個人/01. Daily/2021/04/2021-04-11(週日).md
01. 個人/01. Daily/2021/04/2021-04-12(週一).md
01. 個人/01. Daily/2021/05/2021-05-02(週日).md
01. 個人/01. Daily/2021/05/2021-05-22(週六).md
01. 個人/01. Daily/2021/05/2021-05-24(週一).md
01. 個人/01. Daily/2021/05/2021-05-25(週二).md
01. 個人/01. Daily/2021/05/2021-05-26(週三).md
01. 個人/01. Daily/2021/05/2021-05-27(週四).md
01. 個人/01. Daily/2021/05/2021-05-28(週五).md
01. 個人/01. Daily/2021/05/2021-05-29(週六).md
01. 個人/01. Daily/2021/06/2021-06-01(週二).md
01. 個人/01. Daily/2021/06/2021-06-02(週三).md
01. 個人/01. Daily/2021/06/2021-06-03(週四).md
01. 個人/01. Daily/2021/06/2021-06-04(週五).md
01. 個人/01. Daily/2021/06/2021-06-05(週六).md
01. 個人/01. Daily/2021/06/2021-06-06(週日).md
01. 個人/01. Daily/2021/06/2021-06-07(週一).md
01. 個人/01. Daily/2021/06/2021-06-08(週二).md
01. 個人/01. Daily/2021/06/2021-06-09(週三).md
01. 個人/01. Daily/2021/06/2021-06-10(週四).md
01. 個人/01. Daily/2021/06/2021-06-11(週五).md
01. 個人/01. Daily/2021/06/2021-06-12(週六).md
01. 個人/01. Daily/2021/06/2021-06-15(週二).md
01. 個人/01. Daily/2021/06/2021-06-26(週六).md
01. 個人/01. Daily/2021/06/2021-06-28(週一).md
01. 個人/01. Daily/2021/07/2021-07-01(週四).md
01. 個人/01. Daily/2021/07/2021-07-02(週五).md
01. 個人/01. Daily/2021/07/2021-07-04(週日).md
01. 個人/01. Daily/2021/07/2021-07-05(週一).md
01. 個人/01. Daily/2021/07/2021-07-06(週二).md
01. 個人/01. Daily/2021/07/2021-07-07(週三).md
01. 個人/01. Daily/2021/07/2021-07-09(週五).md
01. 個人/01. Daily/2021/07/2021-07-10(週六).md
01. 個人/01. Daily/2021/07/2021-07-11(週日).md
01. 個人/01. Daily/2021/07/2021-07-14(週三).md
01. 個人/01. Daily/2021/07/2021-07-15(週四).md
01. 個人/01. Daily/2021/07/2021-07-16(週五).md
01. 個人/01. Daily/2021/07/2021-07-17(週六).md
01. 個人/01. Daily/2021/07/2021-07-18(週日).md
01. 個人/01. Daily/2021/07/2021-07-19(週一).md
01. 個人/01. Daily/2021/07/2021-07-20(週二).md
01. 個人/01. Daily/2021/07/2021-07-21(週三).md
01. 個人/01. Daily/2021/07/2021-07-22(週四).md
01. 個人/01. Daily/2021/07/2021-07-23(週五).md
01. 個人/01. Daily/2021/07/2021-07-24(週六).md
01. 個人/01. Daily/2021/07/2021-07-25(週日).md
01. 個人/01. Daily/2021/07/2021-07-26(週一).md
01. 個人/01. Daily/2021/07/2021-07-27(週二).md
01. 個人/01. Daily/2021/07/2021-07-28(週三).md
01. 個人/01. Daily/2021/07/2021-07-29(週四).md
01. 個人/01. Daily/2021/07/2021-07-30(週五).md
01. 個人/01. Daily/2021/07/2021-07-31(週六).md
01. 個人/01. Daily/2021/08/2021-08-02(週一).md
01. 個人/01. Daily/2021/08/2021-08-03(週二).md
01. 個人/01. Daily/2021/08/2021-08-04(週三).md
01. 個人/01. Daily/2021/08/2021-08-05(週四).md
01. 個人/01. Daily/2021/08/2021-08-06(週五).md
01. 個人/01. Daily/2021/08/2021-08-07(週六).md
01. 個人/01. Daily/2021/08/2021-08-08(週日).md
01. 個人/01. Daily/2021/08/2021-08-09(週一).md
01. 個人/01. Daily/2021/08/2021-08-10(週二).md
01. 個人/01. Daily/2021/08/2021-08-11(週三).md
01. 個人/01. Daily/2021/08/2021-08-13(週五).md
01. 個人/01. Daily/2021/08/2021-08-14(週六).md
01. 個人/01. Daily/2021/08/2021-08-16(週一).md
01. 個人/01. Daily/2021/08/2021-08-17(週二).md
01. 個人/01. Daily/2021/08/2021-08-20(週五).md
01. 個人/01. Daily/2021/08/2021-08-24(週二).md
01. 個人/01. Daily/2021/08/2021-08-25(週三).md
01. 個人/01. Daily/2021/08/2021-08-26(週四).md
01. 個人/01. Daily/2021/09/2021-09-01(週三).md
01. 個人/01. Daily/2021/09/2021-09-02(週四).md
01. 個人/01. Daily/2021/09/2021-09-09(週四).md
01. 個人/01. Daily/2021/09/2021-09-13(週一).md
01. 個人/01. Daily/2021/09/2021-09-16(週四).md
01. 個人/01. Daily/2021/09/2021-09-22(週三).md
01. 個人/01. Daily/2021/09/2021-09-23(週四).md
01. 個人/01. Daily/2021/09/2021-09-24(週五).md
01. 個人/01. Daily/2021/09/2021-09-27(週一).md
01. 個人/01. Daily/2021/09/2021-09-28(週二).md
01. 個人/01. Daily/2021/10/2021-10-04(週一).md
01. 個人/01. Daily/2021/10/2021-10-06(週三).md
01. 個人/01. Daily/2021/10/2021-10-14(週四).md
01. 個人/01. Daily/2021/10/2021-10-20(週三).md
01. 個人/01. Daily/2021/10/2021-10-28(週四).md
01. 個人/01. Daily/2021/11/2021-11-01(週一).md
01. 個人/01. Daily/2021/11/2021-11-04(週四).md
01. 個人/01. Daily/2021/11/2021-11-12(週五).md
01. 個人/01. Daily/2021/11/2021-11-15(週一).md
01. 個人/01. Daily/2021/12/2021-12-01(週三).md
01. 個人/01. Daily/2021/12/2021-12-13(週一).md
01. 個人/01. Daily/2021/12/2021-12-15(週三).md
01. 個人/01. Daily/2021/12/2021-12-16(週四).md
01. 個人/01. Daily/2021/12/2021-12-17(週五).md
01. 個人/01. Daily/2021/12/2021-12-18(週六).md
01. 個人/01. Daily/2021/12/2021-12-20(週一).md
01. 個人/01. Daily/2021/12/2021-12-21(週二).md
01. 個人/01. Daily/2021/12/2021-12-23(週四).md
01. 個人/01. Daily/2021/12/2021-12-24(週五).md
01. 個人/01. Daily/2022-06-01(週三).md
01. 個人/01. Daily/2022-06-06(週一).md
01. 個人/01. Daily/2022-07-15(週五).md
01. 個人/01. Daily/2022/01/2022-01-01(週六).md
01. 個人/01. Daily/2022/01/2022-01-08(週六).md
01. 個人/01. Daily/2022/01/2022-01-10(週一).md
01. 個人/01. Daily/2022/01/2022-01-13(週四).md
01. 個人/01. Daily/2022/01/2022-01-19(週三).md
01. 個人/01. Daily/2022/01/2022-01-24(週一).md
01. 個人/01. Daily/2022/01/2022-01-26(週三).md
01. 個人/01. Daily/2022/02/2022-02-10(週四).md
01. 個人/01. Daily/2022/02/2022-02-11(週五).md
01. 個人/01. Daily/2022/02/2022-02-14(週一).md
01. 個人/01. Daily/2022/02/2022-02-15(週二).md
01. 個人/01. Daily/2022/03/2022-03-04(週五).md
01. 個人/01. Daily/2022/03/2022-03-28(週一).md
01. 個人/01. Daily/2022/04/2022-04-06(週三).md
01. 個人/01. Daily/2022/04/2022-04-08(週五).md
01. 個人/01. Daily/2022/04/2022-04-12(週二).md
01. 個人/01. Daily/2022/04/2022-04-14(週四).md
01. 個人/01. Daily/2022/年度目標.md
01. 個人/02. 專注Study/20150803 - Android/ADB 取得 APK 的 icon.md
01. 個人/02. 專注Study/20150803 - Android/ADB.md
01. 個人/02. 專注Study/20150803 - Android/AOSP.md
01. 個人/02. 專注Study/20150803 - Android/Ktor.md
01. 個人/02. 專注Study/20150803 - Android/Service.md
01. 個人/02. 專注Study/20150803 - Android/Tools.md
01. 個人/02. 專注Study/20150803 - Android/UI.md
01. 個人/02. 專注Study/20200207 - Study RxKotlin.md
01. 個人/02. 專注Study/20220601 - C++/C++17 1.md
01. 個人/02. 專注Study/20220601 - C++/C++17.md
01. 個人/02. 專注Study/20220601 - C++/Class template.md
01. 個人/02. 專注Study/20220601 - C++/Structured binding declaration.md
01. 個人/02. 專注Study/20220601 - C++/for_each.md
01. 個人/02. 專注Study/20220601 - C++/lambda.md
01. 個人/02. 專注Study/20220601 - C++/lvalue.md
01. 個人/02. 專注Study/20220601 - C++/move operator.md
01. 個人/02. 專注Study/20220601 - C++/rvalue.md
01. 個人/02. 專注Study/20220601 - C++/智慧指標.md
02. PARA/02. Area(領域)/00000000 - 投資.md
02. PARA/02. Area(領域)/001. Study Android Programming.md
02. PARA/02. Area(領域)/002. Study RxKotlin.md
02. PARA/02. Area(領域)/003. LBRY.md
02. PARA/02. Area(領域)/004. IPFS.md
02. PARA/02. Area(領域)/005. 投資.md
02. PARA/03. Resources(資源)/00. Informations/核芯達.md
02. PARA/03. Resources(資源)/WEB Programming.md
02. PARA/03. Resources(資源)/vim.md
02. PARA/04. Archives(歸檔)/01. Project(專案)/002. Bolide firmware verfication.md
02. PARA/04. Archives(歸檔)/01. Project(專案)/003. Meetup firmware update check.md
02. PARA/04. Archives(歸檔)/01. Project(專案)/004. Group firmware update check.md
02. PARA/04. Archives(歸檔)/03. Resources(資源)/Make Google Docs Look Like Dropbox Paper.md
02. PARA/04. Archives(歸檔)/03. Resources(資源)/PARA筆記.md
03. 資料收集/01. 架站/01. Nginx Layer4 Reverse Proxy.md
03. 資料收集/01. 架站/02. SWAG Reverse proxy.md
03. 資料收集/01. 架站/03. Trojan.md
03. 資料收集/01. 架站/04. Gitea.md
03. 資料收集/50. 軍武/虎式.md
03. 資料收集/51. 模型/Traxxas Sledge.md
03. 資料收集/51. 模型/舊化作例.md
03. 資料收集/802.11.md
03. 資料收集/99. templates/blogHeader.md
03. 資料收集/99. templates/date.md
03. 資料收集/99. templates/front matter.md
03. 資料收集/99. templates/note.md
03. 資料收集/99. templates/table.md
03. 資料收集/99. templates/thisWeek.md
03. 資料收集/99. templates/日記.md
03. 資料收集/99. templates/讀書筆記.md
03. 資料收集/Android operating.md
03. 資料收集/Android programming.md
03. 資料收集/COM/20210726 - COM Interface.md
03. 資料收集/Design Pattern.md
03. 資料收集/FFMPEG/00. Introduction.md
03. 資料收集/FFMPEG/01. Setup.md
03. 資料收集/FFMpeg.md
03. 資料收集/Flask.md
03. 資料收集/HDR Sensor.md
03. 資料收集/HTTP Server/Apache.md
03. 資料收集/HTTP Server/Nginx.md
03. 資料收集/IPFS.md
03. 資料收集/Linux/CLI.md
03. 資料收集/Linux/Programming.md
03. 資料收集/Linux/Timezone.md
03. 資料收集/Linux/Ubuntu.md
03. 資料收集/Linux/scp.md
03. 資料收集/Media Foundation/20210604 - Windows media foundation.md
03. 資料收集/MkDocs.md
03. 資料收集/MySQL.md
03. 資料收集/NextDNS.md
03. 資料收集/Nginx.md
03. 資料收集/Obsidian.md
03. 資料收集/OpenCV.md
03. 資料收集/OpenGL.md
03. 資料收集/Pelican blog.md
03. 資料收集/Proxmox VE.md
03. 資料收集/Python/argparse.ArgumentParser.md
03. 資料收集/Python/decorator.md
03. 資料收集/Python/logging.md
03. 資料收集/Python/opencv.md
03. 資料收集/Python/subprocess.md
03. 資料收集/Python/threading.md
03. 資料收集/Python/tkinter.md
03. 資料收集/Python/檢測工具.md
03. 資料收集/QT/Dropdown button.md
03. 資料收集/QT/QVariant.md
03. 資料收集/QT/Qt.md
03. 資料收集/Qt.md
03. 資料收集/RC.md
03. 資料收集/RaspberryPi.md
03. 資料收集/SLAM.md
03. 資料收集/Scripts.md
03. 資料收集/Storj.md
03. 資料收集/Tool Setup/Chrome.md
03. 資料收集/Tool Setup/Obisidian.md
03. 資料收集/Tool Setup/SublimeText.md
03. 資料收集/Tool Setup/VirtualBox.md
03. 資料收集/Tool Setup/Visual Studio Code.md
03. 資料收集/Tool Setup/Windows Setup.md
03. 資料收集/Tool Setup/Windows Terminal.md
03. 資料收集/Tool Setup/freefilesync.md
03. 資料收集/Tool Setup/vim.md
03. 資料收集/Trojan.md
03. 資料收集/UML.md
03. 資料收集/V2Ray.md
03. 資料收集/__其他.md
03. 資料收集/docker.md
03. 資料收集/frp.md
03. 資料收集/git/apply.md
03. 資料收集/git/submodule.md
03. 資料收集/sqlite.md
03. 資料收集/名言佳句.md
03. 資料收集/每週外食.md
03. 資料收集/演算法.md
03. 資料收集/翻牆/V2Ray.md
03. 資料收集/翻牆/Wireguard.md
03. 資料收集/讀書筆記/20201201 - 學習如何學習.md
03. 資料收集/讀書筆記/20201218 - Kotlin權威2.0.md
03. 資料收集/讀書筆記/20201224 - 寫作是最好的自我投資.md
03. 資料收集/讀書筆記/20210119 - 中產悲歌.md
03. 資料收集/讀書筆記/20210220 - 最高學習法.md
03. 資料收集/讀書筆記/20210320 - 最高學以致用法.md
03. 資料收集/讀書筆記/20210406 - 精準購買.md
03. 資料收集/讀書筆記/20210723 - 高手學習.md
03. 資料收集/讀書筆記/20220526 - 深入淺出設計模式.md
03. 資料收集/讀書筆記/20220619 - 精確的力量.md
03. 資料收集/財經.md
03. 資料收集/量子電腦.md
03. 資料收集/鋰電池.md
2022-09-26 18:19:44 +08:00

15 KiB
Raw Blame History

Reverse Proxy(Layer4)

03. 資料收集/HTTP Server/Nginx#Reverse Proxy所用的方法雖然可以反向代理多個網站但是對於像是Trojan這種TLS不行被中斷的服務來說會導致handshake失敗所以需要用Nginx的stream來做Layer 4的轉發。

docker-compose.yaml

需要先把nginx.confmime.types給copy到data目錄下。 依序執行下面2個命令

sudo docker run --rm -it nginx cat /etc/nginx/nginx.conf > nginx.conf
sudo docker run --rm -it nginx cat /etc/nginx/mime.types > mime.types

然後:

mkdir data ;\
mv nginx.conf mime.types data

建立docker-compose.yaml

version: '3'
services:
  nginx_reverseproxy_l4:
    container_name: nginx
    restart: always
    image: nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./data:/etc/nginx

修改data/nginx.conf

events {
    worker_connections  1024; <-- event 不用動
}

stream {
    map $ssl_preread_server_name $backend_name {
        tjn.awin.one             trojan;
        storj.awin.one           swag;
    }

    # trojan
    upstream trojan {
        server 192.168.1.31:443;
    }

    # swag
    upstream swag {
        server 192.168.1.20:44320;
    }

    # 监听 443 并开启 ssl_preread
    server {
        listen      80 reuseport;
        listen      443 reuseport;
        listen      [::]:443 reuseport;
        proxy_pass  $backend_name;
        ssl_preread on;
    }
}

http {
    ...                     <-- http 不用動
}

2022/05/17更新

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

stream {
    map $ssl_preread_server_name $backend_name {
        tjn.awin.one             trojan;
        storj.awin.one           swag;
        blog.awin.one            swag;
        gitea.awin.one           swag;
    }

    # trojan
    upstream trojan {
        server 192.168.1.31:443;
    }

    # swag
    upstream swag {
        server 192.168.1.20:44320;
    }

    # 监听 443 并开启 ssl_preread
    server {
        listen      443 reuseport;
        listen      [::]:443 reuseport;
        proxy_pass  $backend_name;
        ssl_preread on;
    }
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    ## 新增這一段沒有這一段會造成http無法連線所以拿憑證會失敗
    server {
        listen 80;
        server_name tjn.awin.one;

        location / {
            proxy_pass http://192.168.1.31:80;

            # 把 IP、Protocol 等 header 都一起送給反向代理的 server
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
        }
    }
}

重點來源:

  1. Trojan 共用 443 端口方案 - 程小白
  2. NaiveProxy + Trojan + 多HTTPS站点共存复用443端口 | 心底的河流
  3. $ssl_preread_server_name的官方說明:Module ngx_stream_ssl_preread_module

Reverse Proxy

重要確定docker與docker-compose已經安裝好。 參考RaspberryPi#DockerRaspberryPi#docker-compose

Use SWAG docker

swag(之前叫做letsencrypt)是linuxserver.io包裝的Nginx webserver和reverse proxy的container。

Setup HTTPS

  1. 建立folder

    mkdir -p ~/dockers/linuxserverswag ; cd ~/dockers/linuxserverswag
    
  2. 建立docker-compose.yaml

    vim docker-compose.yaml
    

    填入內容如下:

    version: "2.1"
    services:
      swag:
        image: ghcr.io/linuxserver/swag
        container_name: swag
        cap_add:
          - NET_ADMIN
        environment:
          - PUID=1000
          - PGID=1000
          - TZ=Asia/Taipei
          - URL=awin.one
          - SUBDOMAINS=wildcard
          - VALIDATION=dns
          - CERTPROVIDER= #optional
          - DNSPLUGIN=cloudflare #optional
          - PROPAGATION= #optional
          - DUCKDNSTOKEN= #optional
          - EMAIL=awinhuang@gmail.com
          - ONLY_SUBDOMAINS=false #optional
          - EXTRA_DOMAINS= #optional
          - STAGING=false #optional
          - MAXMINDDB_LICENSE_KEY= #optional
        volumes:
          - ./config:/config
        ports:
          - 44320:443
          - 8020:80 #optional
        restart: unless-stopped
    
    
  3. 先跑一次:

    sudo docker-compose up
    

    會發現有錯誤,這是正常的,錯誤訊息像這樣:

    swag    | Unsafe permissions on credentials configuration file: /config/dns-conf/cloudflare.ini
    swag    | Cleaning up challenges
    swag    | Error determining zone_id: 9103 Unknown X-Auth-Key or X-Auth-Email. Please confirm that you have supplied valid Cloudflare API credentials. (Did you enter the correct email address and Global key?)
    swag    | ERROR: Cert does not exist! Please see the validation error above. Make sure you entered correct credentials into the /config/dns-conf/cloudflare.ini file.
    

    ctrl + c退出。這時候config目錄也會有swag所mapping出來的相關檔案。修改config/dns-conf/cloudflare.ini

    vim config/dns-conf/cloudflare.ini
    

    config/dns-conf/cloudflare.ini改為:

    # Instructions: https://github.com/certbot/certbot/blob/master/certbot-dns-cloudflare/certbot_dns_cloudflare/__init__.py#L20
    # Replace with your values
    
    With global api key:
    dns_cloudflare_email = awinhuang@gmail.com
    dns_cloudflare_api_key = <YOUR_API_KEY_FROM_CLOUDFLARE>
    
    # With token (comment out both lines above and uncomment below):
    #dns_cloudflare_api_token = 0123456789abcdef0123456789abcdef01234567
    
  4. 再跑一次:

    sudo docker-compose up
    

    這一次就可以順利取得認證了,訊息像這樣:

    swag    | IMPORTANT NOTES:
    swag    |  - Congratulations! Your certificate and chain have been saved at:
    swag    |    /etc/letsencrypt/live/awin.one/fullchain.pem
    swag    |    Your key file has been saved at:
    swag    |    /etc/letsencrypt/live/awin.one/privkey.pem
    swag    |    Your certificate will expire on 2021-04-26. To obtain a new or
    swag    |    tweaked version of this certificate in the future, simply run
    swag    |    certbot again. To non-interactively renew *all* of your
    swag    |    certificates, run "certbot renew"
    swag    |  - If you like Certbot, please consider supporting our work by:
    swag    |
    swag    |    Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
    swag    |    Donating to EFF:                    https://eff.org/donate-le
    swag    |
    swag    | New certificate generated; starting nginx
    swag    | Starting 2019/12/30, GeoIP2 databases require personal license key to download. Please retrieve a free license key from MaxMind,
    swag    | and add a new env variable "MAXMINDDB_LICENSE_KEY", set to your license key.
    swag    | [cont-init.d] 50-config: exited 0.
    swag    | [cont-init.d] 60-renew: executing...
    swag    | The cert does not expire within the next day. Letting the cron script handle the renewal attempts overnight (2:08am).
    swag    | [cont-init.d] 60-renew: exited 0.
    swag    | [cont-init.d] 70-templates: executing...
    swag    | [cont-init.d] 70-templates: exited 0.
    swag    | [cont-init.d] 99-custom-files: executing...
    swag    | [custom-init] no custom files found exiting...
    swag    | [cont-init.d] 99-custom-files: exited 0.
    swag    | [cont-init.d] done.
    swag    | [services.d] starting services
    swag    | [services.d] done.
    swag    | nginx: [alert] detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)
    swag    | Server ready
    

    最後一行的swag | Server ready表示server已經跑起來了。先按下ctrl + c退出再來設定reverse proxy。

  5. 修正config/dns-conf/cloudflare.ini的安全性問題

    cd ~/dockers/linuxserverswag ; chmod 600 config/dns-conf/cloudflare.ini
    

Setup reverse proxy

  1. 建立folder

    cd ~/dockers/linuxserverswag ; mkdir -p config/nginx/sites-available config/nginx/sites-enabled 
    
  2. 建立以下檔案:

    • config/nginx/sites-available/common.conf,內容:
      add_header Strict-Transport-Security    "max-age=31536000; includeSubDomains" always;
      add_header X-Frame-Options              SAMEORIGIN;
      add_header X-Content-Type-Options       nosniff;
      add_header X-XSS-Protection             "1; mode=block";
      
    • config/nginx/sites-available/common_location.conf,內容:
      proxy_set_header    X-Real-IP           $remote_addr;
      proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
      proxy_set_header    X-Forwarded-Proto   $scheme;
      proxy_set_header    Host                $host;
      proxy_set_header    X-Forwarded-Host    $host;
      proxy_set_header    X-Forwarded-Port    $server_port;
      
    • config/nginx/sites-available/storj.conf,內容:
      upstream storj {
          server         192.168.1.11:14002;
      }
      
      server {
          listen         443 ssl;
          server_name    storj.awin.one;
      
          include        /config/nginx/sites-available/common.conf;
          include        /config/nginx/ssl.conf;
      
          location / {
              proxy_pass http://192.168.1.11:14002/;
              include    /config/nginx/sites-available/common_location.conf;
          }
      }
      
      
  3. config/nginx/sites-enabled裡面建立要enable的config

    cd config/nginx/sites-enabled ; ln -s ../sites-available/storj.conf . ; cd -
    
  4. 修改config/nginx/nginx.conf

    vim config/nginx/nginx.conf
    

    找到include /config/nginx/site-confs/*;這一行把它comment掉在下面新增一行

    include /config/nginx/sites-enabled/*.conf;
    
  5. 啟動swag

    cd ~/dockers/linuxserverswag ; sudo docker-compose up -d
    

Restart

cd ~/dockers/linuxserverswag ; sudo docker-compose restart

Update certification

  1. 進到dockr的swag bash裡面sudo docker exec -it swag /bin/bash
  2. 輸入 certbot renew
  3. !Pasted image 20210422205534.png

Trouble shooting

  • 如果遇到類似下面的錯誤:
    ERROR: for swag  Cannot start service swag: driver failed programming external connectivity on endpoint swag (7c527d046631e0957de0b831ca25bed296de76e2eb96378964cb0110d7fb017d): Bind for 0.0.0.0:443 failed: port is already allocated.
    
    表示有其他程式佔住了80 port可能是其他docker container或是service必須先關閉它們。1

參考來源

  1. linuxserver/docker-swag: Nginx webserver and reverse proxy with php support and a built-in Certbot (Let's Encrypt) client. It also contains fail2ban for intrusion prevention.
  2. How to set up an easy and secure reverse proxy with Docker, Nginx & Letsencrypt
  3. SWAG setup - LinuxServer.io

新增網站

新網站自帶SSL

進到Rasperberry Pi192.168.1.20)之後,切換到~/dockers/nginx_reverseproxy_l4,並編輯data/nginx.conf在裡面加入新網站的網域與要轉址的ip。 例如要加入一個trojan VPNtrojan VPN本身就有SSL加密所以不用再經過swagdata/nginx.conf的改變如下: !Pasted image 20220506145247.png

新http網站

如果新的網站只是一般的http網站那便把它掛到swag後面這樣就可以經由https來訪問假如要加入一個blog網站但因為我們有2層的reverse proxy第一層是layer 4第2層是swag所以如果是自帶https的要掛到layer 4之後沒有https要先由layer 4轉到swag再轉到實際的server上。 步驟如下:

  1. 進到Rasperberry Pi192.168.1.20
  2. 設定nginx_reverseproxy_l4這邊我們需要把「沒有https」的網站由nginx_reverseproxy_l4導引到linuxserverswag
    cd ~/dockers/nginx_reverseproxy_l4
    vim data/nginx.conf
    
    加入下圖紅框的設定: !Pasted image 20220516152230.png
  3. 切換到~/dockers/linuxserverswag/config/nginx/sites-available
  4. 新增一個confing檔例如叫做blog.conf,內容如下:
    upstream blog {
        server         192.168.1.30:80;  ## 網址
    }
    
    server {
        listen         443 ssl;
        server_name    blog.awin.one;    ## 網域
    
        include        /config/nginx/sites-available/common.conf;
        include        /config/nginx/ssl.conf;
    
        location / {
            proxy_pass http://192.168.1.30:80/;   ## 網址
            include    /config/nginx/sites-available/common_location.conf;
        }
    }
    
    改變3個有註解的地方就可以了。
  5. 剛剛的config檔新增到sites-enabled裡面
    1. cd ~/dockers/linuxserverswag/config/nginx/sites-enabled
    2. ln -s ../sites-available/blog.conf .
  6. 重新啟動swagcd ~/dockers/linuxserverswag ; sudo docker-compose restart