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


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

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

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

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

wordpress多站二级域名重写规则

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

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

备注

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