Strapi in subfolder issue

System Information
  • Strapi Version: 4.0.0
  • Operating System: mac
  • Database: mysql
  • Node Version: v14.1.0
  • NPM Version: 6.14.4
  • Yarn Version: 1.22.10

Hi everyone, i’m trying to configure strapi to be served at /cms path.
As described in documentation, i modified server.js setting url: “/cms”

I rebuild the admin portal and when i run yarn develop i see in the logs:

To manage your project 🚀, go to the administration panel at:
http://localhost:1337/cms/admin

To access the server ⚡️, go to:
http://localhost:1337/cms

when i open the link in the browser, an html page is returned. This page invokes http://localhost:1337/cms/admin/init returns html code (same as index) instead of json and the page breaks.

Also none of the api seems working, i always get a 404

Am i missing something?

Thanks a lot

You need a proxy layer to do something like this, see the “Subfolder unified” tabs:

On it’s own Strapi can’t do what you are trying to do (you could change the API prefix but that won’t modify the route to the admin, file uploads, or various other endpoints).

I tried with nginx and I’ve been able proxy the apis.

The problem is the admin panel. Both admin page (/cms/admin) and apis (ex. /csm/admin/init) start with /cms/admin so the nginx rule present in the documentation applies to both, breaking the FE.

I had to change the url setting in admin.js to something different. Then in my nginx configuration i created another rule to handle the new admin url disabling the rewrite

location /cms/new-admin {
        #rewrite ^/new-admin/?(.*)$ /$1 break;
        proxy_pass http://strapi;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass_request_headers on;
    }

Hello, I have wasted a lot of time trying to do this with strapi 4.1.7

Thank you for sharing the solution :slight_smile:

To recap if someone else needs to host strapi in subfolder path :

  1. You need to add the path in ./config/server.js :
    url: env('STRAPI_URL', ''),
  2. Add a specific path for strapi admin panel in ./config/admin.js :
    url: env('STRAPI_ADMIN_URL', '/admin'),
  3. Add two nginx location, one for each url with only the root location requiring a rewrite
  4. don’t forget to rebuild strapi…

I think the same issue exist with other reverse proxy such caddy or haproxy since it is a strapi configuration issue.
Maybe the documentation at Nginx Proxying - Strapi Developer Docs should be updated ?

The main trick is that url in ./config/server.js should be set to a needed value at build time, it’s not picked up at runtime.

For example, if you want to make it configurable from environment like url: env('BASE_URL'), then your BASE_URL should be available when you’re running npm build.
In case of building a Docker image one can set BASE_URL as follows:

ENV BASE_URL https://example.com/blog

i has the same error in 2024/2