需求場景:因爲服務器只有80端口可供外網訪問,但須要部署兩臺tomcat來運行兩個不一樣的項目,因此選擇了nginx作反向代理nginx
一個upstream tomcat_server對應一個 location{ } 配置web
#第一臺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;tomcat
#配置默認跳轉路徑,須要打開nginx的地址重定向功能 rewrite,好像默認是關閉的。
location = / {
rewrite ^(.*) http://IP/web1 permanent;
limit_conn one 1;
limit_rate 20k;
}
#配置訪問路徑, web一、web2爲具體的項目名
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;
}
服務器