Nginx(二):虚拟主机配置

时间:2022-09-10 12:21:16

什么是虚拟主机?

虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的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. scp报错 -bash: scp: command not found

    环境:RHEL6.5 使用scp命令报错: [root@oradb23 media]# scp /etc/hosts oradb24:/etc/ -bash: scp: command not fou ...

  2. freeswitch模块之event_socket

    这是我之前整理的关于freeswitch mod_event_socket的相关内容,这里记录下,也方便我以后查阅. mod_event_socket以socket的形式,对外提供控制FS一种途径, ...

  3. 几种linux脚本的简单执行方法

    1.hash脚本文件名:assign 内 容:#!/bin/sh cd $1ls 执行: [root@db2 ~]# sh helle2.sh /usr 或者[root@db2 ~]#./helle2 ...

  4. IIS——发布网站

    当我们要上线一个网站时,不要把整个项目原封不动的发布到服务器,而要经过右键发布后,然后再将发布的文件路径配置到IIS~ 详细信息见链接:http://www.52ij.com/jishu/aspx/1 ...

  5. javascript键盘输入控制

    获取键盘控制事件 document.onkeydown = keyDown 当浏览器读到这个语句时,无论按下键盘上的哪个键,都将呼叫KeyDown()函数. 不同浏览器的实现: Netscape Ne ...

  6. ON THE EVOLUTION OF MACHINE LEARNING: FROM LINEAR MODELS TO NEURAL NETWORKS

    ON THE EVOLUTION OF MACHINE LEARNING: FROM LINEAR MODELS TO NEURAL NETWORKS We recently interviewed ...

  7. HDU5100Chessboard(数论)

    HDU5100Chessboard(数论) 题目链接 题目大意:用k∗1的瓷砖区铺n∗n的矩形,问能铺上的最大的面积. 解题思路:这题没有直接得出结论:l = n%k, ans = max[(n^2 ...

  8. django获取某一个字段的列表,values/values_list/flat

    class Building(models.Model): corporation = models.ForeignKey('company.Corporation', verbose_name=u' ...

  9. Nginx+uwsgi+supervisor+Ubuntu+flask

    Nginx+uwsgi+supervisor+Ubuntu+flask Nginx+uwsgi+supervisor在Ubuntu上部署flask应用 网上找了许多讲关于Flask应用部署的文章几乎都 ...

  10. C# foreach 有用方法具体解释

    网上查资料,说foreach 不能改动迭代变量,仅仅能訪问迭代变量.自己理解也不是非常深,通过几个代码进行验证,发现foreach的使用方法还有点特别 验证方法: 1. 迭代变量 为int int[] ...