当前位置: 首页 > 科技观察

都是Nginx前缀导致的,FastDFS无法访问!

时间:2023-03-22 10:42:39 科技观察

作者个人研发在高并发场景下提供了一个简单、稳定、可扩展的延迟消息队列框架,具有精准的定时任务和延迟队列处理功能。开源半年多以来,已成功为十几家中小企业提供精准定时调度解决方案,经受住了生产环境的考验。为了造福更多的童鞋,这里提供开源框架地址:https://github.com/sunshinelyz/mykit-delay之前写到最近在服务器上搭建了一个6节点的FastDFS集群。统一的外部文件上传和访问服务。在实际开发过程中,前端童鞋需要通过访问其他服务器来转发对FastDFS服务的访问。这时候在前端访问FastDFS服务的时候多加了一个前缀,导致FastDFS服务返回400状态码。那么,我们如何解决呢?文章已收录于:https://gitee.com/binghe001/technology-binghe和https://github.com/sunshinelyz/technology-binghe朋友们别忘了给我一个Star哦!!问题重现。在服务器上搭建集群时,对外访问的统一接口为http://192.168.175.110。访问前端童鞋时,是通过http://192.168.175.101访问的,在前端添加了一个前缀文件。通过http://192.168.175.101/file访问。我首先想到的是通过Nginx直接将前端请求转发到192.168.175.110服务器。192.168.175.101服务器上的简化Nginx配置如下所示。upstreamfile{server192.168.175.110:80max_fails=3;}server{listen80;server_name192.168.175.101;location/{roothtml;index.htmlindex.htm;#允许cros跨域访问add_header'Access-Control-Allow-Origin''*';#proxy_redirectdefault;#连接代理服务器的超时时间,一定要注意这个超时时间不能超过75秒,当一个服务器挂了,10秒后会转发到另一个服务器。proxy_connect_timeout10;}location~/file{add_header'Access-Control-Allow-Origin''*';add_header'Access-Control-Allow-Credentials''true';proxy_passhttp://file;proxy_set_headerHost$host:$server_port;}我在192.168.175.110服务器上配置的Ngin如下。server{listen80;server_name192.168.175.110;#charsetkoi8-r;#access_loglogs/host.access.logmain;location/{roothtml;indexindex.htmlindex.htm;#允许cros跨域访问add_header'Access-Control-Allow-origin''*';#proxy_redirectdefault;#连接代理服务器的超时时间,一定要注意这个超时时间不能超过75秒,当一个服务器挂了,10秒后会转发到另一个服务器。proxy_connect_timeout10;}location~/group([0-9]){root/data/fastdfs/storage/data;ngx_fastdfs_module;}}此时出现问题:当请求通过转发到192.168.175.110服务器时文件前缀,将返回400状态码。其实问题的定位比较简单,就是在前端访问的时候多加了一个文件前缀。那么,我们如何解决这个问题呢?问题解决一般情况下,Nginx的反向代理只会替换域名或者IP部分,其他部分原样转发。也就是说,当前端访问http://192.168.175.101/file时,会被转发到http://192.168.175.110/file,从而无法正常访问文件服务接口。既然问题已经确定,我们下一步就是解决它。思路也比较简单,就是在192.168.175.101服务器收到请求的时候去掉文件前缀。如何删除它?其实很简单,只需要在192.168.175.101服务器上的Nginx中添加如下配置即可。location^~/file/{proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-NginX-Proxytrue;proxy_passhttp://file/;}此时192.168.175.101上服务器Nginx配置如下所示。upstreamfile{server192.168.175.110:80max_fails=3;}server{listen80;server_name192.168.175.101;location/{roothtml;index.htmlindex.htm;#允许cros跨域访问add_header'Access-Control-Allow-Origin''*';#proxy_redirectdefault;#连接代理服务器的超时时间,一定要注意这个超时时间不能超过75秒,当一个服务器挂了,10秒后会转发到另一个服务器。proxy_connect_timeout10;}location^~/file/{proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-NginX-Proxytrue;proxy_passhttp://file/;}}此时访问再次将http://192.168.175.101/file转发到http://192.168.175.110时,可以正确访问文件服务接口。知识扩展如何去除nginx反向代理配置中的前缀?当使用Nginx作为反向代理时,你可以简单地将请求转发到下一个服务而不做任何修改。设置proxy_pass请求只会替换域名。如果要根据不同的url后缀访问不同的服务,需要使用以下方法:方法一:添加“/”upstreampay{serverlocalhost:8089weight=5;}upstreamorder{serverlocalhost:8090weight=5;}server{listen80;server_namebinghe.com;location^~/pay/{proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-NginX-Proxytrue;proxy_passhttp://pay/;}location^~/order/{proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-NginX-Proxytrue;proxy_passhttp://order/;}}^~/pay/表示匹配如果前缀是pay的请求,如果proxy_pass末尾有/,那么/pay/*之后的路径会直接拼接在后面,也就是去掉pay。方法二:改写upstreampay{serverlocalhost:8089weight=5;}upstreamorder{serverlocalhost:8090weight=5;}server{listen80;server_namebinghe.com;location^~/pay/{proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-NginX-Proxytrue;rewrite^/user/(.*)$/$1break;proxy_passhttp://pay;}location^~/order/{proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-NginX-Proxytrue;rewrite^/order/(.*)$/$1break;proxy_passhttp://order;}}您可以通过以下二维码关注。转载本文请联系冰川科技公众号。