简体   繁体   English

Strapi/Nginx - Strapi 路由在生产中不起作用

[英]Strapi/Nginx - Strapi routing does not work in production

I created a website which uses a nuxtjs frontend and a strapi backend.我创建了一个使用nuxtjs前端和Strapi后端的网站。 After deployment to a VPS, I cannot get the strapi routing to work.部署到 VPS 后,我无法让strapi 路由工作。 I read multiple posts about this on the internet and followed the official documentation about nginx proxying but to no evail.我在互联网上阅读了多篇关于此的帖子,并遵循了有关 nginx 代理的官方文档,但没有成功。

Path: /etc/nginx/sites-available/example.com路径:/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路径:后端/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.访问 example.com 按预期显示前端。 Visiting example.com/strapi reveals the following site:访问 example.com/strapi 会显示以下站点: 在此处输入图像描述

Clicking on the 'Open admnistration' button leads me to example.com/admin, which returns my 404 frontend error page.单击“打开管理”按钮将我带到 example.com/admin,它会返回我的 404 前端错误页面。

I would be very thankful for any kind of help.我会非常感谢任何形式的帮助。

I had the same issue once我曾经遇到过同样的问题

Now I use subdomains configurations to avoid these problems现在我使用子域配置来避免这些问题

It is much better好多了

Here's my configuration这是我的配置

NGINX NGINX

server {

    listen 80;
    server_name example.com www.example.com;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Client-IP $remote_addr;
    proxy_set_header X-Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
    client_max_body_size 0;

    charset utf-8;

    location / {
        proxy_pass http://webapp:9984;
        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;
    }

}

server {
    listen 80;
    server_name api.example.com www.api.example.com;

    location / {
        proxy_pass http://backend:13131;
        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;
    }
}

STRAPI STRAPI

module.exports = ({ env }) => ({
    host: env('HOST', '0.0.0.0'),
    port: env.int('PORT', 1337),
    url: `http://api.example.com/`,
    admin: {
        auth: {
            secret: env('ADMIN_JWT_SECRET', '----'),
        },
    },
});

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第一个位置语句与您所有的 http 调用匹配,因此即使使用 /strapi 路径的调用也将被重定向到您的前端应用程序,我认为您必须更改顺序尝试移动

location /strapi/

before

location /

Yes.是的。 The way strapi is configured was meant to be used with the default root route, so use subdomains in this case.配置strapi的方式是为了与默认根路由一起使用,所以在这种情况下使用子域。

Example api.example.com when you login to strapi it will hit api.example.com/admin and so on.示例api.example.com当您登录到strapi时,它将点击api.example.com/admin等等。

It's easier to use subdomains.使用子域更容易。 But in your case the following urls to access strapi and the admin are:但在您的情况下,访问strapi和管理员的以下网址是:

  • example.com/strapi example.com/strapi
  • example.com/strapi/admin example.com/strapi/admin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM