googleman
googleman

Reputation: 31

Configure Strapi to work with SSL (with proxy)

I'd like to set up Strapi so that mywebsite.de/api is redirected from Strapi's default port :1337. But at /api, my backend is not accessible.

My frontend (on Gridsome Site Generator) is set to port 443 with an SSL certificate. I configured /etc/nginx/sites-available/default as follows

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name mywebsite.de;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    server_name mywebsite.de;

    ssl_certificate /etc/letsencrypt/live/mywebsite.de/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.de/privkey.pem;

    location /api/ {
        proxy_pass http://localhost:1337;
        proxy_set_header Host $host;
    }

    root /home/debian/frontend/dist;
    index index.html index.htm index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

my backend config backend/config/server.js looks like this:

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  url: "https://mywebsite.de/api", (just /api doesn't work either)
  admin: {
    auth: {
      secret: env('ADMIN_JWT_SECRET', 'mysecret'),
    },
  },
});

When I run "npm run develop" (for testing) I see this in console:

> [email protected] develop /home/debian/backend
> strapi develop
...
ā”‚ Version            ā”‚ 3.6.11 (node v14.17.5)                           ā”‚
...
To manage your project šŸš€, go to the administration panel at:
https://mywebsite.de/api/admin

To access the server āš”ļø, go to:
https://mywebsite.de/api

[2023-05-15T19:29:30.683Z] debug GET /api/ (20 ms) 404

the last line is when I try to access https://mywebsite.de/api In the browser I just see "Not Found"

When I access https://mywebsite.de/api/uploads/mypicture.jpg

[2023-05-15T19:55:27.324Z] debug GET /api/uploads/mypicture.jpg (16 ms) 404

but this http://11.222.3.44:1337/uploads/mypicture.jpg works:

[2023-05-15T19:57:07.929Z] debug GET /uploads/mypicture.jpg (26 ms) 200

What did I forget when setting up a proxy or Strapi? I would appreciate any advice on the subject.

Upvotes: 1

Views: 1306

Answers (1)

googleman
googleman

Reputation: 31

The solution was in Nginx configuration file. It should be like that:

server {
listen 443 ssl;
listen [::]:443 ssl;
server_name mywebsite.de;

ssl_certificate /etc/letsencrypt/live/mywebsite.de/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.de/privkey.pem;

  location / {
        root /home/debian/frontend/dist;
  }

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

Upvotes: 2

Related Questions