介绍
WordPress是当今互联网上最受欢迎的CMS(内容管理系统). 单词 新闻站点可以使用Apache或NGINX等HTTP服务器提供服务,而Apache是服务网站的绝佳选择,许多站点由于具有可扩展的事件驱动架构,资源少和更好的发送静态文件而迁移到NGINX. 在此教程中, 您将学习如何为各种类型的 WordPress 设置配置 NGINX , 包括多站点配置、 重写规则以及 .conf 文件用于应用重复配置 .
要求
在本指南中,您将需要sudo 来安装和编辑文件,我认为您已经通过了初始服务器设置。
您可以按照这些指南在Ubuntu上安装LEMP(https://www.digitalocean.com/community/articles/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04)或 Debian。
请注意,我们的服务器块将是不同的,在本教程中,我们正在使用PHP-FPM使用UNIX接口。
基本的NGINX优化
调整 NGINX 工人流程和连接
通常建议将NGINX工人的数量设置为处理器的数量,您可以使用:
1cat /proc/cpuinfo | grep processor
打开主 NGINX 配置文件:
1sudo nano /etc/nginx/nginx.conf
根据您的系统规格,增加或减少员工数量:
1worker_processes 1;
NGINX限制一个员工可以同时保持的连接数量,如果您的网站有许多访问者,您可能希望增加连接的限制。
1worker_connections 768;
启用 Gzip
文件可以使用Gzip压缩以加速WordPress,用户请求的数据大小越小,响应速度越快. 想想CSS文件和HTML文件,它们有许多相似的字符串,重复的文本和白色空间。 Gzip使用一个名为DEFLATE的算法,通过链接到相同字符串的以前位置来删除重复的字符串,并创建一个更小的文件。
1gzip on;
2gzip_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多站点的特殊配置。
我们将在名为全球
的目录中创建所有文件,但首先我们需要创建上述目录:
1sudo mkdir /etc/nginx/global
我将设置 /etc/nginx/global 作为当前目录,只是为了使事情变得更容易。
1cd /etc/nginx/global
.conf 文件
让我们创建我们的第一个.conf 文件,适用于任何类型的网站。
1sudo nano common.conf
这将打开一个空的文件,复制以下配置:
1# Global configuration file.
2# ESSENTIAL : Configure Nginx Listening Port
3listen 80;
4# ESSENTIAL : Default file to serve. If the first file isn't found,
5index index.php index.html index.htm;
6# ESSENTIAL : no favicon logs
7location = /favicon.ico {
8 log_not_found off;
9 access_log off;
10}
11# ESSENTIAL : robots.txt
12location = /robots.txt {
13 allow all;
14 log_not_found off;
15 access_log off;
16}
17# ESSENTIAL : Configure 404 Pages
18error_page 404 /404.html;
19# ESSENTIAL : Configure 50x Pages
20error_page 500 502 503 504 /50x.html;
21 location = /50x.html {
22 root /usr/share/nginx/www;
23 }
24# SECURITY : Deny all attempts to access hidden files .abcde
25location ~ /\. {
26 deny all;
27}
28# PERFORMANCE : Set expires headers for static files and turn off logging.
29location ~* ^.+\.(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)$ {
30 access_log off; log_not_found off; expires 30d;
31}
「Listen 80;」指示服务器的收听端口。
「index.php...」指定了服务的默认文件(WordPress index.php)。如果第一个文件没有找到,第二个将被使用,等等. 你可能有HTML网站,这就是为什么我们包括index.html & index.htm;。
location = /robots.txt {allow all;}
允许访问 robots.txt,如果您想为 robots.txt 指定另一个目录,您可以添加一个名称:
1location /robots.txt {
2 alias /var/www/example.com/public/sample_robots.txt;
3}
在 Linux 操作系统中,一个隐藏的文件始于一个 ".",某些隐藏的文件,如.htaccess,应该因安全原因而被阻止。
「位置 ~* ^.+.(jşcsşswf...` 期限到期,标题告诉浏览器是否应该从服务器中请求特定文件,或者是否应该从浏览器的缓存中获取它。
保存和退出。
wordpress.conf 文件
让我们创建一个适用于所有WordPress网站的.conf 文件:
1sudo nano wordpress.conf
这将打开一个空的文件,复制以下配置:
1# WORDPRESS : Rewrite rules, sends everything through index.php and keeps the appended query string intact
2location / {
3 try_files $uri $uri/ /index.php?q=$uri&$args;
4}
5
6# SECURITY : Deny all attempts to access PHP Files in the uploads directory
7location ~* /(?:uploads|files)/.*\.php$ {
8 deny all;
9}
10# REQUIREMENTS : Enable PHP Support
11location ~ \.php$ {
12 # SECURITY : Zero day Exploit Protection
13 try_files $uri =404;
14 # ENABLE : Enable PHP, listen fpm sock
15 fastcgi_split_path_info ^(.+\.php)(/.+)$;
16 fastcgi_pass unix:/var/run/php5-fpm.sock;
17 fastcgi_index index.php;
18 include fastcgi_params;
19}
20# PLUGINS : Enable Rewrite Rules for Yoast SEO SiteMap
21rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
22rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
23
24#Yeah! you did it.
「try_files $uri $uri/ /index.php?q=$uri&$args」重写规则是需要的,以便您可以在WordPress上选择自定义的永久链接结构。
位置 ~* /(?:uploads Áthafiles)/.* \.php$ { deny all;}
这将防止恶意代码从WordPress媒体目录上传和执行。
由于WordPress是一个php网站,我们需要告诉NGINX如何将我们的php脚本传输到PHP5。
try_files $uri =404;
这是一个安全规则,你只想服务一个特定的php文件,或者去一个404错误。
更多规则:你可能想添加更多的NGINX规则,例如,如果你使用相同的WP插件,需要在所有安装上定制的规则,就像我一样,你可以添加更多的规则在这个.conf文件中,例如我使用Yoast SEO在我的所有网站上,因此我正在添加这里所需的重写规则,以这种方式,我不需要对每个服务器块复制相同的重写规则。
multisite.conf 文件
与单个网站的WordPress不同,它可以与丑陋
permalinks一起工作,因此不需要任何URL重写,MultiSite安装需要定制重写规则来格式化您的子网站的URL。
1sudo nano multisite.conf
这将打开一个空的文件,复制所需的重写规则:
1# Rewrite rules for WordPress Multi-site.
2if (!-e $request_filename) {
3rewrite /wp-admin$ $scheme://$host$uri/ permanent;
4rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
5rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
6}
保存 & 退出
小笔记
我们的当前工作目录是 /etc/nginx/global ,如果你想更改它,你可以输入:
1cd /desired_directory
创建服务器块
是时候创建我们的第一个服务器块了,因为我们已经在我们的.conf 文件中配置了一切,所以不需要重复默认服务器块文件。
1sudo rm /etc/nginx/sites-enabled/default
然后创建一个服务器封锁文件:
1sudo nano /etc/nginx/sites-available/demo
这将打开一个空的文件,根据您想要实现的目标来复制下列配置:
简单的WordPress安装
想象一下,你想配置一个WordPress网站使用这个域名www.demo.com。首先我们需要创建一个服务器块服务器 {...}
,在那里我们将把我们的规则。我们必须指定哪个服务器块用于给定的URL,包括 common.conf 和** wordpress.conf** ,最后我们会告诉NGINX在我们的服务器上安装WordPress的位置。
1server {
2 # URL: Correct way to redirect URL's
3 server_name demo.com;
4 rewrite ^/(.*)$ http://www.demo.com/$1 permanent;
5}
6server {
7 server_name www.demo.com;
8 root /home/demouser/sitedir;
9 access_log /var/log/nginx/www.demo.com.access.log;
10 error_log /var/log/nginx/www.demo.com.error.log;
11 include global/common.conf;
12 include global/wordpress.conf;
13}
请记住更改以下数据以满足您的需求:
server_name : 确定哪个服务器块用于给定的 URL。* root* : 您网站存储的路径.** 访问日志和错误日志* :为您的日志设置路径
你可以看到有两个服务器块,这是因为www.demo.com & demo.com是不同的URL。你可能想确保谷歌,Bing,用户......等等选择你想要的URL,在这种情况下我希望我的网站是www.demo.com,所以我已经配置了一个 永久重定向 从demo.com到www.demo.com。
1server {
2 # URL: Correct way to redirect URL's
3 server_name demo.com sub.demo.com example.com;
多站点与子目录
如果您想要一个包含子目录的多站点安装,则需要包含存储在 multisite.conf 中的重写规则:
1# URL: add a permanent redirect if required.
2server {
3 server_name www.demo1.com;
4 root /home/demouser/sitedir1;
5 access_log /var/log/nginx/www.demo1.com.access.log;
6 error_log /var/log/nginx/www.demo1.com.error.log;
7 include global/common.conf;
8 include global/wordpress.conf;
9 include global/multisite.conf;
10}
具有子域的多站点
如果您想要使用子域的多站点安装,则需要配置此服务器块以听取具有野卡的域名:
1server {
2 server_name *.demo2.com;
3 root /home/demouser/sitedir2;
4 access_log /var/log/nginx/demo2.com.access.log;
5 error_log /var/log/nginx/demo2.com.error.log;
6 include global/common.conf;
7 include global/wordpress.conf;
8}
HTML 和其他网站
如果您想托管简单的 HTML 网站或其他 Web 应用程序,您可能需要指定自定义规则或创建更多.conf 文件并将其包含在服务器块中:
1# URL: add a permanent redirect if required.
2server {
3 server_name www.demo3.com;
4 root /home/demouser/sitedir3;
5 access_log /var/log/nginx/demo3.com.access.log;
6 error_log /var/log/nginx/demo3.com.error.log;
7 # custom rules
8}
记住保存和退出。
允许服务器封锁文件
最后一步是通过创建网站可用目录和网站可用目录之间的象征链接来激活主机:
1sudo ln -s /etc/nginx/sites-available/demo /etc/nginx/sites-enabled/demo
我们对配置进行了很多更改,重新加载 NGINX 并使更改可见。
1sudo service nginx reload;
最后的笔记
要创建额外的虚拟主机,你可以简单地重复上面的过程,小心每次设置一个新的文档根与相应的新域名。
1server {
2 server_name demo.com;
3 rewrite ^/(.*)$ http://www.demo.com/$1 permanent;
4}
5server {
6 server_name www.demo.com;
7 root /home/demouser/sitedir;
8 access_log /var/log/nginx/www.demo.com.access.log;
9 error_log /var/log/nginx/www.demo.com.error.log;
10 include global/common.conf;
11 include global/wordpress.conf;
12}
13
14server {
15 server_name www.demo1.com;
16 root /home/demouser/sitedir1;
17 access_log /var/log/nginx/www.demo1.com.access.log;
18 error_log /var/log/nginx/www.demo1.com.error.log;
19 include global/common.conf;
20 include global/wordpress.conf;
21 include global/multisite.conf;
22}
23# More server blocks....