Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 无论是静态服务器还是小网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高.
本文介绍如何 Nginx 上添加虚拟主机.
绝大多数的 Nginx 运行在 Linux 机器上, 虽然有 Windows 移植版, 但我也没搭建过. 所以本文将以 Linux 为例讲解, 而 Mac OS 或其他 Unix like 机器上的操作应该是一样的.
增加 Nginx 虚拟主机
这里假设大家的 Nginx 服务器已经安装好, 不懂的请阅读各 Linux 发行版的官方文档或者 LNMP 的安装说明. 配置 Virtual host 步骤如下:
1. 进入 /usr/local/nginx/conf/vhost 目录, 创建虚拟主机配置文件 demo.neoease.com.conf ({域名}.conf).
2. 打开配置文件, 添加服务如下:
server { listen 80; server_name demo.neoease.com; index index.html index.htm index.php; root /var/www/demo_neoease_com; |
3. 打开 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf, 在 http
范围引入虚拟主机配置文件如下:
include vhost/*.conf;
|
4. 重启 Nginx 服务, 执行以下语句.
service nginx restart |
让 Nginx 虚拟主机支持 PHP
在前面第 2 步的虚拟主机服务对应的目录加入对 PHP 的支持, 这里使用的是 FastCGI, 修改如下.
server { listen 80; server_name demo.neoease.com; index index.html index.htm index.php; root /var/www/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; |
WordPress 伪静态配置
如果将 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; |
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 服务器, 可承受更大压力.