使用yum安装nginx,使用如果命令查看nginx文件。

[root@centos1 ~]# rpm -qc nginx

/etc/logrotate.d/nginx

/etc/nginx/conf.d/default.conf

/etc/nginx/conf.d/ssl.conf

/etc/nginx/conf.d/virtual.conf

/etc/nginx/fastcgi.conf

/etc/nginx/fastcgi.conf.default

/etc/nginx/fastcgi_params

/etc/nginx/fastcgi_params.default

/etc/nginx/koi-utf

/etc/nginx/koi-win

/etc/nginx/mime.types

/etc/nginx/mime.types.default

/etc/nginx/nginx.conf

/etc/nginx/nginx.conf.default

/etc/nginx/scgi_params

/etc/nginx/scgi_params.default

/etc/nginx/uwsgi_params

/etc/nginx/uwsgi_params.default

/etc/nginx/win-utf

/etc/sysconfig/nginx


[root@centos1 conf.d]# cd /etc/nginx/conf.d/

[root@centos1 conf.d]# ls

default.conf  ssl.conf  virtual.conf


default.conf中定义中心主机

server {

    listen       (IP1:)80;

    server_name  www.a.com;

    access_log  logs/a.access.log  main;

    root   /var/www/a.com/htdocs;

    index  index.html index.htm;

    }


配置virtual.conf,添加虚拟主机。


基于端口的虚拟主机:

server {

     listen 8080;

     server_name www.b.com;

     access_log logs/b.accesss.log main;

     index index.html;

     root /var/www/b.com/htdocs;

}

  


基于主机名的虚拟主机:

server {

     listen 80;

     server_name www.b.com;

     access_log logs/b.accesss.log main;

     index index.html;

     root /var/www/b.com/htdocs;

}

基于IP地址的虚拟主机:

server {

     listen (IP2:)80;

     server_name www.b.com;

     access_log logs/b.accesss.log main;

     index index.html;

     root /var/www/b.com/htdocs;

}


进行nginx语法检查:

[root@centos1 conf.d]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful


然后重启nginx服务:

[root@centos1]#service nginx restart


设置默认虚拟主机:

如果访问的是IP地址,或者是一个未定义的主机名,则默认是返回第一个虚拟主机。我们可以定义默认虚拟机,当发生如上两种情况时,都返回默认虚拟主机所定义的网页。

语法如下:
server {

   listen 80 default;

   server_name _;

   root /www/default;

   index index.html;

}