hello

Nginx下配置虚拟主机的三种方法

 

Nginx下,一个server标签就是一个虚拟主机。

1、基于域名的虚拟主机,通过域名来区分虚拟主机——应用:外部网站

2、基于端口的虚拟主机,通过端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台

3、基于ip的虚拟主机,几乎不用。

基于域名配置虚拟主机步骤:

需要建立/data/www /data/bbs目录,windows本地hosts添加虚拟机ip地址对应的域名解析;对应域名网站目录下新增index.html文件;

nginx.conf配置文件新增如下代码:

server {

  listen 80;

  server_name www.linuxidc.com;

  index index.html;

  root /data/www;

}

server {

  listen 80;

  server_name bbs.linuxidc.com;

  index index.html;

  root /data/bbs;

}


验证结果,使用curl测试,或者浏览器输入域名访问;

# curl -xlocalhost:80 www.linuxidc.com

this is linuxidc linux

# curl -xlocalhost:80 bbs.linuxidc.com

this is linuxidc bbs

基于端口的虚拟主机配置:   

使用端口来区分,浏览器使用域名或ip地址:端口号 访问;

server

{

    listen 8000;

    server_name www.linuxidc.com;

    root /data/www;

}

server

{

    listen 8001;

    server_name www.linuxidc.com;

    root /data/bbs;

}


验证结果,使用curl测试,或者浏览器输入域名访问;

# curl www.linuxidc.com:8000

this is linuxidc linux

# curl www.linuxidc.com:8001

this is linuxidc bbs


基于ip地址的虚拟主机配置:

通过ip来访问,需要配置多个ip;

# ifconfig eth0:1 192.168.22.21

server

{

    listen 192.168.20.20:80;

    server_name www.linuxidc.com;

    root /data/www;

}

server

{

    listen 192.168.20.21:80;

    server_name www.linuxidc.com;

    root /data/bbs;

}

验证结果,使用curl测试,或者浏览器输入域名访问;

# curl 192.168.22.20

this is linuxidc linux

# curl 192.168.22.21

this is linuxidc bbs

 

 

Windows nginx 配置tp虚拟机

 

server {
        listen       80;
        server_name  local.test.com;
        root   "C:\Users\Administrator\Desktop\yes1";
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
            if (!-e $request_filename) {
                rewrite ^/(.*)$ /index.php/$1 last;
            }
            
        }
        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;
        }
}

 

注意:根目录名字不能出现test这个词

posted @ 2018-03-20 16:34  红尘练  阅读(265)  评论(0编辑  收藏  举报
你来了