美文网首页
nginx实现动态代理

nginx实现动态代理

作者: 雨中星辰0 | 来源:发表于2023-10-15 14:31 被阅读0次
作者 时间
雨中星辰 2023-10-16

需求说明

需要用nginx代理一个应用的请求,但是该没有一个统一的url前缀。
就需要把:http://nginx ip/service-gateway/的请求转发到http://10.35.13.191:9095/
例如:
http://nginx ip/service-gateway/index.html转发后为http://10.35.13.191:9095/index.html
http://nginx ip/service-gateway/api/list转发后为http://10.35.13.191:9095/api/list

nginx配置

    location /service-gateway {
            proxy_pass http://10.35.13.191:9095/;
    }

上面的nginx配置中的location /service-gateway指定了一个URL路径前缀为/service-gateway的位置块。当nginx收到请求时,如果请求的URL以/service-gateway开头,就会将该请求转发到http://10.35.13.191:9095/

具体解释如下:

  • location /service-gateway: 这里指定了要匹配的URL路径前缀为/service-gateway。也就是说,当请求的URL以/service-gateway开始时,将会应用下面的配置。

  • proxy_pass http://10.35.13.191:9095/: 这里使用了proxy_pass指令来将请求转发到指定的后端服务器。http://10.35.13.191:9095/是代理目标的地址。/符号表示将请求的URL保持不变,并且在转发时不附加路径前缀。

    • 例如,当 nginx 收到请求 /service-gateway/hello 时,它会将请求转发到 http://10.35.13.191:9095/hello。原始的 /service-gateway 路径前缀将被忽略,只会将 /hello 部分转发到后端服务器。

这样配置后,当请求http://nginx ip/service-gateway/hello时,nginx会将该请求转发到http://10.35.13.191:9095/hello,并且在转发时不再附加/service-gateway前缀。

相关文章

网友评论

      本文标题:nginx实现动态代理

      本文链接:https://www.haomeiwen.com/subject/wjptidtx.html