如何使用非常永久链接(SEO友好的URL)在Nginx上与Laravel一起安装WordPress?

前端之家收集整理的这篇文章主要介绍了如何使用非常永久链接(SEO友好的URL)在Nginx上与Laravel一起安装WordPress?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个在Nginx上运行的Laravel站点,它很好.

它有一个普通的文件夹结构,如:

  1. /app
  2. /public
  3. /vendor
  4. ...

/ 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配置是:

  1. server {
  2. #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.
  3. server_name mysite.local;
  4. # The location of our project's public directory.
  5. root F:/code/mysite/public/;
  6. # Point index to the Laravel front controller.
  7. index index.PHP;
  8. location / {
  9. # URLs to attempt,including pretty ones.
  10. try_files $uri $uri/ /index.PHP?$query_string;
  11. }
  12. # Remove trailing slash to please routing system.
  13. if (!-d $request_filename) {
  14. rewrite ^/(.+)/$/$1 permanent;
  15. }
  16. # Yoast wordpress SEO plugin says to add these 2 rewrites:
  17. rewrite ^/blog/sitemap_index\.xml$/blog/index.PHP?sitemap=1 last;
  18. rewrite ^/blog/([^/]+?)-sitemap([0-9]+)?\.xml$/blog/index.PHP?sitemap=$1&sitemap_n=$2 last;
  19. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9123
  20. location ~ \.PHP${
  21. fastcgi_pass 127.0.0.1:9123;
  22. fastcgi_index index.PHP;
  23. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  24. include fastcgi_params;
  25. }
  26. location ~* \.(css|js|gif|jpe?g|png)${
  27. #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
  28. expires 168h;
  29. add_header Pragma public;
  30. add_header Cache-Control "public,must-revalidate,proxy-revalidate";
  31. }
  32. }

我花了几个小时审查其他答案(如下所列),并且没有想出如何使这个工作.

建议?

> https://stackoverflow.com/a/10089936/470749
> https://stackoverflow.com/a/18596822/470749
> https://stackoverflow.com/a/17816122/470749
> https://stackoverflow.com/a/12635095/470749
> https://stackoverflow.com/a/11522602/470749
> https://stackoverflow.com/a/6155935/470749
> https://stackoverflow.com/a/23416206/470749
> http://codex.wordpress.org/Nginx#WordPress_Multisite_Subdirectory_rules
> http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory
> https://rtcamp.com/wordpress-nginx/tutorials/multisite/subdirectories/in-a-subdirectory/

附:我对安装wordpress文件的位置很灵活(例如,在/ public / blog或将其移至/ blog或/ wordpress).

最佳答案
您将所有内容路由到/您所在位置的laravel,但是您需要将所有/ blog /写入/blog/index.PHP中的index.PHP

  1. location /blog/ {
  2. try_files $uri $uri/ @wordpress;
  3. }
  4. location @wordpress {
  5. rewrite /blog/ /blog/index.PHP;
  6. }

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

  1. location ^/blog/index.PHP(/.*)?${
  2. fastcgi_split_path_info ^(/blog/index.PHP)(/.*)$;
  3. fastcgi_pass 127.0.0.1:9123;
  4. fastcgi_index index.PHP;
  5. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  6. fastcgi_param PATH_INFO $fastcgi_path_info;
  7. include fastcgi_param;
  8. }

如果这不起作用并打开日志信息,请打开错误日志的调试详细程度.

更新:原始提问者的注意事项:

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

  1. ...
  2. error_log /Users/myuser/code/myproject/storage/logs/Nginx_error.log debug;
  3. # Point index to the Laravel front controller.
  4. index index.PHP;
  5. location /blog/ {
  6. try_files $uri $uri/ @wordpress;
  7. }
  8. location @wordpress {
  9. rewrite /blog/ /blog/index.PHP;
  10. }
  11. location ^/blog/index.PHP(/.*)?${
  12. fastcgi_split_path_info ^(/blog/index.PHP)(/.*)$;
  13. fastcgi_pass 127.0.0.1:9000;
  14. fastcgi_index index.PHP;
  15. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  16. fastcgi_param PATH_INFO $fastcgi_path_info;
  17. include fastcgi_params;
  18. }
  19. location / {
  20. try_files $uri $uri/ /index.PHP$is_args$args;
  21. }
  22. ...

猜你在找的Nginx相关文章