• nginx 虚拟主机配置


    什么是虚拟主机?
    虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。

    nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置。
    1、基于域名的虚拟主机 (server_name来区分虚拟主机——应用:外部网站)
    2、基于ip的虚拟主机, (一台主机绑定多个ip地址)
    3、基于端口的虚拟主机 (端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)

    1、 基于域名的虚拟主机

    1、配置通过域名区分的虚拟机

    [root@localhost ~]# cat /etc/nginx/nginx.conf
    worker_processes  4;
    
    #error_log  logs/error.log;
    worker_rlimit_nofile 102400;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        
        server {
            listen       80;
            server_name  web.testpm.com;
            location / {
                root   /var/www/nginx/;
                index  index.html index.htm;
                limit_rate	2k;
            	}
            }
        
        server {
            listen       80;
            server_name  web.1000phone.com;
            location / {
                root   /1000phone/html;
                index  index.html index.htm;
            	}
            }
    }
    

    2、 为 域名为 web.1000phone.com 的虚拟机,创建 index 文件

    [root@localhost ~]# mkdir -p /1000phone/html
    [root@localhost ~]# vim /1000phone/html/index.html
    <html>
    <p>
    this is my 1000phone
    </p>
    </html>
    

    3、重新加载配置文件

    # 如果编译安装的执行
    [root@nginx]# /usr/local/nginx/sbin/nginx -s reload
    # 如果 yum 安装的执行
    [root@nginx]# nginx -s reload
    

    4、客户端配置路由映射
    在 C:WindowsSystem32driversetchosts 文件中添加两行(linux:/etc/hosts)

    10.0.105.199 web.testpm.com
    10.0.105.199 web.1000phone.com
    

    5、 测试访问

    浏览器输入:http://web.testpm.com/

    浏览器输入:http://web.1000phone.com/

    6、补充:如果配置不能正常访问,

    问题描述: 配置完 nginx 两个虚拟机后,客户端能够访问原始的server ,新增加的 server 虚拟机 不能够访问,报错如下页面

    解决过程:

    1. 查看报错日志(找到错误日志)

      [root@localhost ~]# cat /var/log/nginx/error.log
      2017/06/15 04:00:57 [error] 6702#0: *14 "/root/html/index.html" is forbidden (13: Permission denied), client: 10.219.24.1, server: web.1000phone.com, request: "GET / HTTP/1.1", host: "web.1000phone.com"
      

      2.检查权限

      [root@localhost html]# ll
      drwxr-xr-x. 2 root root 4096 Jun 15 03:59 html
      [root@localhost nginx]# ll
      total 8
      -rw-r--r--. 1 root root 537 Jun 15 03:59 50x.html
      -rw-r--r--. 1 root root 616 Jun 15 03:51 index.html
      说明:发现目录权限没有问题
      

      3.检查nginx启动进程

      [root@localhost nginx]# ps -ef | grep nginx
      root       2079      1  0 12:16 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
      nobody     2080   2079  0 12:16 ?        00:00:00 nginx: worker process
      nobody     2081   2079  0 12:16 ?        00:00:00 nginx: worker process
      nobody     2082   2079  0 12:16 ?        00:00:00 nginx: worker process
      说明:发现nginx的work process是 nobody 的
      

      4.修改 nginx.conf 文件

      打开nginx.conf文件所在的目录,查看文件的属性 (root root)
      [root@nginx]# ll
      drwxr-xr-x. 2 root root 4096 Jun 15 04:08 conf
      在nginx.conf文件的第一行加上 user root root;
      [root@nginx]# cat conf/nginx.conf
      user root;
      

      重新 reload nginx进程

    2、 基于ip的虚拟主机

    [root@localhost ~]# ip a 
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:17:f1:af brd ff:ff:ff:ff:ff:ff
        inet 10.0.105.199/24 brd 10.0.105.255 scope global dynamic ens33
           valid_lft 81438sec preferred_lft 81438sec
        inet6 fe80::9d26:f3f0:db9c:c9be/64 scope link 
           valid_lft forever preferred_lft forever
    [root@localhost ~]# ifconfig ens33:1 10.0.105.201/24
    [root@localhost ~]# ifconfig
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.0.105.199  netmask 255.255.255.0  broadcast 10.0.105.255
            inet6 fe80::9d26:f3f0:db9c:c9be  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:17:f1:af  txqueuelen 1000  (Ethernet)
            RX packets 9844  bytes 1052722 (1.0 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 5567  bytes 886269 (865.4 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.0.105.201  netmask 255.255.255.0  broadcast 10.0.105.255
            ether 00:0c:29:17:f1:af  txqueuelen 1000  (Ethernet)
    
    2、配置通过ip区分的虚拟机
    [root@localhost ~]# cat /etc/nginx/nginx.conf
    user  root;
    worker_processes  4;
    
    #error_log  logs/error.log;
    worker_rlimit_nofile 102400;
    
    
    events {
        worker_connections  1024;
    }
    
    
    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"';
    
        server {
            listen       10.0.105.199:80;
            server_name  web.testpm.com;
            location / {
                root   /var/www/nginx/;
                index  index.html index.htm;
                limit_rate	2k;
            }
            
         server {
            listen       10.0.105.201:80;
            server_name  web.testpm.com;
            location / {
                root   /1000phone/html/;
                index  index.html index.htm;
            	}
            }
    }
    3、重新加载配置文件
    [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
    4、 测试访问
    浏览器输入:http://10.0.105.199
    浏览器输入:http://10.0.105.201
    5、补充
    -- 删除绑定的vip
    [root@localhost ~]# ifconfig ens33:1 10.0.105.201/24 down
    重启一下nginx
    [root@localhost ~]# systemctl restart nginx
    

    3、 基于端口的虚拟主机

    [root@localhost ~]# cat /etc/nginx/nginx.conf
    user  root;
    worker_processes  4;
    
    worker_rlimit_nofile 102400;
    
    
    events {
        worker_connections  1024;
    }
    
    
    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"';
    
    
        sendfile        on;
    
        keepalive_timeout  65;
    
    
        server {
            listen       80;
            server_name  web.testpm.com;
            location / {
                root   /var/www/nginx/;
                index  index.html index.htm;
                limit_rate	2k;
            }
            
        
         server {
            listen       8080;
            server_name  web.1000phone.com;
            location / {
                root   /1000phone/html/;
                index  index.html index.htm;
            	}
            }
    }
    重新加载配置文件:
    [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
    测试访问:
    浏览器输入:http://web.testpm.com/
    浏览器输入:http://web.1000phone.com:8080
    
  • 相关阅读:
    yum 本地 网络 openstack
    docker 负载均衡
    tomcat + nginx 负载均衡 动静分离 session会话配置
    K8S Ingress 之 traefik-ingress-controller
    CentOS7安装和配置rsync+inotify
    DAS,NAS,SAN在数据库存储上的应用
    inotify+rsync实现实时同步
    NFS网络文件系统
    CentOS7下rsync服务的基本详解和使用
    LVM--逻辑卷管理
  • 原文地址:https://www.cnblogs.com/pengpengboshi/p/13167867.html
Copyright © 2020-2023  润新知