微服务项目nginx部署前后端
项目背景
前后端分离项目,前端采用elmadmin,后端为springcloud架构,多个微服务统一项目配置gateway网关,网关地址192.168.64.4:8000。
- 网关将各个微服务接口统一加上/api/services前缀;
- 前端配置代理,将网关接口统一加上/api前缀;
后端代理
- 去除前端url中的/api;
- 通过nginx访问网关地址;
# 1、登录接口配置,登录前端访问接口/api/center/login,后台真实接口/center/loginlocation /api/center/ {proxy_pass http://192.168.64.4:8000/center/;} # 2、geteway网关统一配置了/api/services/。前端访问地址(加了代理,url多一个api) /api/api/services/location /api/api/services/ {proxy_pass http://192.168.64.4:8000/api/services/;}
前端代理
- 前端dist压缩包解压后放到/usr/share/nginx/html/dist路径下
- 通过nginx访问静态文件
# 前端页面配置 ,前端dist解压包放到/usr/share/nginx/html路径下 location ^~ / {proxy_set_header HOST $host;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 加这个是支持history的路由模式try_files $uri $uri/ /index.html;root /usr/share/nginx/html/dist;index index.html;}
nginx.conf文件
全文件,可直接拷贝修改使用
user nginx;worker_processes 2;error_log /var/log/nginx/error.log wa;pid/var/run/nginx.pid;events {worker_connections 2048;}http {include/etc/nginx/mime.types;default_type application/octet-stream;client_max_body_size 1024M;client_body_buffer_size 6M;log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfileon;#tcp_nopush on;keepalive_timeout 65;#gzip on;server {listen 80; # 侦听80端口,如果强制所有的访问都必须是HTTPs的,这行需要注销掉 server_name localhost;client_max_body_size 30240m; proxy_intercept_errors on; fastcgi_intercept_errors on;# 后端微服务接口配置:# 1、登录接口配置,登录前端访问接口/api/center/login,后台真实接口/center/loginlocation /api/center/ {proxy_pass http://192.168.64.4:8000/center/;} # 2、geteway网关统一配置了/api/services/。前端访问地址(加了代理,url多一个api) /api/api/services/location /api/api/services/ {proxy_pass http://192.168.64.4:8000/api/services/;} # 前端页面配置 ,前端dist解压包放到/usr/share/nginx/html路径下 location ^~ / {proxy_set_header HOST $host;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 加这个是支持history的路由模式try_files $uri $uri/ /index.html;root /usr/share/nginx/html/dist;index index.html;}}}
作者:weixin_42459289
来源链接:https://blog.csdn.net/weixin_42459289/article/details/122884501
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。