System Information
-
Strapi Version: 3.6.1
-
Node Version: 14.16.1
-
NPM Version: 6.4.12
This is a follow-up to Strapi admin page will not load when using a non-default URL · Issue #10285 · strapi/strapi · GitHub.
I have a working Strapi app running at the default root URL (i.e. localhost:1337
, localhost:1337/admin
, mywebsite.com
, mywebsite.com/admin
).
However, I need Strapi to treat the path /cms
as its root path (i.e. localhost:1337/cms
, localhost:1337/cms/admin
, mywebsite.com/cms
, mywebsite.com/cms/admin
).
I tried changing the URL property in server.js
to “/cms”, but apparently doing so does not do what I thought it did. I’m told I should use an absolute path, but I don’t understand why because I only need to use a relative path. Also, I’m told I might need a proxy, but I don’t understand why I need a proxy.
How do I accomplish what I’m trying to do?
Yes you do need a proxy because Strapi doesn’t adjust the koa-router
for sub-paths, what the proxy does is handle path rewrites (and if you need SSL, since Strapi doesn’t handle it natively).
Likewise that URL key is used throughout the application to adjust variables for various core packages and plugins as to what the “Public URL” should be.
For example in this sample config: Nginx Proxying - Strapi Developer Documentation
...
location /api/ {
rewrite ^/api/?(.*)$ /$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;
}
This line is a rewrite rule: rewrite ^/api/?(.*)$ /$1 break;
Meaning all requests to yourdomain.com/api get sent to Strapi on /
So something like yourdomain.com/api/some-content-type actually end up on Strapi to localhost:1337/some-content-type meaning the koa-router
is able to properly regex match the request and fire the controller.
See the following Strapi Config from that same guide: Nginx Proxying - Strapi Developer Documentation
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
url: 'https://example.com/api',
});
Okay, so, if I understand correctly, Strapi must run from the root of the domain, and if I want it to appear as if it’s running from (for example) Easy Website Builder | Web Hosting Included | Ownwebsite.com, I’d have to set up a reverse proxy?
yes you need a proxy! i suggest for a backend with greater security to use a subdomain and a reverse proxy as cloudflare protection would be like cms.mywebsite.com and can use over https too