如何配置nginx配置以用于strapi和nuxt?

huangapple go评论86阅读模式
英文:

How to configure nginx config for strapi and nuxt?

问题

我需要配置nginx以使站点nuxt可以在常规地址访问,管理员面板strapi可以在地址/api上访问。站点本身可以工作并接收数据,但无法从strapiuploads文件夹获取图像。

此外,我无法进入strapi管理员面板,因为strapi的主页是欢迎页,然后重定向到/admin页面,但我没有这个页面。我尝试了许多配置变化,但没有成功。

我需要做什么?

/etc/nginx/conf.d/upstream.conf

upstream strapi {
    server localhost:1337;
}

upstream nuxt {
    server localhost:3000;
}

nuxt.config.ts

vite: {
    server: {
        hmr: {
            protocol: "wss",
            clientPort: 443,
            path: "hmr/",
        },
    },
}

confing

server {
    listen 80;
    listen 0.0.0.01:80;
    server_name example.com www.example.com;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    listen 0.0.0.01:443 ssl http2;

    server_name www.example.com;
    return 301 https://example.com$request_uri;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    include snippets/ssl-params.conf;
}

server {
    listen 443 ssl http2;
    listen 0.0.0.01:443 ssl http2;

    server_name example.com;
    root /var/www/example.com/client;
    index index.mjs index.html index.xml;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    include snippets/ssl-params.conf;

    rewrite ^/(.*) /$1 break;

    location / {
        proxy_set_header Host $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_redirect off;
        proxy_buffering on;
        proxy_cache_valid 200 1d;
        proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
        proxy_pass http://nuxt;
        proxy_read_timeout 1m;
        proxy_connect_timeout 1m;
        include proxy_params;
    }

    location /api/ {
        rewrite ^/api/(.*)$ /$1 break;
        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;
        proxy_pass http://strapi;
        include proxy_params;
    }

    location /_nuxt/hmr/ {
        proxy_pass http://localhost:24678;
        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;
    }
}
英文:

I need to configure the nginx configuration so that the site nuxt is accessible at the usual address, the admin panel strapi at the address /api. The site itself works and receives data, but does not receive images from the uploads folder from strapi.

Also, I can't get into the strapi admin panel, since the strapi main page is welcome, then there is a redirect to the /admin page, which I don't have. I've tried a lot of configuration variations, but I'm not getting it.

What do I need to do?

/etc/nginx/conf.d/upstream.conf

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

upstream strapi {
server localhost:1337;
}
upstream nuxt {
server localhost:3000;
}

<!-- end snippet -->

nuxt.config.ts

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 vite: {
server: {
hmr: {
protocol: &quot;wss&quot;,
clientPort: 443,
path: &quot;hmr/&quot;,
},
},
},

<!-- end snippet -->

confing

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

server {
listen 80;
listen 0.0.0.01:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
listen 0.0.0.01:443 ssl http2;
server_name www.example.com;
return 301 https://example.com$request_uri;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl-params.conf;
}
server {
listen 443 ssl http2;
listen 0.0.0.01:443 ssl http2;
server_name example.com;
root /var/www/example.com/client;
index index.mjs index.html index.xml;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl-params.conf;
rewrite ^/(.*) /$1 break;
location / {
proxy_set_header        Host $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_redirect          off;
proxy_buffering         on;
proxy_cache_valid       200 1d;
proxy_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_pass              http://nuxt;
proxy_read_timeout      1m;
proxy_connect_timeout   1m;
include proxy_params;
}
location /api/ {
rewrite ^/api/(.*)$ /$1 break;
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 &quot;Upgrade&quot;;
proxy_pass_request_headers on;
proxy_pass http://strapi;
include proxy_params;
}
location /_nuxt/hmr/ {
proxy_pass http://localhost:24678;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection &quot;upgrade&quot;;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

<!-- end snippet -->

答案1

得分: 0

首先阅读这个

Strapi仅支持子域strapi.exaple.comexample.com/strapi

按照指南,您需要创建一个打开管理员面板strapi的URL。

我从指南中排除了serveAdminPanel: false这一行。

字符串url: &#39;http://yourbackend.com必须包含strapi前端部分的路径。

.env文件中,您站点的所有地址都应该使用https而不是http,这对于应用程序的前端部分和后端部分都是必要的。

英文:

First read this

Strapi works only with subdomain strapi.exaple.com or example.com/strapi.

Following the guide, you need to create a url that will open the admin panel strapi.

I excluded the line serveAdminPanel: false from the guide.

String url: &#39;http://yourbackend.com must contain the path to the frontend part of strapi

All the addresses of your sites in the .env files should have https instead of http, this is necessary in the frontend part and in the backend part of your application

huangapple
  • 本文由 发表于 2023年5月28日 19:09:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定