在两个不同的端口上提供资产

我有一个PHP应用程序在本地主机端口80上运行,另一个在端口81上运行。

指向浏览器http://localhost运行第一个应用程序及其所有资产:js,css,imgs在/assets文件夹中。 但是,第二个应用程序只能提供具有确切文件名的资产作为第一个应用程序。 如果第二个应用程序的资产位于第二个应用程序的/assets目录中,并且与第一个应用程序/assets文件夹中的文件名称不同,则会发现这些文件没有find404错误。

以下是关于我所看到的更多细节:

http://localhost

 <script src='/assets/foo.js'></script> //file contents alert('foo'); when foo.js is in app #1's /assets directory 

http://localhost:81

 <script src='/assets/foo.js'></script> alert('foo 2'); //alerts "foo 2" when foo.js is in app #2's /assets directory <script src='/assets/bar.js'></script> //file contents: alert('bar'); //file not found 404 error when bar.js is in app #2's /assets directory 

在我的nginx.conf可能的原因是:

 http { include /Applications/MNPP/conf/nginx/sites-enabled/app2; include /Applications/MNPP/conf/nginx/sites-enabled/app1; ... 

/ APP 2 /应用/ MNPP / conf目录/ nginx的/启用的站点 – :

 server { listen 81; server_name localhost:81; ... 

/ APP1 /应用/ MNPP / conf目录/ nginx的/启用的站点 – :

 server { listen 80; server_name localhost; ... 

一个问题是你正在指定一个location块内的root指令。 应该没有理由使用该内部location

尝试移动location /块的内容到server级别,并删除location /完全。

而且,您也不需要location /assets ,因为在本规范中,当在server级别指定root时,它将parsing为完全相同的path。

这将不匹配: server_name localhost:81;

将其更改为: server_name localhost;

您可以在多个端口上拥有相同的“服务器名称”。 监听块足以让nginx知道它应该处理哪个服务器块。