Repository: ckan/ckan

Issue:
Fanstatic not honouring root_path
### CKAN Version if known (or site URL)

master( 5f4949c924f3a95c59ab4faf0026629804b83f7a )
### Please describe the expected behaviour

Setting root_path to "/testing/{{LANG}} should make all links and resources fetched from www.site_url/testing/
### Please describe the actual behaviour

Most of the stuff is linked to /testing but js, css and the like is fetched from /fanstatic/...
### What steps can be taken to reproduce the issue?

set `ckan.root_path = /testing/{{LANG}}/`

Might be needed to look at my web setup as well. Using chef to setup apache2 and nginx as reverseproxy

**nginx site:**

```
proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=cache:30m max_size=250m;
proxy_temp_path /tmp/nginx_proxy 1 2;

server {
    client_max_body_size 100M;
    location /testing/ {
        proxy_pass http://127.0.0.1:8080/;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_cache cache;
        proxy_cache_bypass $cookie_auth_tkt;
        proxy_no_cache $cookie_auth_tkt;
        proxy_cache_valid 30m;
        proxy_cache_key $host$scheme$proxy_host$request_uri;
        # In emergency comment out line to force caching
        # proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
    }

}
```

**apache2 site:**

```
<VirtualHost 127.0.0.1:8080>
    ServerName <%= @server_name %>
    ServerAlias <%= @server_alias %>
    WSGIScriptAlias / <%= @config_dir %>/apache.wsgi

    # Pass authorization info on (needed for rest api).
    WSGIPassAuthorization On

    # Deploy as a daemon (avoids conflicts between CKAN instances).
    WSGIDaemonProcess ckan_<%= @project_name %> display-name=ckan_<%= @project_name %> processes=2 threads=15

    WSGIProcessGroup ckan_<%= @project_name %>

    ErrorLog /var/log/apache2/ckan_<%= @project_name %>.error.log
    CustomLog /var/log/apache2/ckan_<%= @project_name %>.custom.log combined

    <IfModule mod_rpaf.c>
        RPAFenable On
        RPAFsethostname On
        RPAFproxy_ips 127.0.0.1
    </IfModule>

    <Directory />
        Require all granted
    </Directory>
</VirtualHost>
```



Discussion:
I can reproduce this in master and 2.6.0 using both paster serve and apache 2.4 w/mod_wsgi (without nginx) on Fedora 23 . The other config difference between my apache setup and @erlingbo's is I have `WSGIScriptAlias /data /etck/ckan/default/apache.wsgi`

The problem for me is that the RootPathMiddleware has been moved back to the bottom of the WSGI stack in d9f3a5036a99c5e447ad93977e99ba745e2868c3 and it is messing up the environment for the Fanstatic middleware, i.e., Fanstatic is passed an empty `SCRIPT_NAME` and it doesn't know about `ckan.root_path`. Moving the RootPathMiddleware higher up the stack, as in #3090, resolves the issue for me.

I am running ckan-2.5.3 and when I upgrade to master or to 2.6.0 my theme breaks in development. I am running paster serve with the following setting  for root url in the config 
[composite:nonroot]
use = egg:Paste#urlmap
/ckan = main
 Basically everything being served by fanstatic is still being served from /fanstatic and not /ckan/fanstatic whis is obviously causing issues, above is very similar issue and I have tried adding the ckan.root_path but that does not seem to be working either. any advice would be appreciated as we are looking to upgrade to 2.6.0 in the very near future and need to rectify the themes first.

The exact same settings as I describe above work fine with 2.5.3
@merkelct does the patch in #3090 fix your issue. By the way, that PR says merged but it was overwritten and so (I think) it doesn't exist in 2.6.0 or master
@MrkGrgsn I believe that does solve my issue is their a plan to get it into master and 2.6.0?
probably a bad merge of some of the flask work :-( looks like this code has moved to https://github.com/ckan/ckan/blob/master/ckan/config/middleware/pylons_app.py#L176 would you mind submitting a PR to restore the fix?
no issue creating a PR but it says nothing to compare when trying to add the PR for #3090 into master or anything else for that matter it thinks it has already been merged ? might be as simple as a re-order in the pylons_app.py

Resolving patch:
diff --git a/ckan/config/middleware/pylons_app.py b/ckan/config/middleware/pylons_app.py
--- a/ckan/config/middleware/pylons_app.py
+++ b/ckan/config/middleware/pylons_app.py
@@ -55,6 +55,8 @@ def make_pylons_stack(conf, full_stack=True, static_files=True,
     for plugin in PluginImplementations(IMiddleware):
         app = plugin.make_middleware(app, config)
 
+    app = common_middleware.RootPathMiddleware(app, config)
+
     # Routing/Session/Cache Middleware
     app = RoutesMiddleware(app, config['routes.map'])
     # we want to be able to retrieve the routes middleware to be able to update
@@ -173,8 +175,6 @@ def make_pylons_stack(conf, full_stack=True, static_files=True,
     if asbool(config.get('ckan.tracking_enabled', 'false')):
         app = common_middleware.TrackingMiddleware(app, config)
 
-    app = common_middleware.RootPathMiddleware(app, config)
-
     # Add a reference to the actual Pylons app so it's easier to access
     app._wsgi_app = pylons_app
 


Test patch: