zoukankan      html  css  js  c++  java
  • Nginx(二):虚拟主机配置

    什么是虚拟主机?

    虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的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  65;
    
        #gzip  on;
        #第一个虚拟主机
        server {
            listen       10.0.0.133:80;               #监听的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:80;
            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:80;
            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  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       192.168.3.121:80;
            server_name  www.bp1.com;   #修改这里
            location / {
                root   html;
                index  index.html index.htm index.php;
            }
            error_page  404              /404.html;
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
            location ~ .php$ {
                root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
        }
        server{
            listen 192.168.3.123:80;
            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:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
    
        }
        server{
            listen 192.168.3.125:80;
            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:9000;
                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  65;
    
        #gzip  on;
        #第一个虚拟主机
        server {
            listen       8111;               #监听的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       8112;
            server_name  localhost;
    
            access_log  logs/host2.access.log  main;
    
            location /
            {
                root /usr/local/nginx/html/host2;
                index  index.html index.htm;
            }
    }
        #第三个虚拟主机
        server {
            listen       8113;
            server_name  localhost;
    
            access_log  logs/host3.access.log  main;
    
            location /
            {
                root /usr/local/nginx/html/host3;
                index  index.html index.htm;
            }
    }
  • 相关阅读:
    Roce ofed 环境搭建与测试
    Ubuntu 1804 搭建NFS服务器
    Redhat 8.0.0 安装与网络配置
    Centos 8.1 安装与网络配置
    SUSE 15.1 系统安装
    VSpare ESXi 7.0 基本使用(模板、iso、SRIOV)
    VSpare ESXi 7.0 服务器安装
    open SUSE leap 15.1 安装图解
    KVM虚拟机网卡连接网桥
    GitHub Action一键部署配置,值得拥有
  • 原文地址:https://www.cnblogs.com/shamo89/p/9949460.html
Copyright © 2011-2022 走看看