六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 597|回复: 0

生产环境下的nginx.conf配置文件(多虚拟主机)

[复制链接]
 楼主| 发表于 2013-11-15 13:17:33 | 显示全部楼层 |阅读模式
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.agoit.com.conf ({域名}.conf).
2. 打开配置文件, 添加服务如下:
  1. server {
  2.         listen       80;
  3.         server_name demo.agoit.com;
  4.         index index.html index.htm index.php;
  5.         root  /var/www/demo_neoease_com;

  6.         log_format demo.agoit.com '$remote_addr - $remote_user [$time_local] $request'
  7.         '$status $body_bytes_sent $http_referer '
  8.         '$http_user_agent $http_x_forwarded_for';
  9.         access_log  /var/log/demo.agoit.com.log demo.agoit.com;
  10. }
复制代码
3. 打开 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf, 在 http 范围引入虚拟主机配置文件如下:
  1. include vhost/*.conf;
复制代码
4. 重启 Nginx 服务, 执行以下语句.
  1. service nginx restart
复制代码
让 Nginx 虚拟主机支持 PHP在前面第 2 步的虚拟主机服务对应的目录加入对 PHP 的支持, 这里使用的是 FastCGI, 修改如下.
  1. server {
  2.         listen       80;
  3.         server_name demo.agoit.com;
  4.         index index.html index.htm index.php;
  5.         root  /var/www/demo_neoease_com;

  6.         location ~ .*\.(php|php5)?$ {
  7.                 fastcgi_pass unix:/tmp/php-cgi.sock;
  8.                 fastcgi_index index.php;
  9.                 include fcgi.conf;
  10.         }

  11.         log_format demo.agoit.com '$remote_addr - $remote_user [$time_local] $request'
  12.         '$status $body_bytes_sent $http_referer '
  13.         '$http_user_agent $http_x_forwarded_for';
  14.         access_log  /var/log/demo.agoit.com.log demo.agoit.com;
  15. }
复制代码
图片防盗链图片作为重要的耗流量大的静态资源, 可能网站主并不希望其他网站直接引用, Nginx 可以通过 referer 来防止外站盗链图片.
  1. server {
  2.         listen       80;
  3.         server_name demo.agoit.com;
  4.         index index.html index.htm index.php;
  5.         root  /var/www/demo_neoease_com;

  6.         # 这里为图片添加为期 1 年的过期时间, 并且禁止 Google, 百度和本站之外的网站引用图片
  7.         location ~ .*\.(ico|jpg|jpeg|png|gif)$ {
  8.                 expires 1y;
  9.                 valid_referers none blocked demo.agoit.com *.google.com *.baidu.com;
  10.                 if ($invalid_referer) {
  11.                         return 404;
  12.                 }
  13.         }

  14.         log_format demo.agoit.com '$remote_addr - $remote_user [$time_local] $request'
  15.         '$status $body_bytes_sent $http_referer '
  16.         '$http_user_agent $http_x_forwarded_for';
  17.         access_log  /var/log/demo.agoit.com.log demo.agoit.com;
  18. }
复制代码
WordPress 伪静态配置如果将 WordPress 的链接结构设定为 /%postname%/, /%postname%.html 等格式时, 需要 rewrite URL, WordPress 提供 Apache 的 .htaccess 修改建议, 但没告知 Nginx 该如何修改. 我们可以将 WordPress 的虚拟主机配置修改如下:
  1. server {
  2.         listen       80;
  3.         server_name demo.agoit.com;
  4.         index index.html index.htm index.php;
  5.         root  /var/www/demo_neoease_com;

  6.         location / {
  7.                 if (-f $request_filename/index.html){
  8.                         rewrite (.*) $1/index.html break;
  9.                 }
  10.                 if (-f $request_filename/index.php){
  11.                         rewrite (.*) $1/index.php;
  12.                 }
  13.                 if (!-f $request_filename){
  14.                         rewrite (.*) /index.php;
  15.                 }
  16.         }
  17.         rewrite /wp-admin$ $scheme://$host$uri/ permanent;

  18.         location ~ .*\.(php|php5)?$ {
  19.                 fastcgi_pass unix:/tmp/php-cgi.sock;
  20.                 fastcgi_index index.php;
  21.                 include fcgi.conf;
  22.         }

  23.         log_format demo.agoit.com '$remote_addr - $remote_user [$time_local] $request'
  24.         '$status $body_bytes_sent $http_referer '
  25.         '$http_user_agent $http_x_forwarded_for';
  26.         access_log  /var/log/demo.agoit.com.log demo.agoit.com;
  27. }
复制代码
LNMP 套件在提供了 WordPress 为静态配置文件 /usr/local/nginx/conf/wordpress.conf, 在虚拟主机配置的 server 范围引用如下即可.
  1. include wordpress.conf;
复制代码
如果你使用 LNMP 套件, 进入 WordPress 后台发现会出现 404 页面, wp-admin 后面缺少了斜杆 /, 请在 wordpress.conf 最后添加以下语句:
  1. rewrite /wp-admin$ $scheme://$host$uri/ permanent;
复制代码
后话一直以来, 我主要在用 Apache, 自从去年从 MT 搬家到 Linode VPS 之后, 发现服务器压力很大, 每隔几天就要宕机一次, 在胡戈戈的协助下转成了 Nginx, 大半年了一直很稳定.
相对 Apache, Nignx 有更加强大的并发能力, 而因为他对进程管理耗用资源也比较少. 而 Apache 比 Nginx 有更多更成熟的可用模块, bug 也比较少. 卖主机的 IDC 选择 Nignx, 因为高并发允许他们创建更多虚拟主机空间更来钱; 淘宝也因此改造 Nignx (Tengine) 作为 CDN 服务器, 可承受更大压力.


本文摘自:http://www.neoease.com/nginx-virtual-host/





该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表