Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 无论是静态服务器还是小网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高.
增加 Nginx 虚拟主机
这里假设大家的 Nginx 服务器已经安装好.我们可以参照apache的关于虚拟主机的配置,直接在主配置文件中引用虚拟主机配置文件,而虚拟主机的配置文件另外存放到特定的虚拟主机存放目录;
1.我们先创建网站资源存放目录;
1
2
3
4
5
[iyunv@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.创建虚拟主机配置文件存放目录;
1
[iyunv@www /]# mkdir /etc/nginx/extra
3.开始配置nginx.conf 主配置文件;
1
2
3
4
[iyunv@www /]# vim /etc/nginx/nginx.conf
注释掉http{}段中的预先定义的server{}段的所有内容;
在下面添加一条:
include extra/nginx-vhost.conf;
如图:
4.现在我们就直接去虚拟主机配置文件目录下创建虚拟主机配置文件;
1
2
3
4
5
6
7
8
9
10
11
[iyunv@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.检查语法;
1
2
3
4
[iyunv@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.放入测试页面;
1
[iyunv@www ~]# echo "172.16.31.40 www.stu31.com " > /www/vhosts/www1/index.html
7.重启nginx服务;
1
2
3
4
5
6
7
[iyunv@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来测试网站访问;
9.查看日志;
1
2
3
4
5
6
7
8
9
10
[iyunv@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; -) -
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com