Linux10.3 Nginx默认虚拟主机

  编辑nginx配置文件

vim /usr/local/nginx/conf/nginx.conf

  删除server{}主机配置端,在最后增加

include vhost/*.conf
mkdir /usr/local/nginx/conf/vhost

  在vhost文件夹内,创建默认主机配置文件aaa.com.conf文件,编辑如下:

server
{
    listen 80 default_server;  // 有这个标记的就是默认虚拟主机
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;       //网站目录,不存在需要创建
}

  也可以将默认虚拟主机配置为

[root@localhost vhost]# cat default.conf
server {

        listen 80 default_server;
        deny all;
}

  在网站目录创建html文件

echo “This is a default site.”>/data/wwwroot/default/index.html

  检查nginx配置文件语法,及重新加载配置文件

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

  测试,默认虚拟主机,不管什么域名,都会指向这个站点

[root@localhost default]# curl localhost
echo “This is a default site.”
[root@localhost default]# curl -x127.0.0.1:80 123.com
echo “This is a default site.”
[root@localhost default]# curl -x127.0.0.1:80 aaa.com
echo “This is a default site.”
[root@localhost default]# curl -x127.0.0.1:80 www.baidu.com
echo “This is a default site.”

  默认访问时不加index.html也可以访问到该访问页面,如果加上index语句,默认则改变

server {

        listen 80;
        server_name www.1.com;
        root /data/wwwroot/www.1.com;
        index welcome.html
}
[root@localhost vhost]# curl -x127.0.0.1:80 www.1.com
welcome www.1.com
[root@localhost vhost]# curl -x127.0.0.1:80 www.1.com/index.html
www.1.com

  如果server_name 后面为 *.1.com ,任何1.com的三级域名都可以访问该主机。

  

  listen端口不同,server_name可以相同。案例,网站两台主机A B ,可以在A的80端口做负载均衡,A的8080为真实网站端口。

原文地址:https://www.cnblogs.com/chyuanliu/p/9043574.html