使用Nginx将子域连接到tomcat7 war实例

我需要在Ubuntu上使用Nginx webserver连接一个war实例(war-demo.war)和clojure2.msms.com

这就是我所做的。

1.设置nginx / tomcat

/etc/nginx/sites-enabled ,我设置了tomcat.msms.com连接到tomcat7。 Tomcat7与apt-get一起安装( https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-7-on-ubuntu-14-04-via-apt-get )。 我也检查/etc/init.d/tomcat7是否正确安装。

 # the IP(s) on which your node server is running. I chose port 3000. upstream tomcat { server 127.0.0.1:8008; keepalive 8; } # the nginx server instance server { listen 0.0.0.0:80; server_name tomcat.msms.com; access_log /var/log/nginx/access_tomcat.log; error_log /var/log/nginx/error_tomcat.log; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://tomcat/; proxy_redirect off; } location /excluded { return 403; } } 

2.战争文件设置

我将warfile upload到/var/lib/tomcat7/webapps以便tomcat.msms.com/war-demo显示正确的结果。

 /var/lib/tomcat7/webapps> ls ROOT war-demo.war 

3.将clojure2.msms.com连接到war-demo.war

我创build了一个CNAME clojure2.msms.com并在/etc/nginx/sites-enabledubuntu目录中添加了这个configuration文件。

 server { listen 80; server_name clojure2.msms.com; location / { proxy_pass http://127.0.0.1:8008/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 

然后我修改了/etc/tomcat7/server.xml

  146 │ <Host name="clojure2.msms.com" appBase="webapps" 147 │ unpackWARS="true" autoDeploy="true"> 148 │ <Context path="" docBase="war-demo" debug="0" privileged="true" /> 149 │ <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 150 │ prefix="localhost_access_log." suffix=".txt" 151 │ pattern="%h %l %u %t &quot;%r&quot; %s %b" resolveHosts="false" /> 152 │ </Host> 

我重新启动与sudo service tomcat7 restart启动tomcat服务器,我似乎已经从pipe理器安装好clojure2.msms.com

在这里输入图像说明

clojure2.msms.com链接显示默认的tomcat7服务器,而不是war-demo.war实例。 我的configuration有什么问题?

这可能是解决scheme之一。

创build另一个服务

  <Service name="Clojure"> <Connector port="8007" protocol="HTTP/1.1" connectionTimeout="20000" URIEncoding="UTF-8" redirectPort="8442" /> <Engine name="Catalina" defaultHost="clojure2"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="clojure2" appBase="webapps2" unpackWARS="true" autoDeploy="true"> </Host> </Engine> </Service> 

基本目录是webapps2,它使用一个新的端口8007。

使clojure2.conf使用任何其他端口。

/etc/nginx/sites-enabled ,创build一个新的nginx服务。

 server { listen 80; server_name clojure2.msms.com; location / { proxy_pass http://127.0.0.1:8007/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 

将war文件复制到webapps2目录中

将war文件复制到/var/lib/tomcat7/webapps2目录中。 不要忘记把war文件的名字改成ROOT.war。

重新启动tomcat

 tomcat7> sudo service tomcat7 restart * Stopping Tomcat servlet engine tomcat7 [ OK ] * Starting Tomcat servlet engine tomcat7 [ OK ] 

可能有比这更好的解决scheme,但是这个解决scheme正如我所料。