假設(shè)場(chǎng)景:
現(xiàn)有兩臺(tái)服務(wù)器
服務(wù)器1內(nèi)網(wǎng)IP:192.168.1.123(搭建了XX項(xiàng)目,只允許內(nèi)網(wǎng)訪問(wèn))
服務(wù)器2外網(wǎng)IP:119.29.23.23
現(xiàn)在要實(shí)現(xiàn)訪問(wèn)119.29.23.23:5678/demo/時(shí),反向代理到192.168.1.123:80,nginx的配置為:
server{
listen 5678;
server_name 119.29.23.23:5678;
location /demo/(.*) {
root /home/web;
proxy_pass http://192.168.1.123:80/$1?$args; #url復(fù)寫到代理,并掛上參數(shù)列表
proxy_redirect http://192.168.1.123/ http://119.29.23.23:5678/demo/; #將refresh和location的url重寫
}
}
配置解析
proxy_pass解析
實(shí)例:
如果訪問(wèn)119.29.23.23:5678/demo/index.php?name=xiaoming&msg=hello
將被定向至192.168.1.123:80/demo/index.php?name=xiaoming&msg=helloproxy_redirect解析
如果請(qǐng)求有重定向Location:http://192.168.1.123/sayhi.php,那么回到客戶端時(shí)會(huì)被改成http://119.29.23.23:5678/demo/sayhi.php