在Nginx上部署WordPress网站


单独的一个网站部署,参考如下:

# PHP连接
upstream php {
        server unix:/tmp/php-cgi.socket;
        #server 127.0.0.1:9000;
}

server {
        ## 网站名称
        server_name www.ggdoc.cn;
        ## 网站代码位置
        root /var/www/wordpress;
        ## 默认首页文件
        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # 查询字符串交给index.php处理
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                #php.ini文件中应该设置 "cgi.fix_pathinfo = 0;" 
                include fastcgi_params;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
                #以下参数也可以包含在fastcgi_params文件中
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

如果WordPress放在其它网站下的根目录下,则需要添加以下设置

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

location ~ \.php$ {
        fastcgi_split_path_info ^(/wordpress)(/.*)$;
}

如果需要将其它域名重定向到主域名下,可以添加以下配置

server {
        server_name  _;
        return 301 $scheme://www.ggdoc.cn$request_uri;
}

关闭favicon.ico、robots.txt文件产生的Nginx日志

location = /favicon.ico {
    log_not_found off;
    access_log off;
}

location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}

禁止访问隐藏文件,以.开头的文件,例如:.htaccess、.htpasswd、 .DS_Store (Mac)

location ~ /\. {
    deny all;
}

禁止执行其它目录下的PHP文件,例如:uploads、files

location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}

多站点(子目录)配置如下:

map $uri $blogname{
    ~^(?P<blogpath>/[^/]+/)files/(.*)       $blogpath ;
}

map $blogname $blogid{
    default -999;

    #Ref: http://wordpress.org/extend/plugins/nginx-helper/
    #include /var/www/wordpress/wp-content/plugins/nginx-helper/map.conf ;
}

server {
    server_name example.com ;

    root /var/www/example.com/htdocs;
    index index.php;

    location ~ ^(/[^/]+)?/files/(.+) {
        try_files /wp-content/blogs.dir/$blogid/files/$2 /wp-includes/ms-files.php?file=$2 ;
        access_log off;     log_not_found off; expires max;
    }

    #avoid php readfile()
    location ^~ /blogs.dir {
        internal;
        alias /var/www/example.com/htdocs/wp-content/blogs.dir ;
        access_log off;     log_not_found off; expires max;
    }

    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^(/[^/]+)?(/.*\.php) $2 last;
    }

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

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass php;
    }

    #add some rules for static content expiry-headers here
}

多站点(子域名)配置如下:

map $http_host $blogid {
    default       -999;

    #Ref: http://wordpress.org/extend/plugins/nginx-helper/
    #include /var/www/wordpress/wp-content/plugins/nginx-helper/map.conf ;

}

server {
    server_name example.com *.example.com ;

    root /var/www/example.com/htdocs;
    index index.php;

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

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass php;
    }

    #WPMU Files
        location ~ ^/files/(.*)$ {
                try_files /wp-content/blogs.dir/$blogid/$uri /wp-includes/ms-files.php?file=$1 ;
                access_log off; log_not_found off;      expires max;
        }

    #WPMU x-sendfile to avoid php readfile()
    location ^~ /blogs.dir {
        internal;
        alias /var/www/example.com/htdocs/wp-content/blogs.dir;
        access_log off;     log_not_found off;      expires max;
    }

    #add some rules for static content expiry-headers here
}