通过nginx访问tomcat中SpringMVC应用&同一tomcat中部署多个SpringMVC应用

通过nginx访问tomcat中SpringMVC应用

&同一tomcat中部署多个SpringMVC应用

nginx简介

Nginx,也被称为"engine x",是一个开源并且支持高性能和高并发的Web服务和代理服务软件。这款软件由俄罗斯人Igor Sysoev开发并首次在俄罗斯的大型网站www.rambler.ru上应用。它不仅可以作为HTTP服务器运行,还可以作为反向代理服务器使用,同时还提供了IMAP/POP3/SMTP等服务。Nginx因其轻量级和高效性在全球范围内得到了广泛的应用。

Linux 安装 nginx

这里安装nginx就不多说了,参考文档:Linux 安装 Nginx 并配置为系统服务(超详细)-CSDN博客

tomcat部署SpringMVC应用

  1. 将SpringMVC应用打war包,通过idea的maven打包项目

    image-20231122204557462

  2. 将 打包好的war包放到tomcat根目录下的webapp

    image-20231122204926969

    image-20231122204910265

  3. 若在同一个tomcat部署多个项目,首先把webapp文件夹拷贝一份取名为webapp2,然后修改conf/server.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    
    <Server port="8005" shutdown="SHUTDOWN">
      <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
      <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
      <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
      <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
      <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
    
      <GlobalNamingResources>
        <Resource name="UserDatabase" auth="Container"
                  type="org.apache.catalina.UserDatabase"
                  description="User database that can be updated and saved"
                  factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
                  pathname="conf/tomcat-users.xml" />
      </GlobalNamingResources>
      <!--这是第一个webapp项目-->
      <Service name="Catalina">  <!--两个webapp项目Service name要不同-->
        <Connector port="18080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443"
                   maxParameterCount="1000"
                   />  <!--两个webapp项目的port要不同-->
        <Engine name="Catalina" defaultHost="localhost">
          <Realm className="org.apache.catalina.realm.LockOutRealm">
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                   resourceName="UserDatabase"/>
          </Realm>
    
          <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
            <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                   prefix="localhost_access_log" suffix=".txt"
                   pattern="%h %l %u %t &quot;%r&quot; %s %b" />
           	<Context docBase="/opt/moiveproject/tomcat9/webapps/Movie" path="/" debug="0" reloadable="true"/>  
            <!--docBase的内容是你war包的位置, MovieManager是war包的名字,不带.war-->
          </Host>
        </Engine>
      </Service>
      <!--这是第二个webapp项目-->
      <Service name="Catalina2">   <!--两个webapp项目Service name要不同-->
        <Connector port="8080" protocol="HTTP/1.1"  
                   connectionTimeout="20000"
                   redirectPort="8443" />  <!--两个webapp项目的port要不同-->
        <Connector port="8010" protocol="AJP/1.3" redirectPort="8445" />
        <Engine name="Catalina" defaultHost="localhost">
          <Realm className="org.apache.catalina.realm.LockOutRealm">
            <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                   resourceName="UserDatabase"/>
          </Realm>
    
          <Host name="localhost"  appBase="webapps2"
                unpackWARs="true" autoDeploy="true">  <!--appBase的内容要与你的文件名一致-->
            <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                   prefix="localhost_access_log" suffix=".txt"
                   pattern="%h %l %u %t &quot;%r&quot; %s %b" />
            <Context docBase="/opt/moiveproject/tomcat9/webapps2/MovieManager" path="/" debug="0" reloadable="true"/> 
            <!--docBase的内容是你war包的位置, MovieManager是war包的名字,不带.war-->
          </Host>
        </Engine>
      </Service>
      
    </Server>
    
    

    image-20231122205930617

  4. /tomcat9/bin/startup.sh 启动tomcat访问对应端口号即可访问应用

nginx访问SpringMVC应用

  1. 在nginx的配置文件目录下修改/nginx/conf/nginx.conf文件

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        root /opt/moiveproject/moive_webapp;
        index Home.jsp Home.jspx Home.do;
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
        
        log_format  main '$status $http_referer';
        sendfile        on;
        keepalive_timeout  65;
    
        server {
            listen       80;
            server_name  niit01;
    
            location /assets/  #静态分离
            {
                root /opt/moiveproject/moive_webapp;  # 静态资源所放位置
            }
           
             location /
            {
                #==========Nginx提供的代理============
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                #=== 如果遇到.jsp .jspx .do .action 的请求就进入该服务器(tomcat)===
                proxy_pass http://niit01:18080;
                # 日志文件位置
    	       access_log /opt/moiveproject/movieLog/access.log main;
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    }
    
  2. 重启nginx,访问niit01:80/即可访问应用

参考文档:

springmvc搭配nginx 实现动静分离 - 贾树丙 - 博客园 (cnblogs.com)

通过nginx访问tomcat中web应用的jsp网页_nginx配置jsp首页-CSDN博客

nginx安装和简单部署java web项目_nginx部署web项目-CSDN博客

服务器使用Nginx部署Springboot项目(jar包)-CSDN博客

Nginx部署JavaWeb项目-CSDN博客

Nginx服务器之Nginx与tomcat结合访问jsp - 小禾点点 - 博客园 (cnblogs.com)

  • 15
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值