我如何配置 nginx(和 strapi)以在同一台服务器上正确地服务两个 strapi 实例?
How do i configure nginx (and strapis) to correctly serve two strapi instances on the same server?
我想在我的服务器上托管两个小网站,它们都是用 strapi 后端和 React 前端制作的,这是一个数字海洋水滴。
我已经为其中一个网站配置了 nginx,并且一切正常。我可以从 site1.com/dashboard 访问 strapi,我的查询指向 site1.com/api/graphql。为此,我遵循了一些教程。
这是我添加的 nginx 文件:
/etc/nginx/sites-available/site1.com:
server {
listen 80;
listen [::]:80;
root /var/www/site1.com/react;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name site1.com www.site1.com;
location /api/ {
rewrite ^/api/(.*)$ / 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;
}
location /dashboard {
proxy_pass http://strapi/dashboard;
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;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
}
}
文件/etc/nginx/conf.d/upstream.conf:
upstream strapi {
server 127.0.0.1:1337;
}
我现在想做的是将另一个网站部署到同一台服务器并配置 nginx 来为其提供服务(在另一个域上,例如 site2.com ).
所以我添加了第二个网站的nginx虚拟服务器文件,复制了第一个并更改了域名和根目录。
站点 2 前端 现在 正常工作 并且可以在其域上访问。
然而,当我启动站点 2 strapi 实例时,它说端口 1337 已被使用(显然,它被站点 1 strapi 实例使用,即 运行 pm2)。所以我将 strapi 配置中的端口更改为 1338(可以吗?)但现在我不知道要在 nginx 中做什么才能为不同域上的两个不同 strapi 实例提供服务。
您在 proxy_pass
指令中使用的主机名是您在单独的配置文件中定义的上游名称。这基本上是对一个或多个真实后端的间接访问。在您的情况下,应用程序 运行ning 在同一台计算机上的端口 1337 上。它也可以是 nginx 负责将负载分配到的外部域列表。
您使用额外虚拟主机的方法看起来不错。如果您的 proxy_pass
指令仍然指向 site2 的 http://strapi
(可能仍然解析为 `localhost:1337),则已经在 site2 下“工作”的前端可能是在新域下服务的旧实例。 =17=]
正如您已经提到的,后端的第二个实例需要 运行 在不同的端口上。您使用的端口号并不重要,因为您将通过上游配置来控制它。只需确保不要使用低于 1024 的端口号(这需要 root 权限),不要与系统上的其他进程冲突(您会注意到)并且作为最佳实践,不要使用其他一些默认的端口号协议(也许检查 this list)。
要修复您的设置,只需像第一个一样定义第二个上游,但指向新的 url,例如localhost:1338
然后在 site2 的 proxy_pass
指令中引用这个新名称。
从技术上讲,每个上游只有一个后端,您也可以完全跳过上游部分并直接在 proxy_pass
指令中引用 URL,但是使用 shorthand 名称可以还支持配置的可读性。
我想在我的服务器上托管两个小网站,它们都是用 strapi 后端和 React 前端制作的,这是一个数字海洋水滴。
我已经为其中一个网站配置了 nginx,并且一切正常。我可以从 site1.com/dashboard 访问 strapi,我的查询指向 site1.com/api/graphql。为此,我遵循了一些教程。 这是我添加的 nginx 文件:
/etc/nginx/sites-available/site1.com:
server {
listen 80;
listen [::]:80;
root /var/www/site1.com/react;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name site1.com www.site1.com;
location /api/ {
rewrite ^/api/(.*)$ / 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;
}
location /dashboard {
proxy_pass http://strapi/dashboard;
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;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
}
}
文件/etc/nginx/conf.d/upstream.conf:
upstream strapi {
server 127.0.0.1:1337;
}
我现在想做的是将另一个网站部署到同一台服务器并配置 nginx 来为其提供服务(在另一个域上,例如 site2.com ).
所以我添加了第二个网站的nginx虚拟服务器文件,复制了第一个并更改了域名和根目录。 站点 2 前端 现在 正常工作 并且可以在其域上访问。
然而,当我启动站点 2 strapi 实例时,它说端口 1337 已被使用(显然,它被站点 1 strapi 实例使用,即 运行 pm2)。所以我将 strapi 配置中的端口更改为 1338(可以吗?)但现在我不知道要在 nginx 中做什么才能为不同域上的两个不同 strapi 实例提供服务。
您在 proxy_pass
指令中使用的主机名是您在单独的配置文件中定义的上游名称。这基本上是对一个或多个真实后端的间接访问。在您的情况下,应用程序 运行ning 在同一台计算机上的端口 1337 上。它也可以是 nginx 负责将负载分配到的外部域列表。
您使用额外虚拟主机的方法看起来不错。如果您的 proxy_pass
指令仍然指向 site2 的 http://strapi
(可能仍然解析为 `localhost:1337),则已经在 site2 下“工作”的前端可能是在新域下服务的旧实例。 =17=]
正如您已经提到的,后端的第二个实例需要 运行 在不同的端口上。您使用的端口号并不重要,因为您将通过上游配置来控制它。只需确保不要使用低于 1024 的端口号(这需要 root 权限),不要与系统上的其他进程冲突(您会注意到)并且作为最佳实践,不要使用其他一些默认的端口号协议(也许检查 this list)。
要修复您的设置,只需像第一个一样定义第二个上游,但指向新的 url,例如localhost:1338
然后在 site2 的 proxy_pass
指令中引用这个新名称。
从技术上讲,每个上游只有一个后端,您也可以完全跳过上游部分并直接在 proxy_pass
指令中引用 URL,但是使用 shorthand 名称可以还支持配置的可读性。