Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
包含特定设置中jQuery.ajax更改中使用的问号的URL(Docker、Nginx重写规则)_Jquery_Ajax_Docker_Nginx_Microservices - Fatal编程技术网

包含特定设置中jQuery.ajax更改中使用的问号的URL(Docker、Nginx重写规则)

包含特定设置中jQuery.ajax更改中使用的问号的URL(Docker、Nginx重写规则),jquery,ajax,docker,nginx,microservices,Jquery,Ajax,Docker,Nginx,Microservices,我有一个停靠的PHP/Laravel应用程序,它在PHP:7.4-apache容器中运行良好。我们已将其路由到http://localhost:81用于开发目的 现在我们希望这项服务在http://localhost/foo/bar在我们的微服务网络中。请求localhost转到nginx服务,我们在nginx_cors.conf中设置了一个重写规则来实现这一点: location /foo/bar { rewrite ^/foo/bar/?(.*) /$1 break; proxy_p

我有一个停靠的PHP/Laravel应用程序,它在PHP:7.4-apache容器中运行良好。我们已将其路由到
http://localhost:81
用于开发目的

现在我们希望这项服务在
http://localhost/foo/bar
在我们的微服务网络中。请求localhost转到nginx服务,我们在
nginx_cors.conf
中设置了一个重写规则来实现这一点:

location /foo/bar {
  rewrite ^/foo/bar/?(.*) /$1 break;
  proxy_pass http://laravel_app;
}
但是现在一些
jQuery.ajax
请求失败了。情况就是这样:

// in vendor code
// url is http://localhost/foo/bar/ajax/lib/?someParam=someValue
$.ajax({
  url: url
});

// in Chrome DevTools
// Note that foo/bar/ is gone
jquery.js:4 GET http://localhost/ajax/lib?someParam=someValue 404 (Not Found)
这个问题似乎会影响所有包含问号的URL。我们为一个不包含参数的请求剥离了它,请求通过了。但在上述情况下,这是不可能的

这里发生了什么?我不确定是否应该归咎于nginx重写规则,因为我根本不希望浏览器注意到它的影响(上面的错误消息来自chromedevtools)

编辑

查看Chrome中的网络选项卡,我意识到有两个类似的请求:

  • 一个转到
    http://localhost/foo/bar/ajax/lib/?someParam=someValue
    如预期。nginx以301和位置头响应
    http://localhost/ajax/lib?someParam=someValue
  • 第二个转到
    http://localhost/ajax/lib?someParam=someValue
    ,它正是第一个请求的位置标头。nginx再次响应,这次是404

因此,nginx似乎将第一个请求“转发”给自己,而不是Laravel应用程序。

似乎是Laravel应用程序而不是nginx本身发出的从
/ajax/lib/
/ajax/lib
的重定向。重写
位置
头的默认nginx行为是将后端主机名/协议替换为提供
的请求http://laravel_app/ajax/lib
重写为
http://localhost/ajax/lib
。该请求不在
位置/foo/bar{…}
下,将由其他一些nginx位置提供服务,从而为您提供http404notfound错误。您需要使用以下指令覆盖默认行为:

proxy_redirect http://laravel_app/ http://localhost/foo/bar/;

我完全不明白这个问题。
http://localhost/foo/bar/ajax/lib/?someParam=someValue
URL?如何将其传递到后端?我不明白这个,根据你的配置,它应该被重写为
http://localhost/ajax/lib/?someParam=someValue
,而不是作为
http://localhost/ajax/lib?someParam=someValue
(注意后面的斜杠).但是您的重写规则应该在将请求代理到laravel应用程序之前,从每个以该前缀开头的URI中删除
/foo/bar
前缀,不是吗?或者你期望从该规则中得到其他行为?在我看来,它不是nginx,而是laravel应用程序,它使
/ajax/lib/
重定向到
/ajax/lib
。您可以使用
curl-i”进行检查http://laravel_app/ajax/lib/?someParam=someValue“
命令并检查该应用程序将返回什么响应。如果我是对的,下一个请求
http://localhost/ajax/lib?someParam=someValue
在重定向后由浏览器生成,将不属于HTTP 404的
位置/foo/bar{…}
下。此外,您可以添加任何带有
curl
的头,包括授权头(参见SO线程),这太复杂了。让我们尝试更简单的解决方案,添加
proxy\u重定向//foo/bar指令到那个位置,让我们看看它是否有用。也许它应该是
proxy\u redirect//foo/bar/或甚至
代理\u重定向http://laravel_app/ /foo/bar/,你能试试这些吗?