nginx - 如何使用 Nginx 在子目录中安装 Ghost?(得到 404)
问题描述
我是使用反向代理或管理技术基础设施的初学者。我在服务器上有我的主应用程序(react + nginx),我正在尝试在与我的主应用程序相同的服务器上的子文件夹https://tinymentions.io/blog上安装 Ghost。
不幸的是,我无法成功配置 Nginx 以正确地为我的 Ghost 博客提供服务。当我访问 URL ttps://tinymentions.io/blog 时,我收到一个错误:“HTTP/1.1 404 NOT FOUND”
我搜索了 Ghost 论坛、Stack Overflow 和整个 Google,尝试了不同的方法,但不幸的是,我仍然无法为这个问题找到解决方案。每次我尝试一种新方法时,我都会重新启动 nginx,sudo service nginx restart
但没有运气。
我将不胜感激。如果我没有正确解释自己和/或需要更多信息,请告诉我。
- 你的网址是什么?
我的主要应用程序位于https://tinymentions.io/
- 幽灵网址
- 您使用的是什么版本的 Ghost?
Ghost-CLI 版本:1.15.0 Ghost 版本:3.35.5
- 什么配置?
这是我的 NGINX 配置文件:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name tinymentions.io www.tinymentions.io;
client_max_body_size 4G;
root /var/www/tiny-mentions/build;
index index.html;
location / {
try_files $uri $uri/ @proxy_to_app;
}
location /blog {
proxy_http_version 1.1;
proxy_set_header Update &http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_pass http://localhost:2368;
}
location @proxy_to_app {
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_connect_timeout 25000s;
proxy_read_timeout 25000;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
proxy_redirect off;
proxy_pass http://localhost:5000;
}
ssl_certificate /etc/letsencrypt/live/tinymentions.io/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/tinymentions.io/privkey.pem; # managed by Certbot
}
server {
if ($host = www.tinymentions.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = tinymentions.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name tinymentions.io www.tinymentions.io;
return 301 https://$server_name$request_uri;
}
以下是我的 config.production.json 文件:
{
"url": "http://tinymentions.io/blog",
"server": {
"port": 2368,
"host": "127.0.0.1"
},
"database": {
"client": "mysql",
"connection": {
"host": "localhost",
"user": "hidden",
"password": "hidden",
"database": "hidden"
}
},
"mail": {
"transport": "Direct"
},
"logging": {
"transports": [
"file",
"stdout"
]
},
"process": "systemd",
"paths": {
"contentPath": "/var/www/ghost/content"
}
}
解决方案
最后,我能够解决这个问题。
似乎 nginx 没有正确重新启动,所以我不得不使用命令停止 nginxsudo systemctl stop nginx
并再次启动它,sudo systemctl start nginx
以尝试不同的配置,看看这些配置是否最终起作用。
我的 NGINX 配置文件中的 location /blog 配置如下:
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Update &http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_pass http://localhost:2368;
}
以下是我的 config.production.json 文件:
{
"url": "https://tinymentions.io/blog",
"server": {
"port": 2368,
"host": "127.0.0.1"
},
"database": {
"client": "mysql",
"connection": {
"host": "localhost",
"user": "hidden",
"password": "hidden",
"database": "hidden"
}
},
"mail": {
"transport": "Direct"
},
"logging": {
"transports": [
"file",
"stdout"
]
},
"process": "systemd",
"paths": {
"contentPath": "hidden"
}
}
推荐阅读
- node.js - Node.js 后端/服务器:在“新函数”实例中无法“要求”
- python - 在 python 函数中执行 bash 命令行
- python - 为什么我在 Python 上使用 BackTrader 会收到此错误?
- spring-mvc - 如何使用 ResponseEntity 从我的 REST 调用中返回简单的错误消息?
- javascript - Angular 的订阅未记录完整错误
- python - keras,无效的预测大小
- java - 如何使 Spring Projections 与 @OneToOne 关系一起工作?
- react-native - 使用 React Navigation 防止在选项卡更改时重新渲染
- r - 在 R 中绘制 geom_line 和 geom_ribbon 时如何添加不连续性?
- regex - 修改 Go 正则表达式,使其不拾取最后一个字符