Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 无论是静态服务器还是小网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高.

我在《Apache 虚拟主机 VirtualHost 配置》介绍了在不同操作系统上使用 Apahce 虚拟主机的方法, 还有那么些朋友想知道 Nginx 虚拟主机配置方法, 本文作为补充也介绍如何 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;
 
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

让 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;
 
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 伪静态配置

如果将 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 服务器, 可承受更大压力.

Linux配置nignx虚拟主机的更多相关文章

  1. Linux下Apache虚拟主机配置

    Linux下Apache虚拟主机的三种配置.这样可以实现一台主机架构多个独立域名网站.其中基于域名的最为常见.性价比也最高.下面PHP程序员雷雪松详细的讲解下Linux下Apache虚拟主机配置的具体 ...

  2. Linux centosVMware 配置Tomcat监听80端口、配置Tomcat虚拟主机、Tomcat日志

    一.配置Tomcat监听80端口 关闭tomcat报错 [root@davery src]# /usr/local/tomcat/bin/shutdown.sh 重装tomcat即可 vim /usr ...

  3. PHP中级篇 Apache配置httpd-vhosts虚拟主机总结及注意事项[OK]

    经常使用Apache虚拟主机进行开发和测试,但每次需要配置虚拟主机时都习惯性的ctrl+c和ctrl+v,这次由于重装系统,需要配置一个新的PHP开发环境虚拟主机,于是总结一下Apaceh配置http ...

  4. WIN10 vagrant和virtualbox虚拟机和一键搭建lnmp环境配置thinkphp虚拟主机

    版本:win10系统 virtualbox:5.1.26 vagrant :1.9.7 centos 7.0 xshell/git 首先下载好对应版本的软件 配置vagrant和virtualbox ...

  5. 细说Linux下的虚拟主机那些事儿

    细说Linux下的虚拟主机那些事儿 我们知道Linux操作系统是目前在服务器上应用广泛的操作系统.在Linux操作系统下的虚拟主机是不是就是我们常说的Linux虚拟主机呢?其实从专业方面说并不是,它是 ...

  6. Centos7 nginx配置多虚拟主机过程

    一.前提准备 1.已经安装好了的Centos7服务器 2.ip 为192.168.1.209   [本次的配置ip] 3.确定防火墙等已经关闭 二.nignx配置文件参数详解 要配置多台虚拟主机,就需 ...

  7. PHP中级篇 Apache配置httpd-vhosts虚拟主机总结及注意事项

    经常使用Apache虚拟主机进行开发和测试,但每次需要配置虚拟主机时都习惯性的ctrl+c和ctrl+v,这次由于重装系统,需要配置一个新的PHP开发环境虚拟主机,于是总结一下Apaceh配置http ...

  8. apache 多端口配置和虚拟主机配置

    1 打开httpd.conf文件 2 添加端口监听 (找到Lisen 80 在后面添加 Listen 端口号 如Listen 1112) port =>你的端口 project_name=> ...

  9. 配置Apache虚拟主机

    实验环境 一台最小化安装的CentOS 7.3虚拟机 配置基础环境 1. 安装apache yum install -y httpd 2. 建立虚拟主机的根目录 mkdir /var/wwwroot ...

随机推荐

  1. jQuery(基础dom及css操作)

    设置元素内容 元素属性操作 ---------- 元素样式操作 ---------------- 对象数组的遍历 测试代码: $(function () { var v=$('div').css([' ...

  2. ZOJ 2315 New Year Bonus Grant

    New Year Bonus Grant Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Or ...

  3. 关于参数net_buffer_length How MySQL Uses Memory

    http://dev.mysql.com/doc/refman/5.6/en/memory-use.html The following list indicates some of the ways ...

  4. APS.NET webform中的isPostBack

    IsPostBack介绍Page.IsPostBack是一个标志:当前请求是否第一次打开. 调用方法为:Page.IsPostBack或者IsPostBack或者this.IsPostBack或者th ...

  5. Hadoop集群(第11期)_常用MySQL数据库命令

    1.系统管理 1.1 连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 举例: 例1:连接到本机上的MySQL. 首先在打开DOS窗口,然后进入目录 mysqlbin,再键入 ...

  6. [Windows Server]安装系统显示“缺少计算机所需的介质驱动程序”解决方案

    1.把电脑上插着的硬盘拔了 2.重试 3.修复计算机找到dos命令行 4.然后进入我们放置解压了的系统的那个符盘,(我这里放在D盘)输入:d:       找到刚才我们解压了的系统文件,进入sourc ...

  7. 升级后开机就提示“android.process.acore”停止执行 --分析 解决方式

    OTA升级的,升级引发的全部问题都是能够解释的,有的能解决,有的不能解决. 一个项目报了这个问题. 升级后开机就提示"android.process.acore"停止执行 抓取 a ...

  8. js面向对象编程: js类定义函数时prototype和this差别?

    在面向对象编写js脚本时,定义实例方法主要有两种 例如以下: function ListCommon2(afirst) { var first=afirst; this.do1=function () ...

  9. mysql基础综述(四)

    1.数据库的简单介绍 1.1 数据库,就是一个文件系统,使用标准sql对数据库进行操作 1.2 常见的数据库 oracle  是oracle公司的数据库,是一个收费的大型的数据库 DB2,是IBM公司 ...

  10. 杂项-DB-分布式:HBase

    ylbtech-杂项-DB-分布式:HBase HBase是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系 ...