Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 不管是静态服务器仍是小网站, Nginx 表现更加出色, 做为 Apache 的补充和替代使用率愈来愈高.php
我在《Apache 虚拟主机 VirtualHost 配置》介绍了在不一样操做系统上使用 Apahce 虚拟主机的方法, 还有那么些朋友想知道 Nginx 虚拟主机配置方法, 本文做为补充也介绍如何 Nginx 上添加虚拟主机.html
绝大多数的 Nginx 运行在 Linux 机器上, 虽然有 Windows 移植版, 但我也没搭建过. 因此本文将以 Linux 为例讲解, 而 Mac OS 或其余 Unix like 机器上的操做应该是同样的.node
这里假设你们的 Nginx 服务器已经安装好, 不懂的请阅读各 Linux 发行版的官方文档或者 LNMP 的安装说明. 配置 Virtual host 步骤以下:nginx
1. 进入 /usr/local/nginx/conf/vhost 目录, 建立虚拟主机配置文件 demo.neoease.com.conf ({域名}.conf).apache
2. 打开配置文件, 添加服务以下:bash
server { listen 80; server_name demo.neoease.com; index index.html index.htm index.php; root /var/www/demo_neoease_com; log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_log /var/log/demo.neoease.com.log demo.neoease.com;} |
3. 打开 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf, 在 http
范围引入虚拟主机配置文件以下:服务器
include vhost/*.conf; |
4. 重启 Nginx 服务, 执行如下语句.并发
service nginx restart |
在前面第 2 步的虚拟主机服务对应的目录加入对 PHP 的支持, 这里使用的是 FastCGI, 修改以下.wordpress
server { listen 80; server_name demo.neoease.com; index index.html index.htm index.php; root /var/www/demo_neoease_com; location ~ .*\.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; } log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_log /var/log/demo.neoease.com.log demo.neoease.com;} |
图片做为重要的耗流量大的静态资源, 可能网站主并不但愿其余网站直接引用, Nginx 能够经过 referer 来防止外站盗链图片.高并发
server { listen 80; server_name demo.neoease.com; index index.html index.htm index.php; root /var/www/demo_neoease_com; # 这里为图片添加为期 1 年的过时时间, 而且禁止 Google, 百度和本站以外的网站引用图片 location ~ .*\.(ico|jpg|jpeg|png|gif)$ { expires 1y; valid_referers none blocked demo.neoease.com *.google.com *.baidu.com; if ($invalid_referer) { return 404; } } log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_log /var/log/demo.neoease.com.log demo.neoease.com;} |
若是将 WordPress 的连接结构设定为 /%postname%/
, /%postname%.html
等格式时, 须要 rewrite URL, WordPress 提供 Apache 的 .htaccess 修改建议, 但没告知 Nginx 该如何修改. 咱们能够将 WordPress 的虚拟主机配置修改以下:
server { listen 80; server_name demo.neoease.com; index index.html index.htm index.php; root /var/www/demo_neoease_com; location / { if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; } } rewrite /wp-admin$ $scheme://$host$uri/ permanent; location ~ .*\.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; } log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_log /var/log/demo.neoease.com.log demo.neoease.com;} |
LNMP 套件在提供了 WordPress 为静态配置文件 /usr/local/nginx/conf/wordpress.conf, 在虚拟主机配置的 server 范围引用以下便可.
include wordpress.conf; |
若是你使用 LNMP 套件, 进入 WordPress 后台发现会出现 404 页面, wp-admin 后面缺乏了斜杆 /
, 请在 wordpress.conf 最后添加如下语句:
rewrite /wp-admin$ $scheme://$host$uri/ permanent; |
一直以来, 我主要在用 Apache, 自从去年从 MT 搬家到 Linode VPS 以后, 发现服务器压力很大, 每隔几天就要宕机一次, 在胡戈戈的协助下转成了 Nginx, 大半年了一直很稳定.
相对 Apache, Nignx 有更增强大的并发能力, 而由于他对进程管理耗用资源也比较少. 而 Apache 比 Nginx 有更多更成熟的可用模块, bug 也比较少. 卖主机的 IDC 选择 Nignx, 由于高并发容许他们建立更多虚拟主机空间更来钱; 淘宝也所以改造 Nignx (Tengine) 做为 CDN 服务器, 可承受更大压力.