nginx+tomcat 配置多域名集群

2025-10-28 14:28:06

1、将nginx安装好

2、将tomcat安装好

3、将开发好的网站发布到tomcat的webapp文件夹下

4、在tomcat的conf/server.xml按照如下配置

<Host name="www.xxxx.com" appBase="webapps" unpackWARs="true" autoDeploy="true">

         <Context path="" docBase="/root/apache-tomcat-8.0.24/webapps/xxxx" reloadable="true" >

         </Context>

         <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" />

    </Host>

其中 host标签 name属性配置你要的域名

docBase里最好配置你访问的网站项目的绝对地址

完成上述配置,tomcat已经配置完成

5、在nginx当中如下配置

配置nginx下 conf/nginx.conf文件

配置内容如下:

upstream site {    

      ip_hash;

      server 192.168.1.6:8080;

      server 192.168.1.7:8080;

    }

在http标签下配置集群,当中ip_hash是根据ip地址的hash算法来处理,意思就是1个ip会固定对应一台服务器。

server{

 listen       80;

        server_name  www.xxxx.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html;

            index  index.html index.jsp;

            proxy_pass http://site;

            proxy_set_header Host $host;

        }

}

在server当中配置以上内容

listen 80 监听80端口

server_name 监听的域名

location 系列配置

需要注意的是

proxy_pass http://你配置的集群的名字在本例子中是upstream site 中的site

proxy_set_header Host $host;//该语句将头文件中的域名信息传递给集群,这个语句是保证能过通过代理跳转多域名的关键

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢