URL parameter for Server not working (for Nginx proxy) #7310

This discussion has been migrated from our Github Discussion #7310


amaneshi140d ago

Describe the bug
If you set “URL” parameter in server configuration file admin home page become broken.

Steps to reproduce the behavior

  1. Add “url” parameter to server configuration file (absolute path fragment, like “/api”)
  2. Rebuild admin panel with “strapi build” command
  3. Open admin panel home page (like “http://localhost:1337/api/admin”)
  4. In browser network tab request “http://localhost:1337/api/admin/init” return bad response (status 200, but content same as for root page html), but if in current configuration we send request without URL prefix (“http://localhost:1337/admin/init”) - then we get correct answer, as bellow:
{
    "data":
        {
           "uuid":"4b3d0c8b-6387-4c45-a87a-83e4643fd82f",
           "currentEnvironment":"development",
           "autoReload":true,
           "strapiVersion":"3.1.3",
           "hasAdmin":true
        }
}

Expected behavior
Admin panel should work as usual.

System

  • Node.js version: 12.18.3 LTS
  • NPM version: 6.14.6
  • Strapi version: 3.0.5 and 3.1.3
  • Database: SQLITE (irrelevant)
  • Operating system: Windows 10 (irrelevant)

In version 3.0.5 this request belonged to “users-permissions” plugin but worked same as in latest version.
Also can’t predict which requests also not work after change as this request blocks other and loading hangs.

Responses to the discussion on Github


derrickmehaffy140d ago

Collaborator

@amaneshi the url key in the server.js doesn’t change the prefix path on the backend, only within the admin. So when it’s set since react is CSR it tries to connect to the backend via the url provided (which would be proxied by Nginx). With that key set, you won’t be able to access the backend on localhost anymore and will need to use a proxy.

Can you provide your Nginx config, or does your config match the example in the sub-folder-unified nginx config docs?


derrickmehaffy140d ago

Collaborator

Something to note from the example I posted above:
rewrite ^/api/(.*)$ /$1 break; <— This line cuts out the /api prefix before proxying the request to the Strapi backend.


amaneshi140d ago

Author

Thank you for quick response!
Yes, now my NGINX configuration set as in docs article that you provided. And I aware of Nginx rewrite instructions (tried it already).
But take a closer look at my problem/bug - I need use URL parameter to serve frontend on root location (I mean “domain/”) and pass all requests that have prefix “/api” to proxy for strapi.
If I use url - I dont need rewrite at all (I assume this, maybe I wrong), and all requests (that I saw in network tab) to serve static admin panel files starts with “/api” prefix and if I use rewrite rule that cut this prefix - then this static files cant be found by server (as webpack serve files with prefix).
From other side - if I not use URL parameter - I can use rewrite but as all requests serve from root path (internally by strapi) I can’t write proxy config to separate frontend and backend location sections.

About current BUG: as you can see on screenshot - when I start server on localhost static files load as expected but last request (as I described in first message) don’t include “/api” prefix and return wrong answer (moreover, there should be 404 error!)

Screenshot

If I understand something wrong - advice me please how correctly locate frontend (angular) and backend (strapi) on same server with Nginx?


derrickmehaffy139d ago

Collaborator

@amaneshi can you clarify where your frontend is?

In the best case scenario within the same server/Nginx instance:

  • Nginx will proxy all requests to say example.com/api to Strapi (via that proxy block)
  • Angular (depending if it’s SSR or CSR) would either be served as static assets (not from within Strapi but directly from nginx) or you would have another proxy block to connect to a live running instance of Angular SSR.

You shouldn’t serve both Strapi and Angular within the same node process (such as building the Angular application and putting the dist folder in the public folder).

I’m going to move this over to a discussion as it’s not really a bug with Strapi but a question about it’s usecase.


amaneshi139d ago

Author

To be clear - yes, I start strapi proccess on default port with reverse proxy in Nginx (in root location now, like example.com/ after proxy). My config (main pieces):

upstream strapi {
		server ubuntu_strapi_1:1337; // My docker service
	}

# HTTPS Server
    server {
        listen 443 http2 ssl;
        server_name mydomain.com;
		
	location /api/ {
			proxy_pass http://strapi/;
			// Other proxy headers...
		}
    }

And added “url” parameter to server config file

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  url: 'https://mydomain.com/api',
});

But you say this is not a bug, I partly disagree with you. Previously I user relative url and have this problem (as documentation say it can be either), but with absolute route with domain this configuration works.
So you can close this issue but or remove phrase about possible relative path or fix it.
Thank you for attention!


derrickmehaffy139d ago

Collaborator

@petersg83 maybe I missed something related to using a relative URL here?


amaneshi139d ago

Author

When I used url like “/api” or “api” - some requests not worked as they should


petersg83139d ago

Maintainer

Hi :slight_smile:
Maybe you want to look at the parameter admin.url:

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  url: 'https://mydomain.com/api',
  admin: {
    url: "https://mydomain.com/api/admin",
  }
});