首页 > 学院 > 开发设计 > 正文

Nginx虚拟主机配置

2019-11-06 07:06:31
字体:
来源:转载
供稿:网友

1 虚拟主机管理

1.1 Nginx管理虚拟主机

虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的Intemet服务器功能(WWW、FTP、Email等),同一台主机上的虚拟主机之间是完全独立的。从网站访问者来看,每一台虚拟主机和一台独立的主机完全一样。

利用虚拟主机,不用为每个要运行的网站提供一台单独的Nginx服务器或单独运行一组Nginx进程。虚拟主机提供了在同一台服务器、同一组Nginx进程上运行多个网站的功能。

 

1.2 Nginx基本配置

1、Nginx的主配置文件是:nginx.conf,nginx.conf主要组成如下:

[html] view plain copy在CODE上查看代码片# 全局区   有一个工作子进程,一般设置为CPU数 * 核数  worker_PRocesses  1;     events {          # 一般是配置nginx进程与连接的特性          # 如1个Word能同时允许多少连接,一个子进程最大允许连接1024个连接          worker_connections  1024;  }    # 配置HTTP服务器配置段  http {            # 配置虚拟主机段              server {                                # 定位,把特殊的路径或文件再次定位。          location  {                       }       }        server {              ...      }  }  

1.3%20基于域名的虚拟主机

1、在http大括号中添加如下代码段:

[html]%20view%20plain%20copy    server {            #监听端口 80            listen 80;                                               #监听域名abc.com;            server_name abc.com;                      location / {                                # 相对路径,相对nginx根目录。                root    abc;                              # 默认跳转到index.html页面                index index.html;                           }        }   2、切换安装目录:cd/usr/local/software/nginx

3、创建目录:mkdir%20abc

4、新建index.html文件:vi%20/usr/local/software/nginx/abc/index.html,文件内容:

[html]%20view%20plain%20copy    <html>          <head>              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />          </head>          <body>              <h2>基于域名的虚拟主机-index</h2>          </body>      </html>  5、重新读取配置文件:

/usr/local/software/nginx/sbin/nginx-s%20reload

kill%20-HUP进程号

6、配置windows本机host:

192.168.197.142 abc.com %20#linux服务器ip地址

7、访问:http://abc.com:80/

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

1、编辑配置文件:vim/usr/local/software/nginx/conf/nginx.conf

[html] view plain copy在CODE上查看代码片    server {          listen  2022;          server_name     abc.com;          location / {             root    /home;             index index.html;          }      }  2、新建index.html文件:vi%20/home/index.html,文件内容:

[html]%20view%20plain%20copy      <html>            <head>                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />            </head>            <body>                <h2>基于端口的虚拟主机配置-index</h2>            </body>        </html>  3、重新读取配置文件:

/usr/local/software/nginx/sbin/nginx-s%20reload

4、访问:http://abc.com:2022/

 

1.5 基于IP地址虚拟主机配置

1、编辑配置文件:vim/usr/local/software/nginx/conf/nginx.conf

[html] view plain copy在CODE上查看代码片    server {        listen  80;        server_name  192.168.197.142;        location / {                root    ip;                index index.html;        }      }  2、新建index.html文件:

执行命令:

mkdir%20/usr/local/software/nginx/ip

vi%20/usr/local/software/nginx/ip/index.html

文件内容:

[html]%20view%20plain%20copy      <html>            <head>                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />            </head>            <body>                <h2>基于IP地址虚拟主机配置-index</h2>            </body>        </html>  3、重新读取配置文件:

/usr/local/software/nginx/sbin/nginx-s%20reload

4、访问:http://192.168.197.142/

                 %20
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表