Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >nginx服务器配置虚拟主机

nginx服务器配置虚拟主机

作者头像
十月梦想
发布于 2018-08-29 06:47:13
发布于 2018-08-29 06:47:13
3.9K00
代码可运行
举报
文章被收录于专栏:十月梦想十月梦想
运行总次数:0
代码可运行

NGINX服务器下配置虚拟主机

在哪里配置?

对于虚拟主机的配置可以在nginx.conf里面配置或者vhosts.conf下,由于vhost.conf便于管理我们在这个文件夹下进行配置虚拟主机

如何配置?

在vhosts.conf下新增一个server表示一个虚拟主机,配置虚拟主机三种方式(端口号,域名,ip地址)

    配置代码如下:

①以端口号为基础创建虚拟主机

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#80端口号下虚拟主机
server {
        listen       80;//监听端口
        server_name  localhost ;//绑定域名
        root   "D:\www";//根目录
        location / {
            index  index.html index.htm index.php;
            //autoindex 是否开启目录列表
            autoindex  on;
        }
        //以下是伪静态规则
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}
#8080端口号虚拟主机
server {
        listen       8080;
        server_name  www.a.com a.com;
        root   "D:\phpStudy\WWW";
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

②以域名配置不同虚拟主机

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#使用 www.test.com访问web1虚拟主机
server {
        listen       80;
        server_name  www.test.com ;
        root   "D:\www\web1";
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}
#使用pic.test.com访问web2虚拟主机
server {
        listen       80;
        server_name  pic.test.com ;
        root   "D:\www\web2";
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

③以ip为基准配置虚拟主机

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//需要自行支持ip内分配地址(一般为内网ip段)
server {
        listen      192.168.20.20:80;
        server_name  localhost ;
        root   "D:\www";
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}
server {
        listen     192.168.20.20:80;
        server_name  localhost ;
        root   "D:\www";
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-8-1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验