準備工作
先配置一個tomcat服務器,我這裏使用docker進行配置:
docker pull tomcat
mkdir ~/tomcat
docker run -id --name=tomcat1 -p 8080:8080 -v ~/tomcat:/usr/local/tomcat/webapps tomcat
在/tomcat/test目錄下創建index.html:
訪問ip:8080:test/index.html
實列一
需求:訪問nginx的80端口,自動跳轉到tomcat的test/Index.html
配置server中的location的proxy_pass屬性
server {
listen 80;
server_name xxxxx;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://xxxxxx:8080/test/index.html;
}
}
再去訪問即可成功
實例二
- 需求:
- 準備工作
準備兩個tomcat服務器,一個運行在8080,一個運行在8081
配置server塊:
server {
listen 80;
server_name xxxxx;
location ^~ /tomcat1/ {
proxy_pass http://xxxxx:8080/test/index.html;
}
location ^~ /tomcat2/ {
proxy_pass http://xxxxx:8081/test/index.html;
}
}
其中~/表示會轉發含有tomcat1的路徑的請求
常用命令
proxy_redirect off;
重寫後端服務器的location和refresh頭。
proxy_set_header Host $host;
重寫發送給後端服務器的請求頭內容。
proxy_connect_timeout 300;
代理服務器接收請求到連接後端服務器的最長等待時間。
proxy_buffer_size 4k;
後端響應的緩衝區數量和大小。
proxy_buffers 4 32k;
請求後端的緩衝區數量和大小。
proxy_busy_buffers_size 64k;
當代理服務器忙時,緩衝區的最大值。
proxy_send_timeout 300
將超時與請求傳輸到代理服務器分配。
proxy_read_timeout 300
NGINX等待獲取請求響應的時間。