Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 无论是静态服务器还是小网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高.
增加 Nginx 虚拟主机
这里假设大家的 Nginx 服务器已经安装好.我们可以参照apache的关于虚拟主机的配置,直接在主配置文件中引用虚拟主机配置文件,而虚拟主机的配置文件另外存放到特定的虚拟主机存放目录;
1.我们先创建网站资源存放目录;
[root@www /]# mkdir /www/vhosts/www{1,2} -pv mkdir: created directory `/www‘ mkdir: created directory `/www/vhosts‘ mkdir: created directory `/www/vhosts/www1‘ mkdir: created directory `/www/vhosts/www2‘
2.创建虚拟主机配置文件存放目录;
[root@www /]# mkdir /etc/nginx/extra
3.开始配置nginx.conf 主配置文件;
[root@www /]# vim /etc/nginx/nginx.conf 注释掉http{}段中的预先定义的server{}段的所有内容; 在下面添加一条: include extra/nginx-vhost.conf;
如图:
4.现在我们就直接去虚拟主机配置文件目录下创建虚拟主机配置文件;
[root@www ~]# vim /etc/nginx/extra/nginx-vhost.conf server { listen 80; server_name www.stu31.com; index index.html index.htm index.php; root /www/vhosts/www1; log_format www.stu31.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/nginx/www.stu31.com.log www.stu31.com; }
注意:这里的日志记录,需要在编译安装nginx时开启debug日志;编译安装时加入--with-debug参数;如果未加入参数,就会在检查语法时出现错误;
5.检查语法;
[root@www ~]# service nginx configtest nginx: [warn] the "log_format" directive may be used only on "http" level in /etc/nginx/extra/nginx-vhost.conf:9 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
我们这里先忽略错误哦!我在编译时未加入--with-debug参数,但是不影响日志的记录;
6.放入测试页面;
[root@www ~]# echo "172.16.31.40 www.stu31.com" > /www/vhosts/www1/index.html
7.重启nginx服务;
[root@www ~]# service nginx restart nginx: [warn] the "log_format" directive may be used only on "http" level in /etc/nginx/extra/nginx-vhost.conf:9 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful Stopping nginx: [ OK ] Starting nginx: nginx: [warn] the "log_format" directive may be used only on "http" level in /etc/nginx/extra/nginx-vhost.conf:9 [ OK ]
8.使用elinks来测试网站访问;
[root@www ~]# elinks -dump 172.16.31.40 172.16.31.40 www.stu31.com [root@www ~]# elinks -dump http://www.stu31.com 172.16.31.40 www.stu31.com
9.查看日志;
[root@www ~]# tail /var/log/nginx/www.stu31.com.log 172.16.31.254 - - [27/Dec/2014:10:10:52 +0800] GET / HTTP/1.1404 570 - Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36 - 172.16.31.254 - - [27/Dec/2014:10:10:52 +0800] GET /favicon.ico HTTP/1.1404 570 - Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36 - 172.16.31.254 - - [27/Dec/2014:10:10:59 +0800] GET /index.html HTTP/1.1404 570 - Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36 - 127.0.0.1 - - [27/Dec/2014:10:21:55 +0800] GET / HTTP/1.1404 168 - ELinks/0.12pre5 (textmode; Linux; -) - 172.16.31.40 - - [27/Dec/2014:10:22:04 +0800] GET / HTTP/1.1404 168 - ELinks/0.12pre5 (textmode; Linux; -) - 172.16.31.40 - - [27/Dec/2014:10:22:36 +0800] GET / HTTP/1.1404 168 - ELinks/0.12pre5 (textmode; Linux; -) - 172.16.31.40 - - [27/Dec/2014:10:23:04 +0800] GET / HTTP/1.1200 27 - ELinks/0.12pre5 (textmode; Linux; -) - 127.0.0.1 - - [27/Dec/2014:10:23:14 +0800] GET / HTTP/1.1200 27 - ELinks/0.12pre5 (textmode; Linux; -) - 172.16.31.40 - - [27/Dec/2014:10:30:10 +0800] GET / HTTP/1.1200 27 - ELinks/0.12pre5 (textmode; Linux; -) -
本文出自 “龙之守护” 博客,请务必保留此出处http://sohudrgon.blog.51cto.com/3088108/1596655
原文地址:http://sohudrgon.blog.51cto.com/3088108/1596655