當(dāng)前位置:首頁(yè) > IT技術(shù) > 系統(tǒng)服務(wù) > 正文

linux 安裝nginx 并實(shí)現(xiàn)簡(jiǎn)易的tomcat負(fù)載均衡
2021-10-18 17:42:40

1.安裝nginx??

  1. 執(zhí)行安裝指令 ? apt-get install nginx
  2. 查看版本? ? ?   nginx -v
  3. 啟動(dòng)nginx? ??  service nginx start
  4. 訪問頁(yè)面,輸入服務(wù)器的ip,出現(xiàn)歡迎界面

2.安裝三個(gè)tomcat,注意修改/conf/server.xml中的三處端口號(hào)

<Server port="9005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />

<Connector port="9080" protocol="HTTP/1.1"
         connectionTimeout="20000"
         redirectPort="8443" />

<Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />

3.在tomcat/webapp/ROOT中創(chuàng)建test.jsp文件,并編輯

<%@ page contentType="text/html;charset=UTF-8" %>
<%
out.print("歡迎訪問 tomcat1");
%> 

?

4.修改nginx關(guān)于負(fù)載均衡的配置

  1. vi /etc/nginx/nginx.conf?在include /etc/nginx/sites-enabled/*;的下面插入
    upstream tomcat_server  {
                    server 127.0.0.1:7080 weight=1;
                    server 127.0.0.1:8080 weight=1;
                    server 127.0.0.1:9080 weight=1;
            }
    
            server {
                    listen       80 default_server;
                    server_name  localhost;
    
                    location / {
                            proxy_pass http://tomcat_server/;
                            proxy_redirect default;
                            proxy_set_header Host $http_host;
                            proxy_set_header X-Forward-For $remote_addr;
                    }
    
                    error_page   500 502 503 504  /50x.html;
                    location = /50x.html {
                            root   html;
                    }
            }

    ?

  2. 重啟service nginx restart
  3. 出現(xiàn)報(bào)錯(cuò),把/etc/nginx/sites-enabled/default中的內(nèi)容都注釋掉,再重啟
  4. 訪問http://IP/test.jsp

?

本文摘自 :https://www.cnblogs.com/

開通會(huì)員,享受整站包年服務(wù)立即開通 >