使用Nginx调用网关,然后网关调用其他微服务

问题前提:目前我的项目是已经搭建了网关根据访问路径路由到微服务,然后现在我使用了Nginx将静态资源都放在了Nginx中,然后我后端定义了一个接口访问一个html页面,但是html页面要用到静态资源,这个静态资源在我的后端是没有的,静态资源都在Nginx中,那么我要怎么办呢,其中一个好办法就是使用Nginx访问我们后台网关,然后后台网关直接访问我们的微服务,因为都在一个域名下面因此直接静态资源就能访问到(这里后面会详细解释)。

前置条件的配置:

Nginx 配置

可以看到在域名www.51xuecheng.cn localhost;下面访问我们的静态资源路径就会通过alias映射到我们服务器上指定目录下面的文件。也就相当于我们的静态资源部署到Nginx中(我记着之前学过可以将静态资源直接放到Nginx中的什么目录下,就不用使用alias了)

        

#访问minio文件系统的网址
    upstream fileserver{
    server 192.168.101.65:9000 weight=10;
    } 
    server {
        listen       80;
        server_name  www.51xuecheng.cn localhost;
        #rewrite ^(.*) https://$server_name$1 permanent;
        #charset koi8-r;
        ssi on;
        ssi_silent_errors on;
        #access_log  logs/host.access.log  main;

        location / {
            alias   E:/code/xc-ui-pc-static-portal/;
            index  index.html index.htm;
        }
        #静态资源
        location /static/img/ {  
                alias  E:/code/xc-ui-pc-static-portal/img/;
        } 
        location /static/css/ {  
                alias   E:/code/xc-ui-pc-static-portal/css/;
        } 
        location /static/js/ {  
                alias   E:/code/xc-ui-pc-static-portal/js/;
        } 
        location /static/plugins/ {  
                alias   E:/code/xc-ui-pc-static-portal/plugins/;
                add_header Access-Control-Allow-Origin http://ucenter.51xuecheng.cn;  
                add_header Access-Control-Allow-Credentials true;  
                add_header Access-Control-Allow-Methods GET;
        } 
        location /plugins/ {  
                alias   E:/code/xc-ui-pc-static-portal/plugins/;
        } 
        location /course/preview/learning.html {
                alias E:/code/xc-ui-pc-static-portal/course/learning.html;
        } 
        location /course/search.html {  
                root   E:/code/xc-ui-pc-static-portal;
        } 
        location /course/learning.html {  
                root   E:/code/xc-ui-pc-static-portal;
        } 

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    server {
        listen       80;
        server_name  file.51xuecheng.cn;
        #charset koi8-r;
        ssi on;
        ssi_silent_errors on;
        #access_log  logs/host.access.log  main;
        location /video {
            proxy_pass   http://fileserver;
        }

        location /mediafiles {
            proxy_pass   http://fileserver;
        }
   }

 网关配置

spring:cloud:gateway:
#      filter:
#        strip-prefix:
#          enabled: trueroutes: # 网关路由配置- id: content-api # 路由id,自定义,只要唯一即可# uri: http://127.0.0.1:8081 # 路由的目标地址 http就是固定地址uri: lb://content-api # 路由的目标地址 lb就是负载均衡,后面跟服务名称predicates: # 路由断言,也就是判断请求是否符合路由规则的条件- Path=/content/** # 这个是按照路径匹配,只要以/content/开头就符合要求
#          filters:
#            - StripPrefix=1- id: system-api# uri: http://127.0.0.1:8081uri: lb://system-apipredicates:- Path=/system/**
#          filters:
#            - StripPrefix=1- id: media-api# uri: http://127.0.0.1:8081uri: lb://media-apipredicates:- Path=/media/**
#          filters:
#            - StripPrefix=1- id: search-service# uri: http://127.0.0.1:8081uri: lb://searchpredicates:- Path=/search/**
#          filters:
#            - StripPrefix=1- id: auth-service# uri: http://127.0.0.1:8081uri: lb://auth-servicepredicates:- Path=/auth/**
#          filters:
#            - StripPrefix=1- id: checkcode# uri: http://127.0.0.1:8081uri: lb://checkcodepredicates:- Path=/checkcode/**
#          filters:
#            - StripPrefix=1- id: learning-api# uri: http://127.0.0.1:8081uri: lb://learning-apipredicates:- Path=/learning/**
#          filters:
#            - StripPrefix=1- id: orders-api# uri: http://127.0.0.1:8081uri: lb://orders-apipredicates:- Path=/orders/**
#          filters:
#            - StripPrefix=1

后端接口代码

/**
 * @description 课程预览,发布
 * @author Mr.M
 * @date 2022/9/16 14:48
 * @version 1.0
 */
 @Controller
public class CoursePublishController {


 @GetMapping("/coursepreview/{courseId}")
 public ModelAndView preview(@PathVariable("courseId") Long courseId){

      ModelAndView modelAndView = new ModelAndView();
      modelAndView.addObject("model",null);
      modelAndView.setViewName("course_template");
   return modelAndView;
  }

}

 

我们的test.ftl应该是course_template ,但是我们接口虽然访问了这个ftl文件但是文件中的资源引用确不能访问到因为我们resource下面没有其他静态文件了。

 可以看到我们course_template文件中的静态文件路径为/static/plugin等 如果我们使用网址http://localhost:63040/content/coursepreview/74 那么就会从我们的域名 http://localhost::63040下面的classpath也就是我们的resource路径下面找,但是这个路径下面并没有这些个文件因此找不到。

问题解决

使用Nginx访问我们的网关然后再通过网关路由到我们的微服务,然后我们静态页面中的资源就会到我们的Nginx相应的域名下面去找。代码如下

 #后台网关
  upstream gatewayserver{
    server 127.0.0.1:63010 weight=10;
  }
  server {
        listen       80;
        server_name  www.51xuecheng.cn localhost;
       

#rewrite ^(.*) https://$server_name$1 permanent;
        #charset koi8-r;
        ssi on;
        ssi_silent_errors on;
        #access_log  logs/host.access.log  main;

        location / {
            alias   E:/code/xc-ui-pc-static-portal/;
            index  index.html index.htm;
        }
        #静态资源
        location /static/img/ {  
                alias  E:/code/xc-ui-pc-static-portal/img/;
        } 
        location /static/css/ {  
                alias   E:/code/xc-ui-pc-static-portal/css/;
        } 
        location /static/js/ {  
                alias   E:/code/xc-ui-pc-static-portal/js/;
        } 
        location /static/plugins/ {  
                alias   E:/code/xc-ui-pc-static-portal/plugins/;
                add_header Access-Control-Allow-Origin http://ucenter.51xuecheng.cn;  
                add_header Access-Control-Allow-Credentials true;  
                add_header Access-Control-Allow-Methods GET;
        } 
        location /plugins/ {  
                alias   E:/code/xc-ui-pc-static-portal/plugins/;
        } 
        location /course/preview/learning.html {
                alias E:/code/xc-ui-pc-static-portal/course/learning.html;
        } 
        location /course/search.html {  
                root   E:/code/xc-ui-pc-static-portal;
        } 
        location /course/learning.html {  
                root   E:/code/xc-ui-pc-static-portal;
        } 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       }
    }

        #api
        location /api/ {
                proxy_pass http://gatewayserver/;
        }

 下面是我在研究这个问题时候的一些疑问,gpt的解答,可能有一些不太准确,但结果是通的,

 

 

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/45337.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Http 状态码汇总

文章目录 Http 状态码汇总1xx(信息性状态码)2xx(成功状态码)3xx(重定向状态码)4xx(客户端错误状态码)5xx(服务器错误状态码) Http 状态码汇总 1xx&#xff08…

PyTorch模型性能分析与优化

动动发财的小手,点个赞吧! 训练深度学习模型,尤其是大型模型,可能是一项昂贵的支出。我们可以使用的管理这些成本的主要方法之一是性能优化。性能优化是一个迭代过程,我们不断寻找提高应用程序性能的机会,然…

Springboot 实践(10)spring cloud 与consul配置运用之服务的注册与发现

前文讲解,完成了springboot、spring security、Oauth2.0的继承,实现了对系统资源的安全授权、允许获得授权的用户访问,也就是实现了单一系统的全部技术开发内容。 Springboot是微服务框架,单一系统只能完成指定系统的功能&#xf…

NSSCTF之Misc篇刷题记录(14)

[SWPUCTF] 2021新生赛之Crypto篇刷题记录① [UUCTF 2022 新生赛]王八快跑[安洵杯 2020]BeCare4[HDCTF 2023]ExtremeMisc[SUCTF 2018 招新赛]follow me[SUCTF 2018 招新赛]佛家妙语 NSSCTF平台:https://www.nssctf.cn/ PS:记得所有的flag都改为NSSCTF […

【Linux取经路】探索进程状态之僵尸进程 | 孤儿进程

文章目录 一、进程状态概述1.1 运行状态详解1.2 阻塞状态详解1.3 挂起状态详解 二、具体的Linux操作系统中的进程状态2.1 Linux内核源代码2.2 查看进程状态2.3 D磁盘休眠状态(Disk sleep)2.4 T停止状态(stopped) 三、僵尸进程3.1 僵尸进程危害总结 四、孤儿进程五、结语 一、进…

C++初阶——string(字符数组),跟C语言中的繁琐设计say goodbye

前言:在日常的程序设计中,我们会经常使用到字符串。比如一个人的身份证号,家庭住址等,只能用字符串表示。在C语言中,我们经常使用字符数组来存储字符串,但是某些场景(比如插入,删除)下操作起来很…

git版本管理加合并笔记

1.创建空文件夹,右键Bash here打开 2.打开链接,点击克隆下载,复制SSH链接 3.输入git SSH链接 回车 遇到问题: 但明明我已经有权限了, 还是蹦出个这 4.换成https在桌面上进行克隆仓库就正常了 5.去vscode里改东西 …

删除远程桌面的下拉框ip地址

原因: 如下图,有时候想清理掉无法连接的IP。 方法: 一、进入 注册表编辑器 进入方法:一下两个方法都可以使用。 1. 在win10里面直接搜索 注册表编辑器,然后打开 2. 打开 运行(Win R)&#xff…

文件同步工具rsync

文章目录 作用特性安装命令服务端启动增加安全认证及免密登录 实时推送源服务器配置结合inotify实现实时推送 参数详解 学些过程中遇到的问题 作用 rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或…

在SpringBoot中添加拦截器忽略请求URL当中的指定字符串

1 自定义拦截器 Component public class GlobalInterceptor implements HandlerInterceptor {Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String path request.getRequestURI();if (pa…

蛊卦-拨乱反正

目录 前言 卦辞 爻辞 总结 前言 题外话,今天占卜时,看错了,以为占到了蛊卦(后续会对自己的占卦经历进行补充,不断完善这个易经学习的专栏),那顺便就学习一下蛊卦,蛊惑人心&#…

OkHttp 源码浅析一

演进之路:原生Android框架不好用 ---- HttpUrlConnect 和 Apache HTTPClient 第一版 底层使用HTTPURLConnect 第二版 Square构建 从Android4.4开始 基本使用: val okhttp OkHttpClient()val request Request.Builder().url("http://www.baidu.com").buil…

axios使用axiosSource.cancel取消请求后怎么恢复请求,axios取消请求和恢复请求实现

在前端做大文件分片上传,或者其它中断请求时,需要暂停或重新请求,比如这里大文件上传时,可能会需要暂停、继续上传,如下GIF演示: 这里不详细说文件上传的处理和切片细节,后续有时间在出一篇&a…

ubuntu22.04 找不到串口,串口ttyusb时断时续的问题(拔插以后能检测到,过会儿就检测不到了)

1. 问题描述 ubuntu22.04的PC,在连接USB串口的时候,有时能找到ttyUSB0,有时找不到,如下: base) airsairs-Precision-3630-Tower:~$ ls -l /dev/ttyUSB* crwxrwxrwx 1 root dialout 188, 0 Aug 17 16:36 /dev/ttyUSB0 (base) air…

【JS基础】一些个人积累的原生JS编码设计思想,和大家一起开拓下思维

文章目录 前言对象配置链式调用队列调用并发执行未完待续 前言 以下都是我个人遇到的前端JS原生编码设计上的一些案例记录,希望能帮助新手开拓写代码的思想,并且能够结合自己的想法应用在实际的项目中,写出更加易读,拓展&#xf…

2023国赛数学建模B题思路模型代码 高教社杯

本次比赛我们将会全程更新思路模型及代码,大家查看文末名片获取 之前国赛相关的资料和助攻可以查看 2022数学建模国赛C题思路分析_2022国赛c题matlab_UST数模社_的博客-CSDN博客 2022国赛数学建模A题B题C题D题资料思路汇总 高教社杯_2022国赛c题matlab_UST数模社…

解决“topk_cpu“ not implemented for ‘Half‘

一、问题描述 如题报错:“topk_cpu” not implemented for ‘Half’ 是在使用transformers库时本地导入某个模型,完整报错如下: File "/Users/guomiansheng/anaconda3/envs/ep1/lib/python3.8/site-packages/torch/utils/_contextlib.p…

安卓监听端口接收消息

文章目录 其他文章监听端口接收消息 建立新线程完整代码 其他文章 下面是我的另一篇文章,是在电脑上发送数据,配合本篇文章,可以实现电脑与手机的局域网通讯。直接复制粘贴就能行,非常滴好用。 点击连接 另外,如果你不…

AI 绘画Stable Diffusion 研究(十二)SD数字人制作工具SadTlaker插件安装教程

免责声明: 本案例所用安装包免费提供,无任何盈利目的。 大家好,我是风雨无阻。 想必大家经常看到,无论是在产品营销还是品牌推广时,很多人经常以数字人的方式来为自己创造财富。而市面上的数字人收费都比较昂贵,少则几…

使用yolov5进行安全帽检测填坑指南

参考项目 c​​​​​​​​​​​​​​GitHub - PeterH0323/Smart_Construction: Base on YOLOv5 Head Person Helmet Detection on Construction Sites,基于目标检测工地安全帽和禁入危险区域识别系统,🚀😆附 YOLOv5 训练自己的…