1

I have one PHP app running on localhost port 80 and another running on port 81.

Pointing the browser to http://localhost runs the first app and all of its assets: js, css, imgs in the /assets folder. However, the 2nd app only can serve assets that have the exact file name as the 1st app. If the 2nd app's assets are in the 2nd app's /assets directory and don't have the same name as files in the first apps /assets folder, then those files aren't found an you get a 404 error.

Here's more specifics on what I'm seeing:

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

The likely cause of the problem in my nginx.conf:

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

/Applications/MNPP/conf/nginx/sites-enabled/app2:

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

/Applications/MNPP/conf/nginx/sites-enabled/app1:

server {
    listen       80;
    server_name  localhost;
    ...
famousgarkin
  • 330
  • 4
  • 12
tim peterson
  • 683
  • 2
  • 9
  • 18

2 Answers2

2

One problem is that you are specifying root directive inside a location block. There should be no reason to use that inside location.

Try moving the contents of location / block to server level, and remove location / completely.

Also, you shouldn't need location /assets either, because with this specification it resolves to exactly same path when root is specified on server level.

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
1

This will not match: server_name localhost:81;

Change it to: server_name localhost;

You can have the same "server name" on multiple ports. The listen block is enough for nginx to tell which server block it should process.

Nathan C
  • 14,901
  • 4
  • 42
  • 62