一、需求
假如,我們需要這樣的代理,平時我們運行調試的時候,就在localhost的默認目錄下的html里面就行。如果我們請求的路徑含有指定的目錄的時候,我們需要它去我們指定的域名和端口請求數據,而不是在本地請求。
比如,當請求http://localhost/extService/extService/wens001.do
的時候,我們是想他遠程訪問地址是http://192.168.2.1:8080/extService/wens001.do
,而不是在我們本地查找該文件。
二、解決辦法1
打開你的nginx配置文件,比如我的地址:nginx-1.17.9\conf\nginx.conf
。在原本的location下面添加一個如下代碼的location
location ^~ /extService/ {
proxy_pass http://192.168.2.1:8080/extService/;
}
這樣配置之后,后面的對/extService/
請求,都會被轉發到你配置的域名中去了。
^ 號表示開頭匹配,以為我的開頭就是根目錄,所以,我加了僅僅限制開頭的這種限制,防止其他的域名中間有該字符串,也會被轉發的錯誤。
配置完成之后,大致代碼如下,僅供參考
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location ^~ /extService/ {
proxy_pass http://192.168.2.1:8080/extService/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
三、解決辦法2
你還可以使用下面的代碼,這種,對轉發的路徑有要求,只能是域名和端口號。
location ~ /extService/ {
proxy_pass http://blog.test:8080;
}
proxy_pass的路徑不能帶有路徑。它是匹配到該種路徑,直接轉發。