1. 程式人生 > 其它 >nginx location配置前後端地址

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;
		}
	}