Nginx的 – 重写和pass_proxy到另一个端口原因404找不到

试图让URL重写与端口转发工作,但似乎不起作用。

我的应用程序服务器运行在:8080端口,而nginx工作在默认的80 。 所以我不仅要重写url,还要把请求转发到另一个端口。

其实,我唯一想做的是通过它的快捷方式http://localhost/tom来查询现有资源http://localhost:8080/#/cat/tom ,而不需要任何redirect。

这是我基于nginx文档和其他问题所做的:

 server { listen 80; server_name localhost; root /; #charset koi8-r; #access_log logs/host.access.log main; location / { rewrite ^/([az]+)$ /#/cat/$1 break; proxy_pass http://localhost:8080; proxy_redirect off; } ... } 

已尝试其他configuration,但都没有工作。 当我访问http://localhost/tom它表示404 not foundhttp://localhost:8080/#/cat/tom按预期工作。

日志没有提到任何错误。

如何按预期强制这项工作?

问题是#/cat/tom是对客户端应用程序的指令,而不是服务器端应用程序。 这意味着它需要被发送到客户端(通过redirect)。

例如:

 location = /tom { return 302 /#/cat/tom; } location / { proxy_pass http://localhost:8080; ... } 

你提到你的问题,你正在尝试做这个没有redirect,但不幸的是,如果你看看一个URL的结构,你会注意到#之后的任何东西都不会被发送到服务器。 请参阅此文档了解更多

nginx只能重写URL的path部分。 它不工作的查询部分,它是由? 它也不是用#分隔的部分工作,这是片段。

实际上,当你的浏览器input你的http://localhost:8080/#/cat/tom URL时,你的浏览器发送一个请求来获取http://localhost:8080/ ,你的浏览器自己处理#后的部分。

因此,您需要了解您的应用程序软件如何与这些碎片部件一起工作,并基于此进行configuration。