1. 编辑nginx.conf配置文件:
[root@hao-01 conf]# vim /usr/local/nginx/conf/nginx.conf删掉黄框中内容:wKiom1mMDpWwVxU6AAB2T9FXa1k194.png
spacer.gif写入一行内容:
include vhost/*.conf;wKiom1mMDq2QN4t8AAAoGn8MF3Y148.png
spacer.gif2. 在当前目录下,创建vhost目录:
[root@hao-01 conf]# mkdir /usr/local/nginx/conf/vhost
3. 进入…vhost目录下:
[root@hao-01 conf]# cd /usr/local/nginx/conf/vhost 在当前…vhost目录下,创建一个aaa.com.conf文件,并写入内容:[root@hao-01 vhost]# vim /usr/local/nginx/conf/vhost/aaa.com.conf
写入内容:
server
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
wKiom1mMDvjxmGpKAAAgXTjfIbs656.png
spacer.gif5. 创建default目录:
(如果根目录下,没有/data/wwwroot/目录,需自行创建即可)
[root@hao-01 vhost]# mkdir /data/wwwroot/default
进入…default目录下:[root@hao-01 vhost]# cd /data/wwwroot/default/
在当前…default目录下,创建一个index.html文件,并写入内容:[root@hao-01 default]# vim index.html
写入内容:
This is the default site.
检测nginx配置文件是否有错?[root@hao-01 default]# /usr/local/nginx/sbin/nginx -t
重新加载nginx配置文件(非重启!):[root@hao-01 default]# /usr/local/nginx/sbin/nginx -s reload
测试curl访问设定的默认虚拟主机主域名aaa.com:[root@hao-01 default]# curl -x127.0.0.1:80 aaa.comwKiom1mMD0Hy4KGFAAANJrEx0OI548.png
spacer.gif11. 测试curl访问非默认虚拟主机的主域名111.com:
(访问111.com,跳转到了默认虚拟主机的主域名aaa.com上)
[root@hao-01 default]# curl -x127.0.0.1:80 111.comwKiom1mMD1miWqCLAAAPABvOn_0700.png
spacer.gif
Nginx用户认证
网站主页面(根目录)访问受限(需用户认证)
进入…vhost目录下:[root@hao-01 ~]# cd /usr/local/nginx/conf/vhost/
在当前…vhost目录下,创建一个index.html文件,并写入内容:[root@hao-01 vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf
写入内容:
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /
{
auth_basic “Auth”;
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
yum安装的apache:直接用htpasswd命令,创建nginx用户名密码!(没有安装apache,用yum安装httpd也可以!yum install httpd)
(从创建第二个,不要-c参数,要不会把之前创建用户名的全部覆盖!!!)
[root@hao-01 vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd hao