nginx 代理多個服務器——多個server方式

原文鏈接:https://blog.csdn.net/wild46cat/article/details/52997005

-------------------------------------------------------------

配置文件下載地址:https://download.csdn.net/download/zengmingen/10462400

nginx 代理多個服務器——多個server方式

上一篇文章(http://blog.csdn.net/wild46cat/article/details/52840125)介紹了nginx的基本配置和使用方法,并且簡單的介紹了一下如何利用nginx結合tomcat進行使用,達到反向代理的作用。現在我們要使用nginx達到這樣的一個目的,能夠代理多個服務器。
首先修改配置文件:
[plain]?view plaincopy
  1. #user??nobody;??
  2. worker_processes??1;??
  3. ??
  4. #error_log??logs/error.log;??
  5. #error_log??logs/error.log??notice;??
  6. #error_log??logs/error.log??info;??
  7. ??
  8. #pid????????logs/nginx.pid;??
  9. ??
  10. ??
  11. events?{??
  12. ????worker_connections??1024;??
  13. }??
  14. ??
  15. ??
  16. http?{??
  17. ????include???????mime.types;??
  18. ????default_type??application/octet-stream;??
  19. ??
  20. ????#log_format??main??'$remote_addr?-?$remote_user?[$time_local]?"$request"?'??
  21. ????#??????????????????'$status?$body_bytes_sent?"$http_referer"?'??
  22. ????#??????????????????'"$http_user_agent"?"$http_x_forwarded_for"';??
  23. ??
  24. ????#access_log??logs/access.log??main;??
  25. ??
  26. ????sendfile????????on;??
  27. ????#tcp_nopush?????on;??
  28. ??
  29. ????#keepalive_timeout??0;??
  30. ????keepalive_timeout??65;??
  31. ??
  32. ????#gzip??on;??
  33. ??
  34. ????server?{??
  35. ????????listen???????9922;??
  36. ????????server_name??firstProxyServer;??
  37. ??
  38. ????????#charset?koi8-r;??
  39. ??
  40. ????????#access_log??logs/host.access.log??main;??
  41. ??
  42. ????????#location?/?{??
  43. ???????????#root???html;??
  44. ????????????#index??index.html?index.htm;??
  45. ????????#}??
  46. ????????location?/?{??
  47. ????????????proxy_pass?http://localhost:8989;??
  48. ????????}??
  49. ??
  50. ????????#error_page??404??????????????/404.html;??
  51. ??
  52. ????????#?redirect?server?error?pages?to?the?static?page?/50x.html??
  53. ????????#??
  54. ????????error_page???500?502?503?504??/50x.html;??
  55. ????????location?=?/50x.html?{??
  56. ????????????root???html;??
  57. ????????}??
  58. ??
  59. ????????#?proxy?the?PHP?scripts?to?Apache?listening?on?127.0.0.1:80??
  60. ????????#??
  61. ????????#location?~?\.php$?{??
  62. ????????#????proxy_pass???http://127.0.0.1;??
  63. ????????#}??
  64. ??
  65. ????????#?pass?the?PHP?scripts?to?FastCGI?server?listening?on?127.0.0.1:9000??
  66. ????????#??
  67. ????????#location?~?\.php$?{??
  68. ????????#????root???????????html;??
  69. ????????#????fastcgi_pass???127.0.0.1:9000;??
  70. ????????#????fastcgi_index??index.php;??
  71. ????????#????fastcgi_param??SCRIPT_FILENAME??/scripts$fastcgi_script_name;??
  72. ????????#????include????????fastcgi_params;??
  73. ????????#}??
  74. ??
  75. ????????#?deny?access?to?.htaccess?files,?if?Apache's?document?root??
  76. ????????#?concurs?with?nginx's?one??
  77. ????????#??
  78. ????????#location?~?/\.ht?{??
  79. ????????#????deny??all;??
  80. ????????#}??
  81. ????}??
  82. ??
  83. ?????server?{??
  84. ????????listen???????9977;??
  85. ????????server_name??secondProxyServer;??
  86. ??
  87. ????????#charset?koi8-r;??
  88. ??
  89. ????????#access_log??logs/host.access.log??main;??
  90. ??
  91. ????????#location?/?{??
  92. ???????????#root???html;??
  93. ????????????#index??index.html?index.htm;??
  94. ????????#}??
  95. ????????location?/?{??
  96. ????????????proxy_pass?http://localhost:8080;??
  97. ????????}??
  98. ??
  99. ????????#error_page??404??????????????/404.html;??
  100. ??
  101. ????????#?redirect?server?error?pages?to?the?static?page?/50x.html??
  102. ????????#??
  103. ????????error_page???500?502?503?504??/50x.html;??
  104. ????????location?=?/50x.html?{??
  105. ????????????root???html;??
  106. ????????}??
  107. ??
  108. ????????#?proxy?the?PHP?scripts?to?Apache?listening?on?127.0.0.1:80??
  109. ????????#??
  110. ????????#location?~?\.php$?{??
  111. ????????#????proxy_pass???http://127.0.0.1;??
  112. ????????#}??
  113. ??
  114. ????????#?pass?the?PHP?scripts?to?FastCGI?server?listening?on?127.0.0.1:9000??
  115. ????????#??
  116. ????????#location?~?\.php$?{??
  117. ????????#????root???????????html;??
  118. ????????#????fastcgi_pass???127.0.0.1:9000;??
  119. ????????#????fastcgi_index??index.php;??
  120. ????????#????fastcgi_param??SCRIPT_FILENAME??/scripts$fastcgi_script_name;??
  121. ????????#????include????????fastcgi_params;??
  122. ????????#}??
  123. ??
  124. ????????#?deny?access?to?.htaccess?files,?if?Apache's?document?root??
  125. ????????#?concurs?with?nginx's?one??
  126. ????????#??
  127. ????????#location?~?/\.ht?{??
  128. ????????#????deny??all;??
  129. ????????#}??
  130. ????}??
  131. ??
  132. ????#?another?virtual?host?using?mix?of?IP-,?name-,?and?port-based?configuration??
  133. ????#??
  134. ????#server?{??
  135. ????#????listen???????8000;??
  136. ????#????listen???????somename:8080;??
  137. ????#????server_name??somename??alias??another.alias;??
  138. ??
  139. ????#????location?/?{??
  140. ????#????????root???html;??
  141. ????#????????index??index.html?index.htm;??
  142. ????#????}??
  143. ????#}??
  144. ??
  145. ??
  146. ????#?HTTPS?server??
  147. ????#??
  148. ????#server?{??
  149. ????#????listen???????443?ssl;??
  150. ????#????server_name??localhost;??
  151. ??
  152. ????#????ssl_certificate??????cert.pem;??
  153. ????#????ssl_certificate_key??cert.key;??
  154. ??
  155. ????#????ssl_session_cache????shared:SSL:1m;??
  156. ????#????ssl_session_timeout??5m;??
  157. ??
  158. ????#????ssl_ciphers??HIGH:!aNULL:!MD5;??
  159. ????#????ssl_prefer_server_ciphers??on;??
  160. ??
  161. ????#????location?/?{??
  162. ????#????????root???html;??
  163. ????#????????index??index.html?index.htm;??
  164. ????#????}??
  165. ????#}??
  166. ??
  167. }??

其中主要的是有兩個server,每個server對應的被代理的服務器的不同。從而實現了nginx代理多個服務器的目的。
下面是兩個服務server的配置:
[plain]?view plaincopy
  1. server?{??
  2. ????????listen???????9922;??
  3. ????????server_name??firstProxyServer;??
  4. ??
  5. ????????#charset?koi8-r;??
  6. ??
  7. ????????#access_log??logs/host.access.log??main;??
  8. ??
  9. ????????#location?/?{??
  10. ???????????#root???html;??
  11. ????????????#index??index.html?index.htm;??
  12. ????????#}??
  13. ????????location?/?{??
  14. ????????????proxy_pass?http://localhost:8989;??
  15. ????????}??
  16. ??
  17. ????????#error_page??404??????????????/404.html;??
  18. ??
  19. ????????#?redirect?server?error?pages?to?the?static?page?/50x.html??
  20. ????????#??
  21. ????????error_page???500?502?503?504??/50x.html;??
  22. ????????location?=?/50x.html?{??
  23. ????????????root???html;??
  24. ????????}??
  25. ??
  26. ????????#?proxy?the?PHP?scripts?to?Apache?listening?on?127.0.0.1:80??
  27. ????????#??
  28. ????????#location?~?\.php$?{??
  29. ????????#????proxy_pass???http://127.0.0.1;??
  30. ????????#}??
  31. ??
  32. ????????#?pass?the?PHP?scripts?to?FastCGI?server?listening?on?127.0.0.1:9000??
  33. ????????#??
  34. ????????#location?~?\.php$?{??
  35. ????????#????root???????????html;??
  36. ????????#????fastcgi_pass???127.0.0.1:9000;??
  37. ????????#????fastcgi_index??index.php;??
  38. ????????#????fastcgi_param??SCRIPT_FILENAME??/scripts$fastcgi_script_name;??
  39. ????????#????include????????fastcgi_params;??
  40. ????????#}??
  41. ??
  42. ????????#?deny?access?to?.htaccess?files,?if?Apache's?document?root??
  43. ????????#?concurs?with?nginx's?one??
  44. ????????#??
  45. ????????#location?~?/\.ht?{??
  46. ????????#????deny??all;??
  47. ????????#}??
  48. ????}??
  49. ??
  50. ?????server?{??
  51. ????????listen???????9977;??
  52. ????????server_name??secondProxyServer;??
  53. ??
  54. ????????#charset?koi8-r;??
  55. ??
  56. ????????#access_log??logs/host.access.log??main;??
  57. ??
  58. ????????#location?/?{??
  59. ???????????#root???html;??
  60. ????????????#index??index.html?index.htm;??
  61. ????????#}??
  62. ????????location?/?{??
  63. ????????????proxy_pass?http://localhost:8080;??
  64. ????????}??
  65. ??
  66. ????????#error_page??404??????????????/404.html;??
  67. ??
  68. ????????#?redirect?server?error?pages?to?the?static?page?/50x.html??
  69. ????????#??
  70. ????????error_page???500?502?503?504??/50x.html;??
  71. ????????location?=?/50x.html?{??
  72. ????????????root???html;??
  73. ????????}??
  74. ??
  75. ????????#?proxy?the?PHP?scripts?to?Apache?listening?on?127.0.0.1:80??
  76. ????????#??
  77. ????????#location?~?\.php$?{??
  78. ????????#????proxy_pass???http://127.0.0.1;??
  79. ????????#}??
  80. ??
  81. ????????#?pass?the?PHP?scripts?to?FastCGI?server?listening?on?127.0.0.1:9000??
  82. ????????#??
  83. ????????#location?~?\.php$?{??
  84. ????????#????root???????????html;??
  85. ????????#????fastcgi_pass???127.0.0.1:9000;??
  86. ????????#????fastcgi_index??index.php;??
  87. ????????#????fastcgi_param??SCRIPT_FILENAME??/scripts$fastcgi_script_name;??
  88. ????????#????include????????fastcgi_params;??
  89. ????????#}??
  90. ??
  91. ????????#?deny?access?to?.htaccess?files,?if?Apache's?document?root??
  92. ????????#?concurs?with?nginx's?one??
  93. ????????#??
  94. ????????#location?~?/\.ht?{??
  95. ????????#????deny??all;??
  96. ????????#}??
  97. ????}??

下面是測試的結果:
首先兩個tomcat中部署兩個服務器:



然后啟動nginx。
cmd下:start nginx

分別訪問這兩個server:
http://localhost:9922/ngtt/


http://localhost:9977/testnnnn/



本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/537959.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/537959.shtml
英文地址,請注明出處:http://en.pswp.cn/news/537959.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

sc openscmanager 失敗 5 mysql_如何增加windows服務

我以前也出現過你這個問題,用優化大師給刪了吧,后來也是重裝的,其實說是重裝也不是重裝,就是修復啦,如果你不想這樣,那可以試試這個,我沒試過用在mysql上,但別的到是用他加載過。讓程…

TemplatePart用法說明

原文:TemplatePart用法說明TemplatePart(Name"PART_Decrease", Typetypeof(RepeatButton)) 一直沒明白這是干嘛用的,搜了一下,記載一下。 以Button的定義為例: namespace System.Windows.Controls {// Summary:// Represents a…

nginx配置多個站點共用80端口

原文鏈接:https://blog.csdn.net/zhezhebie/article/details/73459874 --------------------------------------------- 配置文件下載地址:https://download.csdn.net/download/zengmingen/10462400共用80端口的,要server_name不同。如果用域…

兩點間最短路 java_AcWing 850. Dijkstra求最短路 II_Java實現含詳細注釋

import java.io.*;import java.util.Arrays;import java.util.Comparator;import java.util.PriorityQueue;public class Main {static final int N 150010;static int n, m; //結點數,邊數static int[] h, e, ne, w; //鄰接表適合表示稀疏圖,w用來存每個邊權重sta…

SQL Server如何鏈接到 Oracle并查詢其中的數據?并實現做接口

今天用Oracle的驅動教大家如何從SQL Server鏈接到Oracle. 1. 服務器上需要安裝Oracle 64位的客戶端或者服務端,安裝過程就省略了。不會的同學可以網上搜索一下安裝方法,很詳細,這里不贅述。 安裝完成后SQL Server的訪問接口上會新增”OraOLE…

Tomcat 內存調大

第一種方法:Windows下,在文件/bin/catalina.bat,Unix下,在文件/bin/catalina.sh的前面,增加如下設置:JAVA_OPTS-Xms【初始化內存大小】 -Xmx【可以使用的最大內存】需要把這個兩個參數值調大。例如&#xf…

java spring bean配置文件_Spring基于xml文件配置Bean過程詳解

這篇文章主要介紹了spring基于xml文件配置Bean過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下通過全類名來配置:class:bean的全類名,通過反射的方式在IOC容器中創建B…

win10升級后chrome碰到對話框就卡死

低版本的 chrome 會出現這樣的問題 解決方法: 設置-------高級設置-----取消硬件加速

客戶端SDK測試思路

本文來自網易云社區作者:萬春艷是什么客戶端SDK是為第三方開發者提供的軟件開發工具包,包括SDK接口、開發文檔和Demo示例等。SDK和應用之間是什么關系呢?以云信即時消息服務為例,如下圖所示,應用客戶端通過調用云信SDK…

nginx could not build the server_names_hash 解決方法

原文地址:http://www.jb51.net/article/26412.htm ------------------------------------------------------- nginx “nginx could not build the server_names_hash”解決方法 給一個服務器下增加了一些站點別名,差不多有20多個。 重啟nginx時候&#…

java 使用fusioncharts_fusioncharts同一頁面顯示2個儀表盤,且以java字符串作為xml數據...

fusioncharts同一頁面顯示2個儀表盤,且以java字符串作為xml數據String path request.getContextPath();%>String xml "";%>FusionCharts - Multiple Charts on one Pagevar contextpath "";var xml ;body {font-family: Arial, Helve…

排名前16的Java工具類

原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具類定義了一組公共方法,這篇文章將介紹Java中使用最頻繁及最通用的Java工具類。以下工具類、方法按使用流行度排名,參考數據來源于Github上隨機選取的5萬個開源項目源碼…

VS2012(Visual Studio 2012)官方免費中文旗艦版下載(含激活密鑰)

原文路徑:http://www.nocang.com/visual-studio-ultimate-2012/ vs2012旗艦版安裝激活教程 1、下載到的是ISO格式文件,直接解壓縮或用虛擬光驅加載運行;2、無所不藏推薦直接解壓縮安裝即可,雙擊“vs_ultimate.exe”進行安裝&#…

magic square java_測試Magic Square Java的.txt文件

我不想問,但我無法弄清楚這個任務,當我尋求幫助時,助教也不會。我必須從文本文件中獲取輸入,將文件中的整數輸入到數組列表中,然后測試它是否是anxn幻方。n等于數組列表長度的平方根。如果不是理想的正方形&#xff0c…

字符串拼串 能緩解我們的開發難度→!←(ε=(′ο`*)))唉,又是一個不知道該怎么寫題目的隨筆啊,頭疼)...

簡單描述:今天看我同事提交的代碼,發現一個東西,讓我有了一點小想法,是這樣的,他利用一個‘’無關緊要‘’的標簽屬性,(哈哈哈,也不能說人家是無關緊要的屬性了,暫時是無關緊要的屬性…

SQL中使用DISTINCT顯示多個字段的方法(不使用DISTINCT了)

原文連接: https://www.cnblogs.com/alanliu/archive/2008/02/25/1080626.html --------------------------------- 效果是DISTINCT CUS_NO,并且同時顯示CUS_NAME.SELECTCUS_NO,MIN(CUS_NAME) ASCUS1 FROMdbo.CUS GROUPBYCUS_NO

java 注釋快捷打出時間_Java快捷---自動注釋時間作者。。。

在使用Eclipse 編寫Java代碼時,自動生成的注釋信息都是按照預先設置好的格式生成的。修改作者、日期注釋格式:打開Windows->Preferences->Java->Code Style->Code Templates,點擊右邊窗口中的Comments,可以看到有很多…

016 pickle

英文也是泡菜的意思。 學完了,還是感覺這個模塊是蠻不錯的,對多數據保存到文件中,然后在使用的時候,再讀取出來,讓程序閑的更加優雅,簡潔。 一:介紹 1.為什么使用 在開篇已經介紹了,…

centos7與centos6區別

原文連接:https://www.cnblogs.com/bethal/p/5945026.html ---------------------------------------------------------------- CentOS 7 vs CentOS 6的不同 (1)桌面系統[CentOS6] GNOME 2.x[CentOS7] GNOME 3.x(GNOME Shell)(2)文件系統[…

用java編寫日歷添加窗口一角_Java 實訓4 編寫一個窗體程序顯示日歷

實訓要求:1.使用BorderLayout 進行總體布局2.在North 位置放置包含兩個按鈕( 上月和下月)的Panel3.在South 位置放置一個Label 用于顯示當前年份和月份4.在Center 位置放置一個顯示日歷的Panel5.顯示日歷的Panel 設置7 行7 列的GridLayout 布局,其中第1行…