需求场景:由于服务器只有80端口可供外网访问,但需要部署两台tomcat来运行两个不同的项目,所以选择了nginx做反向代理
一个upstream tomcat_server对应一个 location{ } 配置
#第一台tomcat,8080端口
upstream tomcat_server1 {
ip_hash;
server localhost:8080 ;
}
#第二台tomcat,7080端口
upstream tomcat_server2 {
ip_hash;
server localhost:7080 ;
}
#一个server只能监听一个端口号,切记!!!
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#配置访问路径,
location ^~ /web1/ {
#设置对应tomcat端口跳转路径
proxy_pass http://tomcat_server1/web1/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#配置访问路径
location ^~ /web2/ {
#设置对应tomcat端口跳转路径
proxy_pass http://tomcat_server2/web2/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}