【问题标题】:Nginx install wordpress under some URI prefix on existing websiteNginx 在现有网站的某个 URI 前缀下安装 wordpress
【发布时间】: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/ { ... }.

标签: wordpress nginx


【解决方案1】:

不确定这是否是一个答案,但我已经看到的错误是:

  • 如果您的 React 应用程序不使用 PHP(我想它没有)为什么要使用 location ^~ /blog/ { ... } 在此块下方有一个 PHP 处理程序?这样,对 /blog/any/path/file.php 的请求就不会到达那个 PHP 处理程序。使用简单的 location /blog/ { ... } 前缀位置或将 PHP 处理程序移动到 location ^~ /blog/ { ... } 内,使其成为嵌套位置(首选)。
  • 如果你的WP所在目录是/var/www/example.org/blog,你的URI前缀是/blog/,你最好使用root /var/www/example.org;而不是alias /var/www/example.org/blog;。如果这些字符串不匹配,请在 alias 指令参数的末尾添加斜杠:alias /var/www/example.org/blog/;
  • 您错误地使用了try_files 指令,最后一个参数应该是一个URI,因此要将所有请求重定向到WP 索引文件,您应该使用try_files $uri $uri/ /blog/index.php?$args;
  • 您的 PHP 处理程序使用全局 /var/www/project/app/functions/build 根目录而不是 WordPress 根目录(如果您将 PHP 处理程序设置为嵌套位置,这个位置会自动消失)。

不确定仅此而已,但让我们从修复这些错误开始。

更新

最终的工作配置是由 OP 作为原始问题更新添加的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多