欢迎光临
我们一直在努力

WordPress + Nginx 的 CONF 完美配置方法

本文介绍 WordPress + Nginx 的 CONF 完美配置方法 ,所有的 WordPress 版本通用。包括且不限于 WordPress 单博客站点、多博客+子目录、多博客+子域名等。以下配置文件在 Nginx 在 0.8 版本下测试通过。1、/etc/nginx/nginx.conf 主文件配置、关于性能等 Nginx 参数配置

# Generic startup file.

user {user} {group};

worker_processes 2;

error_log /var/log/nginx/error.log;

pid /var/run/nginx.pid;

# Keeps the logs free of messages about not being able to bind().

#daemon off;

events {

worker_connections 1024;

}

http {

# rewrite_log on;

include mime.types;

default_type application/octet-stream;

access_log /var/log/nginx/access.log;

sendfile on;

# tcp_nopush on;

keepalive_timeout 3;

# tcp_nodelay on;

# gzip on;

client_max_body_size 13m;

index index.php index.html index.htm;

# Upstream to abstract backend connection(s) for PHP.

upstream php {

server unix:/tmp/php-fpm.sock;

# server 127.0.0.1:9000;

}

include sites-enabled/*;   注:这里可插入 server {} 部分。或者再另起新文件、装入。

}

2、站点的 server 文件配置

# Redirect everything to the main site.

server {

server_name *.mysite.com;

root /var/www/mysite.com;

if ($http_host != “mysite.com”) {

rewrite ^ http://mysite.com$request_uri permanent;

}

#include global/restrictions.conf;

#// Additional rules go here.

#// Only include one of the files below.  (以下三选一)

# include global/wordpress.conf;  (单博客)

# include global/wordpress-ms-subdir.conf; (多博客、子目录)

# include global/wordpress-ms-subdomain.conf;  (多博客、子域名)

}

3、通用部份

# Global restrictions configuration file.

# Designed to be included in any server {} block.</p>

location = /favicon.ico {

log_not_found off;

access_log off;

}

location = /robots.txt {

allow all;

log_not_found off;

access_log off;

}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).

location ~ /\. {

deny all;

access_log off;

log_not_found off;

}

# Deny access to any files with a .php extension in the uploads directory

location ~* ^/wp-content/uploads/.*.php$ {

deny all;

access_log off;

log_not_found off;

}

# Deny access to any files with a .php extension in the uploads directory for multisite

location ~* /files/(.*).php$ {

deny all;

access_log off;

log_not_found off;

}

4、单博客

# WordPress single blog rules.

# Designed to be included in any server {} block.

# This order might seem weird – this is attempted to match last if rules below fail.

# http://wiki.nginx.org/HttpCoreModule

location / {

try_files $uri $uri/ /index.php?$args;

}

# Add trailing slash to */wp-admin requests.

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Directives to send expires headers and turn off 404 error logging.

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {

expires 24h;

log_not_found off;

}

# Uncomment one of the lines below for the appropriate caching plugin (if used).

#include global/wordpress-wp-super-cache.conf;

#include global/wordpress-w3-total-cache.conf;

# Pass all .php files onto a php-fpm/php-fcgi server.

location ~ \.php$ {

# Zero-day exploit defense.

# http://forum.nginx.org/read.php?2,88845,page=3

# Won’t work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.

# Comment the ‘try_files’ line out if you set up php-fpm/php-fcgi on another machine. And then cross your fingers that you won’t get hacked.

try_files $uri =404;

fastcgi_split_path_info ^(.+\.php)(/.+)$;

#NOTE: You should have “cgi.fix_pathinfo = 0;” in php.ini

include fastcgi_params;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

# fastcgi_intercept_errors on;

fastcgi_pass php;

}

5、多博客子目录、多博客子域名通用

# WordPress multisite subdirectory rules.

# Designed to be included in any server {} block.

# This order might seem weird – this is attempted to match last if rules below fail.

# http://wiki.nginx.org/HttpCoreModule

location / {

try_files $uri $uri/ /index.php?$args;

}

# Add trailing slash to */wp-admin requests.

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Directives to send expires headers and turn off 404 error logging.

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {

expires 24h;

log_not_found off;

}

# Pass uploaded files to wp-includes/ms-files.php.

rewrite /files/$ /index.php last;

# For multisite: Use a caching plugin that creates symlinks to the correct subdirectory structure to get some performance gains.

set $cachetest “$document_root/wp-content/ms-filemap/${host}${uri}”;

if ($uri ~ /$) {

set $cachetest “”;

}

if (-f $cachetest) {

# Rewrites the URI and stops rewrite processing so it doesn’t start over and attempt to pass it to the next rule.

rewrite ^ /wp-content/ms-filemap/${host}${uri} break;

}

if ($uri !~ wp-content/plugins) {

rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;

}

# Uncomment one of the lines below for the appropriate caching plugin (if used).

#include global/wordpress-ms-subdir-wp-super-cache.conf;

#include global/wordpress-ms-subdir-w3-total-cache.conf;

# Rewrite multisite ‘…/wp-.*’ and ‘…/*.php’.

if (!-e $request_filename) {

rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;

rewrite ^/[_0-9a-zA-Z-]+.*(/wp-admin/.*\.php)$ $1 last;

rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;

}

# Pass all .php files onto a php-fpm/php-fcgi server.

location ~ \.php$ {

# Zero-day exploit defense.

# http://forum.nginx.org/read.php?2,88845,page=3

# Won’t work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.

# Comment the ‘try_files’ line out if you set up php-fpm/php-fcgi on another machine. And then cross your fingers that you won’t get hacked.

try_files $uri =404;

fastcgi_split_path_info ^(.+\.php)(/.+)$;

#NOTE: You should have “cgi.fix_pathinfo = 0;” in php.ini

include fastcgi_params;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

# fastcgi_intercept_errors on;

fastcgi_pass php;

}

更详细的说明请参考 : http://codex.wordpress.org/Nginx

———————–分隔线—————————————————————–

WordPress 自定义 Permalink 小技巧:

WordPress Permalink 中删除 index.php 中方法:

a: Apache 服务器,照着 WordPress 官方提供的 .htaccess 文件上传就能解决问题

b:Nginx 服务器

以上的配置文件就不一定能解决问题,

对于单博客 WordPress :找到 location / 区块:

加入或修改为以下代码

try_files $uri $uri/ /index.php?q=$uri&$args;

对于多博客 WordPress :新增 location /blog/ 区块:

location /blog/ {
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}

最后别忘记了

nginx -s reload

赞(0)
版权归原作者所有,如有侵权请告知。达维营-前端网 » WordPress + Nginx 的 CONF 完美配置方法

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址