什么是虚拟主机?

虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的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. (LeetCode 83)Remove Duplicates from Sorted Lists

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. VM虚拟机如何和主机共享文件夹或文件

    请一定要选中Map as a network drive in Windows guests,否则将无法查看共享.

  3. 绕过Web授权和认证之篡改HTTP请求

    一.什么是HTTP请求 超文本传输协议(HTTP)提供了多种请求方法来与web服务器沟通.当然,大多数方法的初衷是帮助开发者在开发或调试过程中部署和测试HTTP应用.如果服务器配置不当,这些请求方法可 ...

  4. UITextView自适应高度

    - (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{ float fPadding = ...

  5. uni - 介绍

    uni-app 是一个使用 Vue.js 开发跨平台应用的前端框架,开发者编写一套代码,可编译到iOS.Android.H5.小程序等多个平台 优点:基于vue.mpvue.微信小程序 微信小程序AP ...

  6. [转]自定义Drawable实现灵动的红鲤鱼动画(下篇)

      小鱼儿 上篇文章自定义Drawable实现灵动的红鲤鱼动画(上篇)我们绘制了可以摆动身体的小鱼,本篇就分享一下如何让小鱼游到手指点击的位置.用到的主要技术如下: 1).三阶贝塞尔曲线 2).Pat ...

  7. 动态IP或无公网IP时外网訪问内网ORACLE数据库

    ORACLE数据库是应用最多的一个数据库.一般项目应用.将ORACLE部署在内网,内网调用,及运维都仅仅能是内网完毕. 假设ORACLE主机或所在局域网没有固定公网IP,又想在外网对ORACLE进行訪 ...

  8. Content-Length实体的大小

    •15.2 Content-Length实体的大小 Content-Length首部指出了报文中实体主体的字节大小,这个大小包含了所有内容的编码,如对文本进行gzip压缩的话,那么Content-Le ...

  9. Drupal administration theme

    Drupal允许为管理后台设置独立的theme,保存在系统变量variable_get('admin_theme'). Drupal使用全局变量$theme来保存当前请求对应的主题.Drupal在启动 ...

  10. 用C/C++开发android应用

    在某些情况下,比如原来与很多c/c++的代码, 可能希望采用c/c++编写android应用程序.在这种情况下,一般使用NDK.但是由于android直提供了java接口,因此不能够直接调用andro ...