nginx tomcat、springboot 配置


1、環境

兩台(centos)機器,每台機器跑2個web應用

ip web
192.168.0.109 http://192.168.0.109:8081
192.168.0.109 http://192.168.0.109:8082
192.168.0.111 http://192.168.0.111:8083
192.168.0.111 http://192.168.0.111:8084

 

 

 

 

 

 

 

 

 

注:由於我本機是ubuntu系統,我是在ubuntu系統上面安裝的虛擬機,跑了2個centos7,如果發現截圖怎么那么像ubuntu的色調,別感到意外,以下的截圖均為centos7,具體型號為centos7-1810 ,web應用的准備工具為idea

 

2、web應用准備

為了便於驗證,配置了一個干凈的springboot應用

代碼請參考spring官網:https://spring.io/guides/gs/rest-service/

在此基礎上進行修改,只是修改了返回內容,返回當前應用的端口(4個應用的端口分別為8081,8082,8083,8084)

3、兩台機器均安裝nginx服務

可以在官網查看安裝步驟,比如我是2台centos機器

http://nginx.org/en/linux_packages.html#RHEL-CentOS

安裝步驟參考官網進行安裝

4、配置nginx

安裝完成后,配置文件位置為:/etc/nginx

修改nginx.conf

vim nginx.conf

在http的{}中添加一下內容:

server{
    #nginx端口
    listen 8080;
   #服務名稱
    server_name 192.168.0.109;
    location / {
    #轉發真實IP到 header中,如果不需要可以將此行刪除,后台獲取IP代碼為: String ip=request.getHeader("real-ip");
    proxy_set_header real-ip $remote_addr; #proxy_pass 后邊的地址需要與下方的upstream后邊的名稱一樣 #此處為tomcats proxy_pass http:
//tomcats; root html; index index.html index.htm; } } #app的配置 upstream tomcats{ #weight為權重,值越大,分配的就越多 server 192.168.0.109:8081 weight=10; server 192.168.0.109:8082 weight=10; server 192.168.0.111:8083 weight=10; server 192.168.0.111:8084 weight=10; }

兩台機器均要配置

 

 

5、啟動nginx

我是在centos中根據官方文檔在線安裝,所以,命令可以直接調用

nginx

#停止命令  nginx -s stop

如下圖:

證明nginx已經啟動起來了,訪問nginx,不斷的刷新頁面,會發現四個web應用均會被分配到:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM