Nginx系列(六.nginx虚拟主机)

本文深入探讨了Nginx虚拟主机配置的实现方式,并详细解释了各个关键参数的作用与应用场景,包括域名映射、端口监听、根目录设置、访问与错误日志记录等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

假设四个域名需要部署

1.域名一
www.test01.com 80 /web/www/test01

2.域名二
www.test02.com 80 /web/www/test02

3.域名三 指定端口
www.test03.com 8080 /web/www/test03

4.域名四 指定IP
www.test04.com 127.0.0.1:80 /web/www/test04

nginx 虚拟主机配置如下

server {
        listen       80;
        server_name  www.test01.com;
        access_log  logs/test01.access.log  main;
        error_log  logs/test01.error.log  info;

        location / {
            root   /web/www/test01;
            index  index.php index.html index.htm;
        }

        location ~ \.php$ {
            root           /web/www/test02;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }
}

server {
        listen       80;
        server_name  www.test02.com;
        access_log  logs/test02.access.log  main;
        error_log  logs/test02.error.log  info;

        location / {
            root   /web/www/test02;
            index  index.php index.html index.htm;
        }

        location ~ \.php$ {
            root           /web/www/test02;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }
    }


    server {
        listen       8080;
        server_name  www.test03.com;
        access_log  logs/test03.access.log  main;
        error_log  logs/test03.error.log  info;

        location / {
            root   /web/www/test03;
            index  index.php index.html index.htm;
        }

        location ~ \.php$ {
            root           /web/www/test03;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }
    }


    server {
        listen 127.0.0.1:80;
        server_name  www.test04.com;
        access_log  logs/test04.access.log  main;
        error_log  logs/test04.error.log  info;

        location / {
            root   /web/www/test04;
            index  index.php index.html index.htm;
        }

        location ~ \.php$ {
            root           /web/www/test04;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }
    }

参数含义:
server_name #虚拟主机的域名,可以写多个域名,类似于别名,可以配置成如下

server_name www.test01.com www.test02.com www.test03.com

listen 80 #监听ip 和端口,这边仅仅只有端口,表示当前服务器所有ip 的80 端口,如果只想监听127.0.0.1的80,写法如下:

listen 127.0.0.1:80

root /web/www/test01 #站点根目录,网站文件路径

index index.php index.html index.htm; #索引文件

access_log logs/test04.access.log main; #访问日志

error_log logs/test04.error.log info; #错误日志

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值