【问题标题】:Running Wordpress on a subfolder within a Laravel app on NGINX在 NGINX 上的 Laravel 应用程序中的子文件夹上运行 Wordpress
【发布时间】:2019-12-22 18:11:47
【问题描述】:

我在 Laravel Forge 上的 Laravel 应用程序的子文件夹上配置 wordpress 时遇到问题(服务器由 nginx 提供支持)。

Wordpress 应用位于 /home/forge/my-domain.com/public/blog 下(my-domain.com 包含我的 laravel 应用)。

我在 SO 上尝试了其他问题的几个答案,但我不断收到错误(主要是“重定向太多”或“502 网关错误”)。

这是我当前的解决方案(取自 here - 导致 502 错误网关,也适用于我的 wordpress-app 的管理区域):

# BLOG START
location @rewriteblog {
    rewrite ^(.*)$ /blog/index.php?q=$uri&$args;
}

location @rewriteblogadmin {
    rewrite ^(.*)$ /blog/wp-admin/index.php?q=$uri&$args;
}

location = /blog/favicon.ico {
    log_not_found off;
    access_log off;
}

location = /blog/robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}

location /blog {
    try_files $uri @rewriteblog;
}

location /blog/wp-admin {
    try_files $uri @rewriteblogadmin;
}

location ~ ^/(blog|blog\/wp-admin)/(.*)\.php(/|$) {
    try_files $uri =404;
    fastcgi_index index.php;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;

    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $realpath_root;

    fastcgi_intercept_errors on;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
}
#BLOG-END

另一个尝试是这样的(502 Bad Gateway):

location /blog {
    root      /home/forge/my-domain.com/public/blog;
    index     index.php;
    try_files $uri $uri/ /blog/index.php?q=$uri;
}

另一个尝试是这样的(管理区域和起始页可访问,帖子导致重定向过多):

location /blog {
    root      /home/forge/my-domain.com/public/;
    index     index.php;
    try_files $uri $uri/ /blog/index.php?q=$uri;
}

另一个尝试是这样的(管理区域和起始页重定向过多,可访问帖子):

location /blog {
    root      /home/forge/faaren.com/public/blog;
    index     index.php;
    try_files $uri $uri/ /index.php?q=$uri;
}

任何想法如何配置我的 nginx 来完成这项工作?

【问题讨论】:

    标签: wordpress laravel nginx


    【解决方案1】:

    保持简单,从工作状态开始,然后扩展。

    您没有发布 NGINX 配置的 Laravel 部分,但让我们假设它的基本要素是:

    root /home/forge/my-domain.com/public;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        fastcgi_pass ...
    }
    

    现在假设我们添加了一个 /blog/ 和您指定的不同 root

    location /blog/ {
        root /home/forge/my-domain.com/public/blog; 
        try_files $uri $uri/ /index.php?$args;
    }
    

    为什么这不起作用是因为对于/blog/ URL,NGINX 从字面上检查/home/forge/my-domain.com/public/blog(指定root)+/blog/index.php(请求URI)的存在(try_files)。

    如果您将root 设置为父文件夹(与 Laravel 的 webroot 相同;换句话说,根本不指定它),那么 NGINX 会尝试:/home/forge/my-domain.com/public (root) + /blog/index.php (request URI),这是正确的。

    对于/blog/foo 类型的 URL(SEO 友好),您希望 NGINX 使用 root + /blog/index.php

    因此,完整的配置:

    root /home/forge/my-domain.com/public;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location /blog/ {
        try_files $uri $uri/ /blog/index.php?$args;
    }
    location ~ \.php$ {
        fastcgi_pass ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-14
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多