skip to Main Content

I am trying to get a Strapi.io installation set up so that it works on an existing Apache site running on Port 80.

Details:

  • The Strapi server is running on Port 1337.
  • The Strapi server has both a front page at / and an admin panel /admin
  • My Apache site is running at http://example.com on port 80

I have edited my apache configuration file and added the following lines:

ProxyPass /admin http://localhost:1337/admin
ProxyPassReverse /admin http://localhost:1337/admin

ProxyPass /api http://localhost:1337/
ProxyPassReverse /api http://localhost:1337/

Currently that works fine for the /api page, but not the admin page. On the Admin page I get the following console errors:

main.js:40 Uncaught (in promise) SyntaxError: Unexpected token < in
JSON at position 0
at r (main.js:40) r @ main.js:40 Promise.then (async) u @ main.js:1 ./node_modules/strapi-helper-plugin/lib/src/app.js @
main.js:40 t @ main.js:1 (anonymous) @ main.js:1 (anonymous) @
main.js:1

main.js:45 Uncaught (in promise) SyntaxError: Unexpected
token < in JSON at position 0
at t (main.js:45) t @ main.js:45 Promise.then (async) u @ main.js:1 ./node_modules/strapi-helper-plugin/lib/src/app.js @
main.js:45 a @ main.js:1 (anonymous) @ main.js:1 (anonymous) @

main.js:1 main.js:40 Uncaught (in promise) SyntaxError: Unexpected
token < in JSON at position 0
at n (main.js:40)

If I refresh the page, some or all of the main.js show as admin.js. Not sure if it makes a difference:

main.js:40 Uncaught (in promise) SyntaxError: Unexpected token < in
JSON at position 0
at r (main.js:40) r @ main.js:40 Promise.then (async) u @ main.js:1 ./node_modules/strapi-helper-plugin/lib/src/app.js @
main.js:40 t @ main.js:1 (anonymous) @ main.js:1 (anonymous) @
main.js:1

admin:1 Uncaught (in promise) SyntaxError: Unexpected token
< in JSON at position 0 Promise.then (async) u @ main.js:1
./node_modules/strapi-helper-plugin/lib/src/app.js @ main.js:40 t @
main.js:1 (anonymous) @ main.js:1 (anonymous) @ main.js:1

admin:1
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at
position 0

Why am I experiencing this problem. What do I have to do to get this working correctly with my Apache server?

To clarify:

This was a very simple two minute install. I have not started to add any content to the site yet. I’m just trying to get the admin panel to work through Apache.

2

Answers


  1. Apache serves by default index.html in response to any request it gets.

    Therefore, my first thoughts would be adding

    DirectoryIndex disabled 
    

    to .htaccess, since strapi.io API runs at / root folder and apache returns the default Apache .html file instead, which is overriding the API request.

    What is happening there exactly?

    1.- Your JavaScript App (strapi.io) asks for data.json at your API point and it gets the contents of an index.html.

    2.- Since the contents of index.html are not JSON, and start with a <, it throws the error message. A JSON file cannot start with <.

    Login or Signup to reply.
  2. You’re probably running into issues because Strapi doesn’t currently implement URL prefixes.

    To get around this, you will probably want to use a subdomain or run Strapi on port 80 from the root directory with “/” instead of “/api”. If you have other projects or apps on the same Apache server, you can use ProxyPassMatch to ignore those paths. For example:

    ProxyPassMatch ^/otherproject !
    ProxyPass "/" http://localhost:1337/
    ProxyPassReverse "/" http://localhost:1337/
    

    In this case the “/admin” route will also work without further modifications.

    Alternatively, some people use Strapi middleware to apply prefixes to their routes (see example here).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search