您现在的位置是:首页 > 网站制作 > WordpressWordpress

wordpress nginx多站点rewrite(重写)规则

杰帅2023-06-19【Wordpress】人已围观

简介wordpress多站点模式可以被应用在多种方式上。其中最常用的是在”子目录”模式或者”二级域名”模式上。
Nginx提供了两种特殊的指令:”x-accel-redirect”和”map”,使用这两个指令可以使得wordpress多站点的网络服务实现伪静态功能。

wordpress多站点模式可以被应用在多种方式上。其中最常用的是在”子目录”模式或者”二级域名”模式上。
Nginx提供了两种特殊的指令:”x-accel-redirect”和”map”,使用这两个指令可以使得wordpress多站点的网络服务实现伪静态功能。

wordpress多站点使用子目录重写规则

配置中shouce.ren修改为自己的站点域名。


 
  1. map $uri $blogname{
  2. ~^(?P<blogpath>/[^/]+/)files/(.*) $blogpath ;
  3. }
  4.  
  5. map $blogname $blogid{
  6. default -999;
  7.  
  8. #Ref: http://wordpress.org/extend/plugins/nginx-helper/
  9. #include /var/www/wordpress/wp-content/plugins/nginx-helper/map.conf ;
  10. }
  11.  
  12. server {
  13. server_name shouce.ren ;
  14.  
  15. root /var/www/shouce.ren/htdocs;
  16. index index.php;
  17.  
  18. #多站点配置
  19. location ~ ^(/[^/]+/)?files/(.+) {
  20. try_files /wp-content/blogs.dir/$blogid/files/$2 /wp-includes/ms-files.php?file=$2 ;
  21. access_log off; log_not_found off; expires max;
  22. }
  23.  
  24. #avoid php readfile()
  25. location ^~ /blogs.dir {
  26. internal;
  27. alias /var/www/shouce.ren/htdocs/wp-content/blogs.dir ;
  28. access_log off; log_not_found off; expires max;
  29. }
  30.  
  31. if (!-e $request_filename) {
  32. rewrite /wp-admin$ $scheme://$host$uri/ permanent;
  33. rewrite ^(/[^/]+)?(/wp-.*) $2 last;
  34. rewrite ^(/[^/]+)?(/.*\.php) $2 last;
  35. }
  36.  
  37. location / {
  38. try_files $uri $uri/ /index.php?$args ;
  39. }
  40.  
  41. location ~ \.php$ {
  42. try_files $uri =404;
  43. include fastcgi_params;
  44. fastcgi_pass php;
  45. }
  46.  
  47. #此处可以继续添加伪静态规则
  48. }

wordpress多站二级域名重写规则

配置中shouce.ren修改为自己的站点域名。


 
  1. map $http_host $blogid {
  2. default -999;
  3.  
  4. #Ref: http://wordpress.org/extend/plugins/nginx-helper/
  5. #include /var/www/wordpress/wp-content/plugins/nginx-helper/map.conf ;
  6.  
  7. }
  8.  
  9. server {
  10. server_name shouce.ren *.shouce.ren ;
  11.  
  12. root /var/www/shouce.ren/htdocs;
  13. index index.php;
  14.  
  15. location / {
  16. try_files $uri $uri/ /index.php?$args ;
  17. }
  18.  
  19. location ~ \.php$ {
  20. try_files $uri =404;
  21. include fastcgi_params;
  22. fastcgi_pass php;
  23. }
  24.  
  25. #WPMU Files
  26. location ~ ^/files/(.*)$ {
  27. try_files /wp-content/blogs.dir/$blogid/$uri /wp-includes/ms-files.php?file=$1 ;
  28. access_log off; log_not_found off; expires max;
  29. }
  30.  
  31. #WPMU x-sendfile to avoid php readfile()
  32. location ^~ /blogs.dir {
  33. internal;
  34. alias /var/www/shouce.ren/htdocs/wp-content/blogs.dir;
  35. access_log off; log_not_found off; expires max;
  36. }
  37.  
  38. #此处可以继续添加伪静态规则
  39. }

备注

  • “map”部分可以应用于小站点。大站点的多站点应用可以使用 nginx-helper wordpress插件 。
  • 如果想进一步优化wordpress的性能可以使用Nginx的fastcgi_cache,当使用fastcgi_cache配置需要在编译nginx时加上ngx_cache_purge模块以及使用wordpress的缓存插件等等

Tags:wordpress   nginx   站点   rewrite   重写

很赞哦! ()

文章评论

本站推荐

站点信息

  • 建站时间:2018-10-24
  • 网站程序:帝国CMS7.5
  • 主题模板《今夕何夕》
  • 文章统计1172篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们