各位讀者,大家好。
我們在很多項目中都是前后集成的方式,但是前端無法直接訪問后端,因此使用nginx中間件來實現跳轉和負載。
那么我們就需要會配置nginx。
如下,先找到前端部署的服務所在機器。登錄之后找到nginx配置文件:
比如,我的nginx配置文件在/mnt/nginx/conf/nginx.conf
第一步,修改配置文件:
1)配置(前端調)后端服務的跳轉
例如:假設該機器IP為x.x.x.x,外網可訪問,並且前端部署端口為80;
那么通過本地瀏覽器訪問http://x.x.x.x/goods/queryAll 其實就是訪問后端接口 http://10.111.21.15:8668/queryAll
#此處配置根據后端服務名找到服務器ip:port upstream goods-service { server 10.111.21.15:8668;
# 如果有多個節點,可以同理配置多個 } #前端所調用的后端服務接口前綴 location /goods/ { root registry/static_root; index index.html index.htm; proxy_pass http://goods-service/; }
2)配置前端靜態資源
例如:假設該機器IP為x.x.x.x,外網可訪問,並且前端部署端口為80;
那么通過本地瀏覽器訪問http://x.x.x.x/title/index.html 其實就是訪問部署在該機器中的前端靜態資源:./title/index.html
# 后端映射的服務及端口(不存在的端口),此處只是僅作掛靠,並無實際含義 upstream mobile-gateway-ytt { server 10.111.21.15:8686; # 如果有多個節點,可以同理配置多個 } # 前端靜態資源的訪問配置 location /bwmg/ { root html; index index.html index.htm; proxy_pass http://mobile-gateway-ytt/; } location /scan { root html; index index.html index.htm; proxy_pass http://mobile-gateway-ytt; } location /scan/saoma { root html; index index.html index.htm; proxy_pass http://mobile-gateway-ytt; } location /s { root html; index index.html index.htm; proxy_pass http://mobile-gateway-ytt; } location /t { root html; index index.html index.htm; proxy_pass http://mobile-gateway-ytt; } location /w { root html; index index.html index.htm; proxy_pass http://mobile-gateway-ytt; } location /invoice { root html; index index.html index.htm; proxy_pass http://mobile-gateway-ytt; } location /issue { root html; index index.html index.htm; proxy_pass http://mobile-gateway-ytt; } location /MP_verify { root html; index index.html index.htm; proxy_pass http://mobile-gateway-ytt; } location /title { # 連接到資源的路徑 alias /opt/nginx/static/static_root/title/; index index.html index.htm; }
第二步,重啟nginx,我的nginx是裝在docker容器中的
docker restart nginx
第三步,查詢nginx啟動情況。因為我的nginx是裝在docker容器中的
docker ps
最后,如果nginx.conf配置文件中只有upstream,而不存在location,那么肯定是把url的路徑映射放在另外一個文件了,通過include進入。例如
如果對您有幫助,您的鼓勵是對作者汗水最大的肯定~