当服务器采用nginx+tomcat时,nginx便成了tomcat的客户端,这时需要配置proxy_pass进行请求的转发。在使用tomcat作为web应用程序的服务器时,nginx配置大致如下:
server{
listen 80;
server_name localhost;
root /usr/local/tomcat/webapps/;
index index.html index.htm index.jsp;
location ~\.(jsp|do)${
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-client-IP $remote_addr;
}
}
需要特别说明的是,因为nginx作为tomcat的客户端,所以在默认情况下只能获取到服务器本身的Host消息头,故需要通过$host和$remote_addr将用户客户端的Host消息头经nginx传递给tomcat,tomcat下运行的应用程序才能获取到用户客户端的消息头信息。