前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx服务器配置虚拟主机

nginx服务器配置虚拟主机

作者头像
十月梦想
发布2018-08-29 14:47:13
3.8K0
发布2018-08-29 14:47:13
举报
文章被收录于专栏:十月梦想

NGINX服务器下配置虚拟主机

在哪里配置?

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

如何配置?

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

    配置代码如下:

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

代码语言:javascript
复制
#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
复制
#使用 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
复制
//需要自行支持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 删除。

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • NGINX服务器下配置虚拟主机
    • 在哪里配置?
      • 如何配置?
        • ①以端口号为基础创建虚拟主机
        • ②以域名配置不同虚拟主机
        • ③以ip为基准配置虚拟主机
    相关产品与服务
    轻量应用服务器
    轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门软件打包实现一键构建应用,提供极简上云体验。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档