ubuntu下Nginx+Tomcat配置多个域名,多个站点

相信各位此时已经熟悉Nginx和Tomcat的配置文件修改和启动、停止。
那就直接上配置了:

vim /etc/nginx/nginx.conf # 打开nginx的配置文件

大家将下面的http{}里面的内容复制到自己的http{}里面,然后修改相应的域名

http{

    upstream tomcat {
       server localhost:8080;
    }

    server {

        listen 80;
        server_name www.*.com;
        location / {
            proxy_pass  http://tomcat;
            proxy_redirect default;
            #设置代理
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        } #将所有访问请求转发给tomcat进行处理

    }

    server {

       listen 80;
       server_name www.*.net;
       location / {
          proxy_pass  http://tomcat;
          proxy_redirect default;
        #设置代理
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
        } #将所有访问请求转发给tomcat进行处理

   }

}

Nginx到此配置完毕,下面进入tomcat的配置

root@xxx:/usr/local/lib/tomcat-8# vim conf/server.xml  #打开编辑server.xml文件

删去或者注释掉默认的Host节点,加入下面两个节点,其中修改对应自己的name中的域名和Context节点的属性docBase中的项目名,也就是你发布在webapps下面的war包名称

<Host name="www.*.com" appBase="webapps" unpackWARs="true"
        autoDeploy="true">
        <Context path="" docBase="项目名" debug="0" reloadable="true"
            crossContext="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" />
    </Host>

    <Host name="www.*.net" appBase="webapps" unpackWARs="true"
        autoDeploy="true">
        <Context path="" docBase="项目名" debug="0" reloadable="true" crossContext="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" />
    </Host> 

修改完毕保存文件,重启tomcat,重启Nginx

打开浏览器直接访问你的域名即可.

猜你喜欢

转载自blog.csdn.net/qq_37604508/article/details/78868504
今日推荐