先部署启动后端程序 java -jar xxx.jar
前端打包给你的dist文件夹,把他放到一个位置,这里是放在 D:\soft\dist
然后配置nginx配置文件 如下:
server { listen 80; server_name localhost; location / { root D:\soft\dist; try_files $uri $uri/ /index.html; index index.html index.htm; } location /api/ { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET, POST'; proxy_pass http://172.17.1.2:9090/; proxy_connect_timeout 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /index/ { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'OPTIONS, GET, POST'; proxy_pass https://iot.krzhibo.com/index/; proxy_connect_timeout 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
值得注意的是这里一个server中配置了3个location
第一个location / 表示浏览器中输入localhost的时候 默认访问D:\soft\dist文件夹下的index.html
第二个location /api/ 表示访问路径中带api的,反向代理到http://172.17.1.2:9090/
所以前端在调用接口的地址前面都需要加上api
第三个location /index/ 表示访问路径中带index的,反向代理到https://iot.krzhibo.com/index/ 外部接口,
例如: