什么是虚拟主机?

虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的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. cocos2d-x -3.81+win7+vs2013开发环境创建新的项目

    cocos2d-x -3.81+win7+vs2013开发环境创建新的项目 1.准备阶段 (1) vs2013下载及安装 (2)cocos2d-x 3.8.1下载及解压 (3)python下载及安装( ...

  2. 代码生成器的关键代码(读取PDM文件)

    /// <summary> /// 处理PDM文件 /// </summary> public class DoPDMDal:IDoDataBaseDal { public L ...

  3. UITabBarController 的配置

    UITabBarController --> UITabBar Customizing Appearance backgroundImage  背景图片 selectedImageTintCol ...

  4. Tomcat之安装篇- 1

    1. 提供了下载页面 以及tomcat下载地址,点击即可下载 : Tomcat9.0(Windows64) 方便好用的录像机下载请点击: gif工具 即可下载. 2.下载好的压缩包进行解压 3.配置路 ...

  5. Hotaru&#39;s problem(hdu5371+Manacher)多校7

    Hotaru's problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. 包装类、object、单例模式、final、抽象类

    /*包装类*/ /*  byte   Byte  short   Short  int    Integer  long   Long  char   Character  float   Float ...

  7. java面试第十五天

    网络编程: 多线程+网络: 1.服务器端的等待客户连接代码( while(true) ),服务器端与单个客户端交互的代码放入线程体( run ) 2.客户端如有其他要求,与服务器交互的代码也要放入线程 ...

  8. LightOj 1123-Trail Maintenance(最小生成树:神级删边)

    1123 - Trail Maintenance PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...

  9. rarcrack

    apt-get install rarcrack使用方法:rarcrack --threads xx --type rar xx.rar

  10. bzr登陆加密

    bzr提示:You have not informed bzr of your Launchpad ID, and you must do this towrite to Launchpad or a ...