本文介绍了Nginx 在现有网站的某个 URI 前缀下安装 wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个由合作伙伴做出反应的网站.我们想在同一个域的子目录中添加一个博客:

We have a website build with react by a partener. And we want to add a blog on the same domain, in a subdirectory:

  • example.org =>/var/www/example.org/app
  • example.org/blog =>/var/www/example.org/blog

我尝试了很多解决方案,但我总是在 example.org/blog 上收到 404

I tried many solutions, but I always got a 404 on example.org/blog

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 站点配置不好.

I tried to create a subdomain with a separate nginx site conf to test if everything was OK, and it was. So it's my nginx site config above which is not good.

你有什么想法吗?谢谢!

Do you have any idea?Thanks you!

解决方案

感谢 Ivan Shatsky,我能够更正我的配置以使一切正常.我为什么总是有 404 的主要问题是因为我的索引.我有一个额外的斜线:index/index.php =>index index.php

Thanks to Ivan Shatsky, I was able to correct my config to make everything works.My main issue why I always had a 404 was because of my index. I had an extra slash: 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;
    }
    
}

推荐答案

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

Not sure if this will be an answer, but the errors I already see are:

  • 如果你的 React 应用程序没有使用 PHP(我想它没有使用),你为什么要使用 location ^~/blog/{ ... } 在下面有一个 PHP 处理程序这个块?这样,对 /blog/any/path/file.php 的请求就不会到达那个 PHP 处理程序.使用简单的 location/blog/{ ... } 前缀 location 或将 PHP 处理程序移动到 location ^~/blog/{ ... } 使其嵌套位置(首选).
  • 如果你的 WP 所在目录是 /var/www/example.org/blog 并且你的 URI pefix 是 /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 处理程序设置为嵌套位置,则该目录会自动消失).
  • If your react app doesn't make use of PHP (and I suppose it doesn't) why are you use location ^~ /blog/ { ... } having a PHP handler below this block? This way no request for /blog/any/path/file.php ever reach that PHP handler. Use simple location /blog/ { ... } prefix location or move the PHP handler inside the location ^~ /blog/ { ... } making it nested location (preferred).
  • If your directory where the WP is located is /var/www/example.org/blog and your URI pefix is /blog/ you'd better use root /var/www/example.org; instead of alias /var/www/example.org/blog;. It those string doesn't match, add the trailing slash at the end of the alias directive argument: alias /var/www/example.org/blog/;
  • You are using try_files directive incorectly, the last argument supposed to be an URI, so to redirect all the requests to WP index file you should use try_files $uri $uri/ /blog/index.php?$args;
  • Your PHP handler uses global /var/www/project/app/functions/build root instead of WordPress one (if you'd make the PHP handler nested location, this one would gone automatically).

不确定这就是全部,但让我们从修复这些错误开始.

Not sure that's all, but lets start from fixing these errors.

更新

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

The final working configuration was added by OP as the original question update.

这篇关于Nginx 在现有网站的某个 URI 前缀下安装 wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 17:42