• Nignx及负载均衡&动静分离


    目录

    一.Nginx负载均衡

    1.1.下载

    1.2.安装

    1.3.负载均衡

    二.前端部署

    2.1. 准备工作

    2.2.部署

             好啦今天就到这里了哦!!!希望能帮到你哦!!!


    一.Nginx负载均衡

    1.1.下载

    输入命令 :  cd javaCloudJun/software  进入到资源文件目录

    安装 Nginx 的4个依赖

    输入命令 : yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

    输入命令 :  tar -xvf nginx-1.13.7.tar.gz   ( 解压 Nginx

     

    解压后进入其文件

    命令 :  cd nginx-1.13.7  

    编译,执行配置: 考虑到后续安装ssl证书 添加两个模块

    命令 : ./configure --with-http_stub_status_module --with-http_ssl_module

    1.2.安装

    之后进行安装

    命令 :   make && make install

    安装完后,进入该目录

    输入命令 :  cd /usr/local/nginx

    进入 /usr/local/nginx/sbin 目录下启动:

    输入命令 :  cd sbin/

    启动前安装 lsof 命令

    输入命令 :  yum install -y  lsof

    设置防火墙 开放 80 端口

    输入命令 : firewall-cmd --zone=public --add-port=80/tcp --permanent

    更新防火墙的端口并且查看已开放的端口

    输入命令 :  firewall-cmd --reload && firewall-cmd --list-port

    # 启动

    命令 :  ./nginx

    #查看 

    命令 :  lsof -i:80

    在浏览器中,输入虚拟机【Linux】-Centos的IP地址进行搜索

    以上就是Nginx 的使用配置并且开启完成了哦。

    1.3.负载均衡

    在资源文件夹中,创建一个tomcat文件夹,来存放Tomcat

    输入命令 :  mkdir tomcat

    并且将tomcat服务解压到指定目录,刚刚创建的tomcat文件夹中。

    输入命令 : tar -xvf apache-tomcat-8.5.tar.gz -C tomcat

    进入tomcat文件夹中

    命令 :  cd tomcat/

    复制一个tomcat,准备2个tomcat

    命令 : cp -r apache-tomcat-8.5.20/ apache-tomcat-8.5.20_8081/

    查看命令 : ll

    将其中的一个tomcat修改端口,避免两个服务同时开启时端口被占用的情况。

    命令 :  cd apache-tomcat-8.5.20/conf   ( 进入到tomcat的conf文件中 )

    找到server.xml 文件进行修改端口

    命令 : vim server.xml   ( 编辑文件修改端口 )

     

    修改端口号:

     

     修改后的所以代码 : 

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!--
    3. Licensed to the Apache Software Foundation (ASF) under one or more
    4. contributor license agreements. See the NOTICE file distributed with
    5. this work for additional information regarding copyright ownership.
    6. The ASF licenses this file to You under the Apache License, Version 2.0
    7. (the "License"); you may not use this file except in compliance with
    8. the License. You may obtain a copy of the License at
    9. http://www.apache.org/licenses/LICENSE-2.0
    10. Unless required by applicable law or agreed to in writing, software
    11. distributed under the License is distributed on an "AS IS" BASIS,
    12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13. See the License for the specific language governing permissions and
    14. limitations under the License.
    15. -->
    16. <!-- Note: A "Server" is not itself a "Container", so you may not
    17. define subcomponents such as "Valves" at this level.
    18. Documentation at /docs/config/server.html
    19. -->
    20. <Server port="8006" shutdown="SHUTDOWN">
    21. <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
    22. <!-- Security listener. Documentation at /docs/config/listeners.html
    23. <Listener className="org.apache.catalina.security.SecurityListener" />
    24. -->
    25. <!--APR library loader. Documentation at /docs/apr.html -->
    26. <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
    27. <!-- Prevent memory leaks due to use of particular java/javax APIs-->
    28. <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
    29. <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
    30. <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
    31. <!-- Global JNDI resources
    32. Documentation at /docs/jndi-resources-howto.html
    33. -->
    34. <GlobalNamingResources>
    35. <!-- Editable user database that can also be used by
    36. UserDatabaseRealm to authenticate users
    37. -->
    38. <Resource name="UserDatabase" auth="Container"
    39. type="org.apache.catalina.UserDatabase"
    40. description="User database that can be updated and saved"
    41. factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
    42. pathname="conf/tomcat-users.xml" />
    43. </GlobalNamingResources>
    44. <!-- A "Service" is a collection of one or more "Connectors" that share
    45. a single "Container" Note: A "Service" is not itself a "Container",
    46. so you may not define subcomponents such as "Valves" at this level.
    47. Documentation at /docs/config/service.html
    48. -->
    49. <Service name="Catalina">
    50. <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    51. <!--
    52. <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
    53. maxThreads="150" minSpareThreads="4"/>
    54. -->
    55. <!-- A "Connector" represents an endpoint by which requests are received
    56. and responses are returned. Documentation at :
    57. Java HTTP Connector: /docs/config/http.html
    58. Java AJP Connector: /docs/config/ajp.html
    59. APR (HTTP/AJP) Connector: /docs/apr.html
    60. Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    61. -->
    62. <Connector port="8081" protocol="HTTP/1.1"
    63. connectionTimeout="20000"
    64. redirectPort="8443" />
    65. <!-- A "Connector" using the shared thread pool-->
    66. <!--
    67. <Connector executor="tomcatThreadPool"
    68. port="8080" protocol="HTTP/1.1"
    69. connectionTimeout="20000"
    70. redirectPort="8443" />
    71. -->
    72. <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
    73. This connector uses the NIO implementation. The default
    74. SSLImplementation will depend on the presence of the APR/native
    75. library and the useOpenSSL attribute of the
    76. AprLifecycleListener.
    77. Either JSSE or OpenSSL style configuration may be used regardless of
    78. the SSLImplementation selected. JSSE style configuration is used below.
    79. -->
    80. <!--
    81. <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
    82. maxThreads="150" SSLEnabled="true">
    83. <SSLHostConfig>
    84. <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
    85. type="RSA" />
    86. </SSLHostConfig>
    87. </Connector>
    88. -->
    89. <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
    90. This connector uses the APR/native implementation which always uses
    91. OpenSSL for TLS.
    92. Either JSSE or OpenSSL style configuration may be used. OpenSSL style
    93. configuration is used below.
    94. -->
    95. <!--
    96. <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
    97. maxThreads="150" SSLEnabled="true" >
    98. <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
    99. <SSLHostConfig>
    100. <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
    101. certificateFile="conf/localhost-rsa-cert.pem"
    102. certificateChainFile="conf/localhost-rsa-chain.pem"
    103. type="RSA" />
    104. </SSLHostConfig>
    105. </Connector>
    106. -->
    107. <!-- Define an AJP 1.3 Connector on port 8009 -->
    108. <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
    109. <!-- An Engine represents the entry point (within Catalina) that processes
    110. every request. The Engine implementation for Tomcat stand alone
    111. analyzes the HTTP headers included with the request, and passes them
    112. on to the appropriate Host (virtual host).
    113. Documentation at /docs/config/engine.html -->
    114. <!-- You should set jvmRoute to support load-balancing via AJP ie :
    115. <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    116. -->
    117. <Engine name="Catalina" defaultHost="localhost">
    118. <!--For clustering, please take a look at documentation at:
    119. /docs/cluster-howto.html (simple how to)
    120. /docs/config/cluster.html (reference documentation) -->
    121. <!--
    122. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    123. -->
    124. <!-- Use the LockOutRealm to prevent attempts to guess user passwords
    125. via a brute-force attack -->
    126. <Realm className="org.apache.catalina.realm.LockOutRealm">
    127. <!-- This Realm uses the UserDatabase configured in the global JNDI
    128. resources under the key "UserDatabase". Any edits
    129. that are performed against this UserDatabase are immediately
    130. available for use by the Realm. -->
    131. <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
    132. resourceName="UserDatabase"/>
    133. </Realm>
    134. <Host name="localhost" appBase="webapps"
    135. unpackWARs="true" autoDeploy="true">
    136. <!-- SingleSignOn valve, share authentication between web applications
    137. Documentation at: /docs/config/valve.html -->
    138. <!--
    139. <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    140. -->
    141. <!-- Access log processes all example.
    142. Documentation at: /docs/config/valve.html
    143. Note: The pattern used is equivalent to using pattern="common" -->
    144. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
    145. prefix="localhost_access_log" suffix=".txt"
    146. pattern="%h %l %u %t "%r" %s %b" />
    147. </Host>
    148. </Engine>
    149. </Service>
    150. </Server>

    注 : 将该服务器tomcat的所以端口都进行了修改,以免其他所有端口也有冲突。

    并且将这个服务器的页面显示内容进行修改,访问时容易分辨是哪个tomcat端口进入的

    在tomcat根目录的webapps中的ROOT目录中,找到index.jsp文件,在MobaXterm工具的左边选中这个文件,右键点击第二个进行打开文件,并且修改编辑文件。

    编辑后的所有内容如下  : 

    1. <%--
    2. Licensed to the Apache Software Foundation (ASF) under one or more
    3. contributor license agreements. See the NOTICE file distributed with
    4. this work for additional information regarding copyright ownership.
    5. The ASF licenses this file to You under the Apache License, Version 2.0
    6. (the "License"); you may not use this file except in compliance with
    7. the License. You may obtain a copy of the License at
    8. http://www.apache.org/licenses/LICENSE-2.0
    9. Unless required by applicable law or agreed to in writing, software
    10. distributed under the License is distributed on an "AS IS" BASIS,
    11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. See the License for the specific language governing permissions and
    13. limitations under the License.
    14. --%>
    15. <%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
    16. <%
    17. java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
    18. request.setAttribute("year", sdf.format(new java.util.Date()));
    19. request.setAttribute("tomcatUrl", "http://tomcat.apache.org/");
    20. request.setAttribute("tomcatDocUrl", "/docs/");
    21. request.setAttribute("tomcatExamplesUrl", "/examples/");
    22. %>
    23. <!DOCTYPE html>
    24. <html lang="en">
    25. <head>
    26. <meta charset="UTF-8" />
    27. <title><%=request.getServletContext().getServerInfo() %></title>
    28. <link href="favicon.ico" rel="icon" type="image/x-icon" />
    29. <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
    30. <link href="tomcat.css" rel="stylesheet" type="text/css" />
    31. </head>
    32. <body>
    33. <h1>8081</h1>
    34. </body>
    35. </html>

    返回到tomcat目录,并且开其两个服务(tomcat)

    命令:  cd apache-tomcat-8.5.20/bin  ( 进入到tomcat的bin目录中 )

     命令:  ./startup.sh   ( 开启服务 )

    再到 nginx的目录中

    命令:  cd /usr/local/nginx

    并且进入 : sbin目录

    输入 :  cd sbin

    命令 :  ./nginx -s reload   

    注 : 重新开启nginx

    之后在浏览器中访问,就有两个服务在运行用一个了

    8080:

    8081:

    二.前端部署

    2.1. 准备工作

    前端项目打包之前需要增加以下的设置

    在前端项目中 config文件下的index.js中要增加以下代码 : 

     assetsPublicPath: './',//修改后

    在前端项目中 build文件下的 utils.js 中增加以下代码 : 


     // 解决icon路径加载错误
            publicPath:'../../'

    在前端项目的跟目录中,cmd打开命令窗口

    输入命令 : npm run build   ( 进行前端项目打包 )

    命令执行后,会出现如图中以下文件  dist

    在 /usr/local/ 目录下创建一个文件夹,为mypor ,并且进入文件夹,之后将dist文件拖入mypor文件夹中

    并且选中zip解压的命令 :  yum install -y unzip

    将该文件进行解压 

    输入命令 : unzip dist.zip

    2.2.部署

    输入命令 :  cd /usr/local/nginx/conf/    找到nginx.conf进行编辑

    以下是所有代码 :  

    1. #user nobody;
    2. worker_processes 1;
    3. #error_log logs/error.log;
    4. #error_log logs/error.log notice;
    5. #error_log logs/error.log info;
    6. #pid logs/nginx.pid;
    7. events {
    8. worker_connections 1024;
    9. }
    10. http {
    11. include mime.types;
    12. default_type application/octet-stream;
    13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    14. # '$status $body_bytes_sent "$http_referer" '
    15. # '"$http_user_agent" "$http_x_forwarded_for"';
    16. #access_log logs/access.log main;
    17. sendfile on;
    18. #tcp_nopush on;
    19. #keepalive_timeout 0;
    20. keepalive_timeout 65;
    21. #gzip on;
    22. #服务器的集群
    23. upstream tomcat_list { #服务器集群名字
    24. server 127.0.0.1:8080 weight=1; #服务器1 weight是权重的意思,权重越大,分配的概率越大。
    25. server 127.0.0.1:8081 weight=2; #服务器2 weight是权重的意思,权重越大,分配的概率越大
    26. }
    27. server {
    28. listen 80;
    29. server_name localhost;
    30. #charset koi8-r;
    31. #access_log logs/host.access.log main;
    32. location / {
    33. root /usr/local/mypro/dist;
    34. #proxy_pass http://tomcat_list;
    35. index index.html index.htm;
    36. }
    37. location ^~/api/ {
    38. #^~/api/表示匹配前缀是api的请求,proxy_pass的结尾有/, 则会把/api/*后面的路径直接拼接到后面,即移除api
    39. proxy_pass http://tomcat_list/;
    40. }
    41. #error_page 404 /404.html;
    42. # redirect server error pages to the static page /50x.html
    43. #
    44. error_page 500 502 503 504 /50x.html;
    45. location = /50x.html {
    46. root html;
    47. }
    48. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    49. #
    50. #location ~ \.php$ {
    51. # proxy_pass http://127.0.0.1;
    52. #}
    53. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    54. #
    55. #location ~ \.php$ {
    56. # root html;
    57. # fastcgi_pass 127.0.0.1:9000;
    58. # fastcgi_index index.php;
    59. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    60. # include fastcgi_params;
    61. #}
    62. # deny access to .htaccess files, if Apache's document root
    63. # concurs with nginx's one
    64. #
    65. #location ~ /\.ht {
    66. # deny all;
    67. #}
    68. }
    69. # another virtual host using mix of IP-, name-, and port-based configuration
    70. #
    71. #server {
    72. # listen 8000;
    73. # listen somename:8080;
    74. # server_name somename alias another.alias;
    75. # location / {
    76. # root html;
    77. # index index.html index.htm;
    78. # }
    79. #}
    80. # HTTPS server
    81. #
    82. #server {
    83. # listen 443 ssl;
    84. # server_name localhost;
    85. # ssl_certificate cert.pem;
    86. # ssl_certificate_key cert.key;
    87. # ssl_session_cache shared:SSL:1m;
    88. # ssl_session_timeout 5m;
    89. # ssl_ciphers HIGH:!aNULL:!MD5;
    90. # ssl_prefer_server_ciphers on;
    91. # location / {
    92. # root html;
    93. # index index.html index.htm;
    94. # }
    95. #}
    96. }

    输入命令 : cd /usr/local/nginx/sbin/   

     输入命令 :./nginx -s reload   重启nginx

    在将后端的war包 传入tomcat服务器中。

    并且输入命令 :   ./startup.sh    (开启访问)

    在浏览器中进行访问 使用虚拟机的IP加tomcat的端口

    在到文件资源管理器中进入到 以下本地目录

    C:\Windows\System32\drivers\etc

    找到 hosts 文件进行修改IP的请求

    在进浏览器中进行访问 使用虚拟机的IP加tomcat的端口,即可哦!!!

             好啦今天就到这里了哦!!!希望能帮到你哦!!!

  • 相关阅读:
    力扣(LeetCode)31. 下一个排列(C语言)
    数据库连接池长时间不用,乍一用还用不了,结果是防火墙的锅
    候选键的确定方法-如何判断属性集U的子集K是否为候选键、如何找到关系模式的候选键
    (附源码)计算机毕业设计SSM敬老院信息管理系统
    SpringBoot学习目录
    游戏模板:MFPS 2.0: Multiplayer FPS
    鸿蒙应用程序入口UIAbility详解
    java数据结构与算法刷题-----LeetCode101:对称二叉树
    ES选举:Elasticsearch中Master选举完全解读
    mysql面试题14:讲一讲MySQL中什么是全同步复制?底层实现?
  • 原文地址:https://blog.csdn.net/m0_74915426/article/details/134253934