2

I created a website which uses a nuxtjs frontend and a strapi backend. After deployment to a VPS, I cannot get the strapi routing to work. I read multiple posts about this on the internet and followed the official documentation about nginx proxying but to no evail.

Path: /etc/nginx/sites-available/example.com

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    # Strapi API and Admin
    location /strapi/ {
        rewrite ^/strapi/(.*)$ /$1 break;
        proxy_pass http://127.0.0.1:1337;
        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;
    }
}

Path: backend/config/server.js

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

Visiting example.com reveals the frontend, as intended. Visiting example.com/strapi reveals the following site: enter image description here

Clicking on the 'Open admnistration' button leads me to example.com/admin, which returns my 404 frontend error page.

I would be very thankful for any kind of help.

kampfkuchen
  • 454
  • 1
  • 4
  • 20
  • Hello, what does http://example.com/strapi/admin ? You configuration look good ([the new documentation is online](https://strapi.io/documentation/developer-docs/latest/setup-deployment-guides/configurations.html#available-options)) – Sacha STAFYNIAK Mar 04 '21 at 18:08

3 Answers3

2

Yes. The way strapi is configured was meant to be used with the default root route, so use subdomains in this case.

Example api.example.com when you login to strapi it will hit api.example.com/admin and so on.

Elias Soares
  • 9,884
  • 4
  • 29
  • 59
Dennis Zab
  • 33
  • 3
1

It's easier to use subdomains. But in your case the following urls to access strapi and the admin are :

  • example.com/strapi
  • example.com/strapi/admin
-2

the first location statement is matching with all your http calls so even calls with /strapi path will be redirected to your front end app i think you have to change the order try to move

location /strapi/

before

location /
  • That is not correct. Please have a look at http://nginx.org/en/docs/http/ngx_http_core_module.html#location The example there clearly shows that locations with higher specificity are preferred. – Nico Wiedemann Dec 17 '21 at 14:24