介绍
WordPress是当今互联网上使用的最流行的CMS(内容管理系统)。WordPress网站可以使用Apache或NGINX等HTTP服务器提供服务,而Apache是服务网站的绝佳选择,许多网站已经迁移到NGINX,因为它具有可扩展的事件驱动架构,低资源和更好的静态文件交付。在本教程中,您将学习如何为各种类型的WordPress安装配置NGINX,包括多站点配置,重写规则以及使用.conf文件应用重复配置。
要求
在本指南中,您将需要 sudo 来安装和编辑文件。我假设您已经完成了初始服务器设置。
您将需要安装MySQL,PHP和NGINX。您可以按照这些指南在 Ubuntu 或 Debian 上安装 LEMP。
请注意,我们的服务器块将是不同的,在本教程中,我们将使PHP-FPM使用UNIX套接字。
基本 NGINX 优化
调整NGINX工作进程和连接
通常建议将NGINX工作器的数量设置为等于处理器的数量,您可以使用以下方法确定处理器的数量:
cat /proc/cpuinfo | grep processor
打开主 NGINX 配置文件:
sudo nano /etc/nginx/nginx.conf
根据系统的规格增加或减少工作器数量:
worker_processes 1;
NGINX限制了工人一次可以保持的连接数量,如果您的网站有很多访问者,您可能希望增加连接限制。理论上,最大连接数 = 工作线程 * 限制。
worker_connections 768;
启用 Gzip
可以使用Gzip压缩文件以加速WordPress,用户请求的数据大小越小,响应越快。想想CSS文件和HTML文件,它们有许多相似的字符串,重复的文本和空格。Gzip 使用一种称为 DEFLATE 的算法,该算法通过链接到相同字符串的先前位置来删除重复的字符串,并创建一个更小的文件。找到 Gzip 部分并启用它:
gzip on;
gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
保存并退出。
创建 NGINX .conf 文件
由于您可能托管多个WordPress网站,因此我们将创建一些可以从服务器块加载的.conf文件,而不是在每个服务器块上多次编写相同的配置。
在接下来的步骤中,我们将创建 3 个文件来保存我们的配置:
- common.conf:适用于所有站点的配置。
- wordpress.conf:适用于所有WordPress网站的配置。
- multisite.conf:带有子目录的WordPress多站点的特殊配置。
我们将在名为“global”的目录中创建所有文件,但首先我们需要创建上述目录:
sudo mkdir /etc/nginx/global
我将设置 /etc/nginx/global 作为当前目录,只是为了让事情变得更容易。
cd /etc/nginx/global
通用.conf 文件
让我们创建适用于任何类型的网站的第一个 .conf 文件。
sudo nano common.conf
这将打开一个空文件,复制以下配置:
# Global configuration file.
# ESSENTIAL : Configure Nginx Listening Port
listen 80;
# ESSENTIAL : Default file to serve. If the first file isn't found,
index index.php index.html index.htm;
# ESSENTIAL : no favicon logs
location = /favicon.ico {
log_not_found off;
access_log off;
}
# ESSENTIAL : robots.txt
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# ESSENTIAL : Configure 404 Pages
error_page 404 /404.html;
# ESSENTIAL : Configure 50x Pages
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# SECURITY : Deny all attempts to access hidden files .abcde
location ~ /\. {
deny all;
}
# PERFORMANCE : Set expires headers for static files and turn off logging.
location ~* ^.+\.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires 30d;
}
listen 80;
指定服务器的侦听端口。
index index.php...
指定要提供的默认文件(WordPress index.php)。如果未找到第一个文件,将使用第二个文件,依此类推。您可能有HTML网站,这就是为什么我们包括index.html和index.htm;。
location = /robots.txt {allow all;}
允许访问机器人.txt,如果要为机器人指定另一个目录.txt可以添加别名:
location /robots.txt {
alias /var/www/example.com/public/sample_robots.txt;
}
location ~ /\. {deny all;}
在 Linux 操作系统中,隐藏文件以“.”开头,出于安全原因,应阻止对某些隐藏文件(如 .htaccess)的访问。
location ~* ^.+\.(js|css|swf...
过期标头告诉浏览器是应该从服务器请求特定文件,还是应该从浏览器的缓存中获取该文件。随着过期 30d,我们告诉浏览器将静态文件(如图片)存储 30 天。
保存并退出。
wordpress.conf 文件
让我们创建一个适用于所有WordPress网站的.conf文件:
sudo nano wordpress.conf
这将打开一个空文件,复制以下配置:
# WORDPRESS : Rewrite rules, sends everything through index.php and keeps the appended query string intact
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
# SECURITY : Deny all attempts to access PHP Files in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
# REQUIREMENTS : Enable PHP Support
location ~ \.php$ {
# SECURITY : Zero day Exploit Protection
try_files $uri =404;
# ENABLE : Enable PHP, listen fpm sock
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# PLUGINS : Enable Rewrite Rules for Yoast SEO SiteMap
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
#Yeah! you did it.
try_files $uri $uri/ /index.php?q=$uri&$args
需要重写规则才能允许您在WordPress上选择自定义永久链接结构。
location ~* /(?:uploads|files)/.*\.php$ {deny all;}
这将防止恶意代码从WordPress媒体目录上传和执行。
location ~ \.php$ {...}
由于WordPress是一个php网站,我们需要告诉NGINX如何将我们的php脚本传递给PHP5。
try_files $uri =404;
这是一个安全规则,您只想提供确定的 PHP 文件或转到 404 错误。
更多规则:您可能希望添加更多NGINX规则,例如,如果您使用与我一样在所有安装上都需要自定义规则的相同WP插件,则可以在此.conf文件中添加更多规则,例如,我在所有网站上都使用Yoast SEO,因此我在此处添加所需的重写规则, 这样,我就不必为每个服务器块复制相同的重写规则。
多站点 conf 文件
与单站点WordPress不同,它可以处理“丑陋”的永久链接,因此不需要任何URL重写,多站点安装需要自定义重写规则来格式化子网站的URL。让我们创建一个适用于多站点WordPress安装的.conf文件:
sudo nano multisite.conf
这将打开一个空文件,复制所需的重写规则:
# Rewrite rules for WordPress Multi-site.
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
保存并退出。
小纸条
我们当前的工作目录是 /etc/nginx/global,如果你想改变它,你可以输入:
cd /desired_directory
创建服务器块
是时候创建我们的第一个服务器块了。由于我们已经在 .conf 文件中配置了所有内容,因此无需复制默认的服务器块文件。让我们禁用默认服务器块:
sudo rm /etc/nginx/sites-enabled/default
并创建一个服务器块文件:
sudo nano /etc/nginx/sites-available/demo
这将打开一个空文件,根据您要实现的目标复制以下配置:
简单的WordPress安装
假设您想使用此域 www.demo.com 配置WordPress站点。首先,我们必须创建一个服务器块,我们将在其中放置规则。我们必须指定哪个服务器块用于给定的URL,包括common.conf和wordpress.conf,最后我们将告诉NGINX在我们的服务器中安装WordPress的位置。server {...}
server {
# URL: Correct way to redirect URL's
server_name demo.com;
rewrite ^/(.*)$ http://www.demo.com/$1 permanent;
}
server {
server_name www.demo.com;
root /home/demouser/sitedir;
access_log /var/log/nginx/www.demo.com.access.log;
error_log /var/log/nginx/www.demo.com.error.log;
include global/common.conf;
include global/wordpress.conf;
}
请记住更改以下数据以满足您的需求:
- server_name:确定用于给定 URL 的服务器块。
- root:存储站点的路径。
- 访问日志和错误日志:设置日志的路径
你可以看到有两个服务器块,这是因为 www.demo.com 和 demo.com 是不同的URL。您可能希望确保Google,Bing,用户...等选择您想要的 URL,在这种情况下,我希望我的网站 www.demo.com 所以我配置了从 demo.com 到 www.demo.com 的永久重定向。也可以指定多个域:
server {
# URL: Correct way to redirect URL's
server_name demo.com sub.demo.com example.com;
具有子目录的多站点
如果你想要一个带有子目录的多站点安装,你需要包括存储在multisite.conf中的重写规则:
# URL: add a permanent redirect if required.
server {
server_name www.demo1.com;
root /home/demouser/sitedir1;
access_log /var/log/nginx/www.demo1.com.access.log;
error_log /var/log/nginx/www.demo1.com.error.log;
include global/common.conf;
include global/wordpress.conf;
include global/multisite.conf;
}
具有子域的多站点
如果要使用子域进行多站点安装,则需要配置此服务器块以侦听带有通配符的域:
server {
server_name *.demo2.com;
root /home/demouser/sitedir2;
access_log /var/log/nginx/demo2.com.access.log;
error_log /var/log/nginx/demo2.com.error.log;
include global/common.conf;
include global/wordpress.conf;
}
HTML 和其他网站
如果要托管简单的 html 网站或其他 Web 应用程序,则可能需要指定自定义规则或创建更多 .conf 文件并将它们包含在服务器块中:
# URL: add a permanent redirect if required.
server {
server_name www.demo3.com;
root /home/demouser/sitedir3;
access_log /var/log/nginx/demo3.com.access.log;
error_log /var/log/nginx/demo3.com.error.log;
# custom rules
}
记得保存并退出。
启用服务器块文件
最后一步是通过在站点可用目录和启用站点的目录之间创建符号链接来激活主机:
sudo ln -s /etc/nginx/sites-available/demo /etc/nginx/sites-enabled/demo
™我们对配置进行了大量更改。重新加载NGINX并使更改可见。
sudo service nginx reload;
结语
要创建其他虚拟主机,您可以重复上述过程,每次都要小心地使用适当的新域名设置新的文档根目录。也可以在一个文件中组合多个服务器块:
server {
server_name demo.com;
rewrite ^/(.*)$ http://www.demo.com/$1 permanent;
}
server {
server_name www.demo.com;
root /home/demouser/sitedir;
access_log /var/log/nginx/www.demo.com.access.log;
error_log /var/log/nginx/www.demo.com.error.log;
include global/common.conf;
include global/wordpress.conf;
}
server {
server_name www.demo1.com;
root /home/demouser/sitedir1;
access_log /var/log/nginx/www.demo1.com.access.log;
error_log /var/log/nginx/www.demo1.com.error.log;
include global/common.conf;
include global/wordpress.conf;
include global/multisite.conf;
}
# More server blocks....