nginx默认虚拟主机
-
默认虚拟主机配置
1.vim /usr/local/nginx/conf/nginx.conf 删除文件尾部如下内容server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } }
2.删除后,新增一行 include vhost/*.conf;
3.新建vhost目录 mkdir /usr/local/nginx/conf/vhost
4.进入vhost目录,新建默认虚拟主机文件并配置如下内容 vim default.confserver { listen 80 default_server; // 有这个标记的就是默认虚拟主机 server_name aaa.com; index index.html index.htm index.php; root /data/wwwroot/aaa; }
5 . 如上root对应站点aaa.com的文件主目录,新建站点aaa.com的文件主目录
mkdir -p /data/wwwroot/aaa
6 .在aaa.com站点内新建web文件,以便curl测试
echo "this is aaa.com site" > /data/wwwroot/default/index.html
7 . /usr/local/nginx/sbin/nginx -t 检查nginx配置文件语法是否有误
8 . /usr/local/nginx/sbin/nginx -s reload 重新加载nginx服务,即使加载的文件有问题,也不会对原正常运行的文件造成影响;如使用systemctl restart nginx命令,当新加入的文件有错误时,则可能会影响原正常的服务。
9 . curl测试默认主机站点[root@load-balancer ~]# curl localhost this is aaa.com [root@load-balancer ~]# curl -x127.0.0.1:80 bbb.com this is aaa.com
当你访问一个不存在的域名时,nginx会自动导入到默认虚机即aaa.com
nginx多虚机站点
- cd /usr/local/nginx/conf/vhost目录下
- 新建站点文件,如abc.conf
- 编辑abc.conf
server
{
server_name abc.com;
index index.html index.htm index.php;
root /data/wwwroot/abc;
}
4.新建站点abc.com主目录,并新建测试网页
mkdir /data/wwwroot/abc
echo "abc.com" > /data/wwwroot/abc/index.html
5 . curl测试
[root@load-balancer vhost]# curl -x127.0.0.1:80 abc.com
abc.com
[root@load-balancer vhost]# curl -x127.0.0.1:80 aaa.com
this is aaa.com
[root@load-balancer vhost]# curl -x127.0.0.1:80 cba.com
this is aaa.com
访问abc.com时,返回站点abc.com的值,访问aaa.com时,返回aaa.com的值,访问不存在的站点cba.com时,返回默认虚拟主机aaa.com的值
转载于:https://blog.51cto.com/fonphxion/2045993