1

I have strapi (as a backend) and nextJs (as a frontend) serving from the same domain. so i used “Subfolder unified” nginx configuration from strapi docuentation (Nginx Proxying | Strapi Documentation), to reverse proxy all the strapi code from domainname.com/backend such .

Problem: now strapi shows “The server is running successfully.” with “production” text, in the url domainname.com/backend, however if i run domainname.com/backend/admin, it does not show strapi login screen. Instead it shows blank in brower, with following error in console.

browser console error: Uncaught SyntaxError: Unexpected token ‘<’ (at runtime~main.fa3b5436.js:1:1) main.cebf08e6.js:1 Uncaught SyntaxError: Unexpected token ‘<’ (at main.cebf08e6.js:1:1)

Debugging attempted:

tried reverse proxing only strapi from nginx without nextjs config, and everything works, that is strapi admin login opens in path domainname.com/admin. This proves we have no problem with strapi application server.

now tried running with nextjs reverse proxy code, and nextjs opens at rootdomain, and strapi shows “The server is running successfully.” at url domainname.com/backend enter image description here

also tried accessing images in public/upload folder with url: http://domainname.com/backend/uploads/placeholder.png and picture does open in browser.

Tried to open strapi admin login sceen with url: http://domainname.com/backend/admin and i get blank screen with console error: enter image description here

my nginx file: /etc/nginx/sites-available/strapi.conf is:

    # Listen HTTP
    listen 80;
    server_name domainname.com;

    # Next.js
    location / {
        proxy_pass http://127.0.0.1:3000;  # Replace with the appropriate Next.js server address
        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 /backend/ {
         rewrite ^/backend/?(.*)$ /$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;
    }
}

my upstream file: /etc/nginx/conf.d/upstream.conf

upstream strapi {
    server 127.0.0.1:1337;
}```

1 Answer 1

1

Well after deep digging problem was how strapi was configured.

changes: add url in server.js at "./config/server.js". Also add proxy: true eg:

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  app: {
    keys: env.array('APP_KEYS'),
  },
  webhooks: {
    populateRelations: env.bool('WEBHOOKS_POPULATE_RELATIONS', false),
  },
  proxy: true,
  url: "https://yoursite.com/backend",
});

adding url https://yoursite.com/backend will automatically create following links, please review Subfolder unified Strapi at https://docs.strapi.io/dev-docs/deployment/nginx-proxy):

Example domain: https://yoursite.com/backend
Example admin: https://yoursite.com/backend/admin
Example API: https://yoursite.com/backend/api
Example uploaded Files (local provider): https://yoursite.com/backend/uploads

Now we need to make changes in admin.js file at "./config/admin.js":

module.exports = ({ env }) => ({
  auth: {
    secret: env('ADMIN_JWT_SECRET'),
  },
  apiToken: {
    salt: env('API_TOKEN_SALT'),
  },
  transfer: {
    token: {
      salt: env('TRANSFER_TOKEN_SALT'),
    },
  },
  port: 1337,
  url: `https://yoursite.com/admin`
});

Note: If you do not have https you might have to change middleware, please see example below security section in strapi documentation: https://docs.strapi.io/dev-docs/configurations/middlewares#security

Very Imp: Lastly you must build strapi app other wise above configuration change will not apply. Make sure you do this:

NODE_ENV=production npm run build
or
NODE_ENV=production yarn run build
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.