Jsp Nginx ·

nginx配置之tomcat支持多域名

我们知道,用nginx来配置多域名很常见,也很容易,但在java环境中使用tomcat时,怎么配置多域名呢?

多数情况下我们只有一个tomcat,并不想启动多个tomcat,只是简单的把应用 放到一个tomcat下,使用上下文来访问,

现告诉大家怎么配置:

1. 先配置nginx

user  www www;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    use epoll;
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    include gzip.conf;

    server {
        listen       80;
        server_name  localhost;

        location / {
	        deny all;
        }

        location ~ ^/NginxStatus {
            stub_status on;
            access_log off;
            allow   127.0.0.1;
            allow   42.96.200.198;
            deny    all;
        }

    }

    server {
        listen 80;
        server_name www.tojsp.com;

        access_log  logs/tojsp.log;

	location ~ ^/NginxStatus {
            stub_status on;
            access_log off;
        }

	location ~ ^/(WEB-INF)/ {
            deny all;
        }

        location ~ \.(gif|jpg|jpeg|png)$ {
            root /xdata/www/resource/tojsp;
	    access_log off;
            expires 24h;
        }

        location / {
            proxy_pass http://localhost:8080;
            include proxy.conf;
        }
    }

    server {
        listen 80;
        server_name it.tojsp.com;

        access_log  logs/tojsp.log;

	location ~ ^/NginxStatus {
            stub_status on;
            access_log off;
        }

	location ~ ^/(WEB-INF)/ {
            deny all;
        }

        location ~ \.(gif|jpg|jpeg|png)$ {
            root /xdata/www/resource/tojsp;
	    access_log off;
            expires 24h;
        }

        location / {
            proxy_pass http://localhost:8080;
            include proxy.conf;
        }
    }
}

2. 再配置tomcat中的server.xml, 找到Host节点

<Host name="www.tojsp.com"  appBase="/xdata/www/tojsp.com"
    unpackWARs="true" autoDeploy="true">

    <Context path="" docBase="/xdata/www/tojsp.com" reloadable="true" /> 
</Host>
<Host name="it.tojsp.com"  appBase="/xdata/www/it.tojsp.com"
    unpackWARs="true" autoDeploy="true">

    <Context path="" docBase="/xdata/www/it.tojsp.com" reloadable="true" /> 
</Host>

3.最后启动nginx与tomcat看看效果,是不是成功了。

参与评论