【发布时间】:2020-11-11 14:19:12
【问题描述】:
我们有一个由合作伙伴做出反应的网站。我们想在同一个域的子目录中添加一个博客:
- example.org => /var/www/example.org/app
- example.org/blog => /var/www/example.org/blog
我尝试了很多解决方案,但在 example.org/blog 上总是得到 404
server {
server_name example.org;
root /var/www/project/app/functions/build;
access_log /var/log/nginx/example.org_access.log;
error_log /var/log/nginx/example.org_error.log;
index index.html index.htm;
location ^~ /blog/ {
access_log /var/log/nginx/blog-example.org_access.log;
error_log /var/log/nginx/blog-example.orgm_error.log;
alias /var/www/example.org/blog;
index /index.php;
# Add a trailing slash if missing
if (!-f $request_filename) {
rewrite [^/]$ $uri/ permanent;
}
try_files $uri $uri/ /index.php?$args;
}
location / {
try_files $uri @prerender;
}
location @prerender {
// .. config for react stuff
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
# Change this to your fpm socket
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
我尝试使用单独的 nginx 站点 conf 创建一个子域,以测试一切是否正常,并且确实如此。所以这是我上面的 nginx 站点配置不好。
你有什么想法吗? 谢谢你!
解决方案
感谢 Ivan Shatsky,我能够更正我的配置以使一切正常。
我为什么总是有 404 的主要问题是因为我的索引。我有一个额外的斜线:index /index.php => index index.php
location /blog/ {
access_log /var/log/nginx/blog-example.org_access.log;
error_log /var/log/nginx/blog-example.org_error.log;
root /var/www/example.org;
index index.php;
# Add a trailing slash if missing
if (!-f $request_filename) {
rewrite [^/]$ $uri/ permanent;
}
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
# Change this to your fpm socket
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
【问题讨论】:
-
感谢您发布最终配置。错过了
index指令的那个问题,我说我不确定我是否找到了所有的错误:) 如果我需要做同样的事情,我将为此添加书签,你能把你的问题重命名为某事吗像“Nginx 在现有网站的某个 URI 前缀下安装 wordpress”?需要时更容易找到。 -
还有一件事要提。尽管您的特定情况不需要它,但如果将这样的解决方案应用于包含正则表达式匹配位置 (
location ~ <regex> { ... }) 的配置,以防止这些位置捕获任何 WordPress 资产/路由,应使用与 WordPress 相关的位置^~修饰符:location ^~ /blog/ { ... }.