我目前正在虚拟机上使用Strapi作为后端为我的研究开发一个网络研究。虽然所有的运行都很顺利,但现在我要进行全面部署,我遇到了一个小问题,我似乎无法得到我的头周围。
前端已经在线,运行在Nginx(v.1.18.0)上。为了安全和最佳实践,我为我的域生成了一个SSL证书,并将所有HTTP请求重新路由到工作正常的HTTPS。
然而,Strapi仍然运行在localhost:1337上,没有HTTPS,这导致浏览器拒绝连接。作为回应,我按照Strapi的文档设置了一个代理(Nginx Proxying),但是当我试图 curl 代理时,我得到了一个未解决的主机错误。
我对ngnix和strapi很陌生。当我测试nginx -t
时,它成功响应。然而,代理不工作。
下面,我的档案:
我的./config/env/production/server.js仍然非常基本,看起来如下:
module.exports = ({ env }) => ({
host: env('HOST', '127.0.0.1'),
port: env.int('PORT', 1337),
url: 'https://api.my-domain.com',
app: {
keys: env.array('APP_KEYS'),
},
});
/etc/nginx/配置文件中指定的文件类型
# Strapi server
upstream strapi {
server 127.0.0.1:1337;
}
我的/etc/nginx/sites-available/strapi.conf(在location
中,我添加了return 200 'OK'
用于测试..)
server {
# Listen HTTP
listen 80;
server_name api.my-domain.com;
# Redirect HTTP to HTTPS
return 301 https://$host$request_uri;
}
server {
# Listen HTTPS
listen 443 ssl;
server_name api.my-domain.com;
# SSL config
ssl_certificate path/to/certificate/fullchain.pem
ssl_certificate_key path/to/certificate/privkey.pem
# Proxy Config
location / {
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;
return 200 "OK";
}
}
我将默认域更改为一个自定义文件-将继续在这里调用它默认thoguh:
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
include snippets/self-signed.conf;
include snippets/ssl-params.conf;
root /var/www/my-domain/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name my-domain.com www.my-domain.com;
location / {
# First attempt to serve request as file, then
try_files $uri $uri/ =404;
}
}
server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl ;
listen [::]:443 ssl ;
include snippets/self-signed.conf;
include snippets/ssl-params.conf;
root /var/www/my-domain.com/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name my-domain.com; # managed by Certbot
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
ssl_certificate path/to/certificate/fullchain.pem
ssl_certificate_key path/to/certificate/privkey.pem
}
提前感谢!
- 表带版本**:4.4.3
- 操作系统**:Ubuntu 20.04.5语言版本
- 数据库**:MySQL数据库
- 节点版本**:版本18.10.0
- NPM版本**:8.19.2
- Yarn型号**:1.22.19
1条答案
按热度按时间r6l8ljro1#
事实证明,遵循多个指南可以很快把任何Nginx配置变成一团乱麻。我检查了所有这些指南并清理了我的文件。最终的结果就像一个魅力: