Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Created July 31, 2015 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplay-dk/478bf412c9ee3f880038 to your computer and use it in GitHub Desktop.
Save mindplay-dk/478bf412c9ee3f880038 to your computer and use it in GitHub Desktop.
server {
listen 127.0.0.1:80;
server_name teatapp.test;
root /workspace/myapp/webroot;
index index.php;
location /assets/ {
if (-f $uri) {
break;
}
root /workspace/myapp/modules;
rewrite ^/assets/([^/]+)/(.*)$ /$1/assets/$2 break;
}
location / {
try_files $uri /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9100;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Directory structure:

workspace/
  myapp/
    webroot/
      index.php
      assets/
        baz.js
        hello/
          foo.js
    modules/
      hello/
        assets/
          foo.js
          bar.js

Expected results

/                     =>  /workspace/myapp/webroot/index.php
/assets/hello/foo.js  =>  /workspace/myapp/webroot/assets/hello/foo.js
/assets/hello/bar.js  =>  /workspace/myapp/modules/hello/assets/foo.js
/assets/baz.js        =>  /workspace/myapp/webroot/assets/baz.js

In summary:

  • foo.js is only present in the modules/hello/assets folder and gets delivered from there.
  • bar.js is present both in webroot/assets/hello and modules/hello/assets and gets delivered from webroot. (it hides/overrides the file in modules)
  • baz.js is only present in webroot/assets and gets delivered from there.

The part that doesn't work right now, is this:

    location /assets/ {
        if (-f $uri) {
            break;
        }
        root     /workspace/myapp/modules;
        rewrite  ^/assets/([^/]+)/(.*)$ /$1/assets/$2 break;
    }

Namely the if directive, doesn't seem to have any affect - the bar.js file gets delivered from modules rather than webroot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment