nginx location配置前后端地址

location 后面地址的说明

location /api/esbapi nginx 会对前端访问的地址进行截取,例如前端访问地址是 http://localhost:4800/api/esbapi/manager/cm0004 那么截取后 剩余的地址为 /manager/cm0004
,然后再加上 proxy_pass= http://127.0.0.1:56668/esbapi 代理的后端地址后,实际访问的地址为 http://127.0.0.1:56668/esbapi/manager/cm0004

fontEndUrl=http://localhost:4800/api/esbapi/manager/cm0004

split_address=fontEndUrl - /api/esbapi
             = /manager/cm0004

backEndUrl= http://127.0.0.1:56668/esbapi

realPath= backEndUrl + split_address
        = http://127.0.0.1:56668/esbapi/manager/cm0004

配置文件实例

server{
		listen 4800; # 配置端口
		server_name localhost; # 配置域名
		charset utf-8; # 编码
		access_log C:/Users/Administrator/Desktop/nginx-1.12.2/logs/access.log main; # 成功日志
		error_log C:/Users/Administrator/Desktop/nginx-1.12.2/logs/error.log error; # 错误日志
		index index.html; # 查找文件顺序
		# 其余location
		#静态资源访问
		location / {
            #------------------前台资源跟路径------------------
            root   C:/Users/Administrator/Desktop/dist;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

		location /api {
			proxy_pass http://127.0.0.1:56668;
		}
		
		#/api/esbapi 对前端请求的匹配和地址截取
		#http://localhost:4800/api/esbapi/manager/cm0004 截取后保留 /manager/cm0004
		#然后定位到后端地址 /esbapi/manager/cm0004 其中esbapi是后端的 context-path
		location /api/esbapi {
			proxy_pass http://127.0.0.1:56668/esbapi;
		}
		
		location /api/mqcore {
			proxy_pass http://127.0.0.1:56666/mqcore;
		}
		
		location /api/mqtask {
			proxy_pass http://127.0.0.1:56669/mqtask;
		}
	}
posted @ 2021-07-02 11:44  iullor  阅读(3066)  评论(0编辑  收藏  举报