Nginx实现虚拟机

来源:互联网 发布:天音淘宝复制大师3.02 编辑:程序博客网 时间:2024/06/11 13:11

区分不同的网站有三种方式:

1、ip区分

方法一:

使用标准的网络配置工具(比如ifconfigroute命令)添加lP别名:

当前ip配置情况:


eth0网卡再绑定一个ip192.168.101.103

 

/sbin/ifconfig eth0:1 192.168.101.103 broadcast 192.168.101.255 netmask 255.255.255.0 up

/sbin/route add -host 192.168.101.103 dev eth0:1

方法二:

1/etc/sysconfig/network-scripts/ifcfg-eth0文件复制一份,命名为ifcfg-eth0:1

修改其中内容:

DEVICE=eth0:1

IPADDR=192.168.25.103

其他项不用修改

2、重启系统

 Nginx的配置文件

 

#user  nobody;

worker_processes  1;

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

 

events {

    worker_connections  1024;

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

 

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

 

    #access_log  logs/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

    #gzip  on;

 

    server { #一个Server就是一个虚拟主机

        listen       80;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html;

            index  index.html index.htm;

        }

    }

 

}

基于ip的虚拟主机配置

server {

        listen       80;

        server_name  192.168.25.141;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html-141;

            index  index.html index.htm;

        }

 

        

    }

 

    server {

        listen       80;

        server_name  192.168.25.100;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html-100;

            index  index.html index.htm;

        }

 

        

    }

Nginx重新加载配置文件。

2、端口区分

server {

        listen       81;

        server_name  192.168.25.141;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html-81;

            index  index.html index.htm;

        }

 

        

    }

    server {

        listen       82;

        server_name  192.168.25.141;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html-82;

            index  index.html index.htm;

        }

 

        

    }


3、域名区分

最有用的虚拟主机配置方式。

一个域名只能绑定一个ip地址,一个ip地址可以被多个域名绑定。


可以修改host文件实现域名访问。


可通过软件修改,无需权限,访问目录等(此软件)

软件下载地址:

http://download.csdn.net/detail/qq1137623160/9823260


也可访问目录C:\Windows\System32\drivers\etc修改windowhosts文件。

基于域名的虚拟主机配置

server {

        listen       80;

        server_name  www.itheima.com;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html-www;

            index  index.html index.htm;

        }

 

        

    }

 

    server {

        listen       80;

        server_name  hehe.itheima.com;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html-hehe;

            index  index.html index.htm;

        }

 

        

    }

修改配置文件后,需要nginx重新加载配置文件。


3 0