您的位置:首页 > 运维架构 > Nginx

Nginx 虚拟主机 VirtualHost 配置

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

绝大多数的 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. 打开配置文件, 添加服务如下:

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';

server {

listen 80;

server_name demo.neoease.com;

index index.html index.htm index.php;

root /var/www/demo_neoease_com;

access_log /var/log/demo.neoease.com.log demo.neoease.com;

}

上面log_format中参数的相关说明解释(log_format一定要放在server层外,否则会报异常):

1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;

http_x_forwarded_for是走代理的时候代理服务会加上的。remote_addr是远程的主机,也不一定是原始的http_client_ip

2.$remote_user :用来记录客户端用户名称;

3.$time_local : 用来记录访问时间与时区;

4.$request : 用来记录请求的url与http协议;

5.$status 与 $upstream_status: 响应的状态码(用来记录请求状态;成功是200)

6.$bytes_sent 与 $body_bytes_sent :记录发送给客户端文件主体内容大小(含HTTP头和不含HTTP头);

7.$http_referer :用来记录从那个页面链接访问过来的;

8.$http_user_agent :记录客户端浏览器的相关信息;

9.$host:访问的具体域名

10.$connection:

11.$msec:

12.$pipe:

13.$request_length:

14.$request_time 与 $upstream_response_time:请求时间和后台处理时间

15.$time_iso8601:

16.$upstream_addr:后端服务的IP地址

3. 打开 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf, 在 http 范围引入虚拟主机配置文件如下:

include vhost/*.conf; (这一行我习惯放置在末尾)

4. 重启 Nginx 服务, 执行以下语句.

/usr/local/nginx/sbin/nginx -s reload

这里说下其它的启停等命令

启动:/usr/local/nginx/sbin/nginx

停止:/usr/local/nginx/sbin/nginx -s stop

不停服务情况下重新加载:kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

检查配置文件是否正确:/usr/local/nginx/sbin/nginx -t

让 Nginx 虚拟主机支持 PHP

在前面第 2 步的虚拟主机服务对应的目录加入对 PHP 的支持, 这里使用的是 FastCGI, 修改如下.

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';

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;

}

access_log /var/log/demo.neoease.com.log demo.neoease.com;

}

图片防盗链

图片作为重要的耗流量大的静态资源, 可能网站主并不希望其他网站直接引用, Nginx 可以通过 referer 来防止外站盗链图片.

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';

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;

}

}

# 这里为js和css添加过期时间,一小时:1h、或者三十天:30d

location ~ .*\.(js|css)$ {

expires 1h;

}

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, Nignx 有更加强大的并发能力, 而因为他对进程管理耗用资源也比较少. 而 Apache 比 Nginx 有更多更成熟的可用模块, bug 也比较少. 卖主机的 IDC 选择 Nignx, 因为高并发允许他们创建更多虚拟主机空间更来钱; 淘宝也因此改造 Nignx (Tengine) 作为 CDN 服务器, 可承受更大压力.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  虚拟主机