什么是虚拟主机?

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

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

Nginx和Apache一样支持配置基于IP的虚拟主机,基于域名的虚拟主机,基于端口的虚拟主机这三种。

配置基于IP的虚拟主机

在Nginx配置文件(nginx.conf)中,分别对10.0.0.133、10.0.0.189、10.0.0.190三个IP配置三个纯静态HTML支持的虚拟主机。

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 ; #gzip on;
#第一个虚拟主机
server {
listen 10.0.0.133:; #监听的IP和端口
server_name 10.0.0.133; #主机名称 access_log logs/host1.access.log main; #访问日志文件存放路径 location /
{
root /usr/local/nginx/html/host1; #HTML网页文件存放的目录
index index.html index.htm; #默认首页文件,顺序从左到右,如果找不到index.html文件,则查找index.htm文件作为首页文件
}
}
#第二个虚拟主机
server {
listen 10.0.0.189:;
server_name 10.0.0.189; access_log logs/host2.access.log main; location /
{
root /usr/local/nginx/html/host2;
index index.html index.htm;
}
}
#第三个虚拟主机
server {
listen 10.0.0.190:;
server_name 10.0.0.190; access_log logs/host3.access.log main; location /
{
root /usr/local/nginx/html/host3;
index index.html index.htm;
}
}

配置基于域名的虚拟主机

其实基于域名和基于ip的虚拟主机配置是差不多的,在配置基于ip的虚拟主机上我们只需要修改几个地方就能变成基于域名的虚拟主机,一个是要修改域名,一个是host文件

worker_processes  ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
server {
listen 192.168.3.121:;
server_name www.bp1.com; #修改这里
location / {
root html;
index index.html index.htm index.php;
}
error_page /.html;
error_page /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server{
listen 192.168.3.123:;
server_name www.bp3.com; #修改这里
access_log logs/bp2.access.log combined;
location /
{
index index.html index.php;
root html/bp2;
}
location ~ \.php$ {
root html/bp2;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} }
server{
listen 192.168.3.125:;
server_name www.bp2.com; #修改这里
location /{
root html/bp3;
index index.html index.php;
}
location ~ \.php$ {
root html/bp3;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#include /opt/nginx/conf/vhosts/www.domain2.com.conf; #这一句是包含另一个nginx虚拟机主机的配置文件,其内容类似于上面最后一个server里面的内容,去掉注释后其功能相当于新增一台虚拟主机
}

配置基于端口虚拟主机

如一台服务器只有一个IP或需要通过不同的端口访问不同的虚拟主机,可以使用基于端口的虚拟主机配置。

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 ; #gzip on;
#第一个虚拟主机
server {
listen ; #监听的IP和端口
server_name localhost; #主机名称 access_log logs/host1.access.log main; #访问日志文件存放路径 location /
{
root /usr/local/nginx/html/host1; #HTML网页文件存放的目录
index index.html index.htm; #默认首页文件,顺序从左到右,如果找不到index.html文件,则查找index.htm文件作为首页文件
}
}
#第二个虚拟主机
server {
listen ;
server_name localhost; access_log logs/host2.access.log main; location /
{
root /usr/local/nginx/html/host2;
index index.html index.htm;
}
}
#第三个虚拟主机
server {
listen ;
server_name localhost; access_log logs/host3.access.log main; location /
{
root /usr/local/nginx/html/host3;
index index.html index.htm;
}
}

Nginx(二):虚拟主机配置的更多相关文章

  1. Nginx中虚拟主机配置

    一.Nginx中虚拟主机配置 1.基于域名的虚拟主机配置 1.修改宿主机的hosts文件(系统盘/windows/system32/driver/etc/HOSTS) linux : vim /etc ...

  2. Nginx的虚拟主机配置

    虚拟主机技术能够让同一台服务器.同一组Nginx进程上运行多个网站,降低了资金和服务器资源的损耗.Nginx可以配置三种类型的虚拟主机,本文就是主要介绍这三种虚拟主机配置方式. 配置基于IP的虚拟主机 ...

  3. 4.Nginx配置文件Nginx.conf_虚拟主机配置规则

    1.Nginx配置文件及各个配置项含义 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全 ...

  4. windows下搭建nginx+php+虚拟主机配置过程

    需要软件信息: nginx php RunHiddenConsole 首先安装之前要规划一下把他们放到那里,比如我将他们统一放在e :/web下 那么将这些都拷贝过来,开始吧,window要执行php ...

  5. 【nginx运维基础(2)】Nginx的配置文件说明及虚拟主机配置示例

    配置文件说明 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为当前主机的CPU总核心数. worker_processes 8; #全局错误日志定义类型, ...

  6. Nginx教程(二) Nginx虚拟主机配置

    Nginx教程(二) Nginx虚拟主机配置 1 虚拟主机管理 1.1 Nginx管理虚拟主机 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主 ...

  7. 第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置

    第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置 软件版本  uwsgi- ...

  8. Nginx教程(二) Nginx虚拟主机配置 (转)

    Nginx教程(二) Nginx虚拟主机配置 1 虚拟主机管理 1.1 Nginx管理虚拟主机 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主 ...

  9. Nginx(二)-- 配置文件之虚拟主机配置

    1.配置文件与解释 #user nobody; worker_processes 1; # 设置工作子进程,默认是1个工作子进程,可以修改,一般设置为CPU的总核数 #error_log logs/e ...

随机推荐

  1. swift学习之元组

    元组在oc中是没有的.在swift中是新加的,学oc数组概念时还在想既然能够存储同样类型的元素,那不同类型的元素有没有东西存储呢,答案非常悲伤,oc没有元组这个概念.只是swift中加入了这个东西,也 ...

  2. ArcGIS安装问题集锦

    1. 软件安装 软件下载.安装问题自行解决,否则就不要使用. 2. 常见问题 2.1 许可管理器版本不正确 2013年3月19日 问题一:ArcGIS10安装后,更改许可管理器时,通常,在ArcGIS ...

  3. Windows 之 防火墙

          对于只使用浏览.电子邮件等系统自带的网络应用程序,Windows防火墙(firewall)根本不会产生影响.也就是说,用IE.OutlookExpress等系统自带的程序进行网络连接,防火 ...

  4. 网页中的图片查看器viewjs使用

    需求分析: 对于网页中的图片进行连续放大(便于用户清晰查看内容).缩小,旋转等操作,可以使用viewjs图片查看器插件实现. viewjs官方网址:https://github.com/fengyua ...

  5. quartz实现定时任务调度

    一. 业务需求: 实际工作中我们一般会遇到这种需求: 使用Ajax技术每隔几秒从缓存或数据库中读取一些数据, 然后再显示在页面上, 眼下有一个比較好的定时调度框架: quartz能够满足我们的需求. ...

  6. exception Access restriction: The type 'BASE64Encoder' is not API

      Created by Marydon on 1.情景展示 在eclipse中切换jdk版本后,报错信息为:exception Access restriction: The type 'BASE6 ...

  7. Jprofiler监控工具(内存泄漏)

    内存泄漏 1.测试代码 /** * JProfiler内存监控例子 * * @author yhye * @2011-11-9上午09:46:06 */ public class JProfilerM ...

  8. AIX查看系统安装时间和运行时长

    $ lslpp -h bos.mp* --AIX系统的安装时间,可以通过bos.mp和bos.mp64文件集的安装时间得知 Fileset Level Action Status Date Time ...

  9. Android基于TCP的局域网聊天通信

    概述 在同一局域网内,两台设备通过TCP进行通信聊天. 详细 代码下载:http://www.demodashi.com/demo/10567.html 一.准备工作 开发环境 jdk1.8 Ecli ...

  10. How to forcefully delete a daemonset or a pod in kubernetes cluster

    I have setup a kubernetes cluster which is working fine. I created deployment with type as daemonset ...