【问题标题】:How to install WordPress alongside Laravel on Nginx with pretty permalinks (SEO-friendly URLs)?如何使用漂亮的永久链接(SEO 友好的 URL)在 Nginx 上与 Laravel 一起安装 WordPress?
【发布时间】:2014-07-01 23:04:36
【问题描述】:

我有一个在 Nginx 上运行的 Laravel 站点,没问题。

它有一个正常的文件夹结构,如:

/app
/public
/vendor
...

/public 文件夹是 Laravel index.php 所在的位置。

我在 /public/blog 安装了 WordPress,因为我希望我的博客在 mywebsite.org/blog 上可见。

如果我将/blog/wp-admin/options-permalink.php 中定义的永久链接设置设置为“默认”(这意味着帖子的 URL 看起来像 /blog/?p=123),则该博客目前可以正常工作。 如果我将永久链接设置更改为 /blog/%postname%/,我将无法查看帖子(我得到一个 Laravel 404 页面)。

我绝对希望我的博客帖子具有对 SEO 友好的 URL(漂亮的永久链接)。

我当前的 Nginx 配置是:

server {
    #This config is based on https://github.com/daylerees/laravel-website-configs/blob/6db24701073dbe34d2d58fea3a3c6b3c0cd5685b/nginx.conf and seemed to be necessary to get Laravel working.
    server_name mysite.local;

     # The location of our project's public directory.
    root F:/code/mysite/public/;

     # Point index to the Laravel front controller.
    index           index.php;

    location / {
        # URLs to attempt, including pretty ones.
        try_files   $uri $uri/ /index.php?$query_string;
    }

    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
            rewrite     ^/(.+)/$ /$1 permanent;
    }

    # Yoast WordPress SEO plugin says to add these 2 rewrites:
    rewrite ^/blog/sitemap_index\.xml$ /blog/index.php?sitemap=1 last;
    rewrite ^/blog/([^/]+?)-sitemap([0-9]+)?\.xml$ /blog/index.php?sitemap=$1&sitemap_n=$2 last;

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9123
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9123;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~* \.(css|js|gif|jpe?g|png)$ {
        #images, CSS, and JS have 1 week expiration: http://aspyct.org/blog/2012/08/20/setting-up-http-cache-and-gzip-with-nginx/ See also: http://serverfault.com/questions/339240/chromium-audit-says-its-not-caching-static-content-yet-headers-are-set-who-i
        expires 168h;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

}

我花了几个小时查看其他答案(如下所列),但还没有弄清楚如何让它发挥作用。

建议?

附:我可以灵活选择安装 WordPress 文件的位置(例如,在 /public/blog 或将其上移至 /blog/wordpress)。

【问题讨论】:

  • 嘿,哥们,在这里回答你的号召:) 正如我在我的博客上发布的链接所讨论的那样,你是否尝试过将 wordpress 上移一层并为其添加位置,如下所述: tom.londondroids.com/2011/11/… ?欢呼
  • @gru 非常感谢。是的,我试过了。我认为梅尔文在下面的回答看起来不错,对吧?它似乎对我有用!

标签: wordpress nginx laravel


【解决方案1】:

您将所有内容路由到您/ 位置的laravel,但您需要将所有/blog/ 写入/blog/index.php 中的index.php:

location /blog/ {
    try_files $uri $uri/ @wordpress;
}

location @wordpress {
    rewrite /blog/ /blog/index.php;
}

那么你的 php 处理程序需要路径信息支持:

location ^/blog/index.php(/.*)?$ {
    fastcgi_split_path_info ^(/blog/index.php)(/.*)$;
    fastcgi_pass   127.0.0.1:9123;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    include fastcgi_param;
}

如果这不起作用,请打开错误日志的详细调试并发布日志信息。

更新:来自原始提问者的注释:

这是我的新 Nginx 配置的 sn-p,它似乎适用于以下 URL:/、/blog、/course、/blog/innately-happy 和 /blog/sitemap_index.xml

...
error_log /Users/myuser/code/myproject/storage/logs/nginx_error.log debug;

 # Point index to the Laravel front controller.
index           index.php;

location /blog/ {
    try_files $uri $uri/ @wordpress;
}

location @wordpress {
    rewrite /blog/ /blog/index.php;
}

location ^/blog/index.php(/.*)?$ {
    fastcgi_split_path_info ^(/blog/index.php)(/.*)$;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    include fastcgi_params;
}

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}
...

【讨论】:

  • 谢谢!一切似乎都在工作!我想也许github.com/daylerees/laravel-website-configs/blob/… 一开始就不是一个好主意。我最终以digitalocean.com/community/articles/… 为例。我会看看是否可以将我的git diff 添加到您的答案中,以便人们可以确切地知道我为使其正常工作所做的更改。注意:在完成测试并准备部署到生产环境时删除 `debug`。
  • 这个例子对我有用,但有一个主要例外。重写破坏了我所有的 css 和 js 引用。
  • 我遵循了完全相同的步骤,但由于某种原因,当我转到博客文件夹时,出现“重定向过多”错误。
  • @brs14ku 您可以将重写限制为仅允许放入 slug 中的内容。喜欢rewrite /blog/([a-zA-Z0-9/-]+)$ /blog/index.php/$1。这假设您不允许在您的 slug 中使用点并且确实为您的资源使用扩展名 (.js/.css)。或者,您可以对它们所在的目录使用前瞻断言/blog/.*(?!wp-includes)$(我假设是 wp-includes)。
  • 谢谢。我认为我们需要更新这个“astcgi_pass 127.0.0.1:9123;”到“unix:/var/run/php5-fpm.sock;”以便它可以在服务器上运行。
猜你喜欢
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
  • 2021-07-26
  • 2011-01-29
相关资源
最近更新 更多