Nginx—基本使用

最小配置文件

#表示在启动work时,启动多少个work进程,需要根据你具体的CPU核数去优化
worker_processes  1;

#表示一个work能有多少个连接
events {
    worker_connections  1024;
}


http {
    #表示将其他配置文件引到当前配置文件当中
    include       mime.types;
    default_type  application/octet-stream;

    #sendfile指令指定nginx是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须        设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度。
    sendfile        on;
    #连接保持超时时间
    keepalive_timeout  65;

 
    server {
        listen       80;
        server_name  localhost;

     

        location / {
            root   html;
            index  index.html index.htm;
        }

      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
       
    }

}

虚拟主机、Nginx与Http协议

虚拟主机原理

Host文件解析域名

文件路径:C:\Windows\System32\drivers\etc

1、由于该文件起始没有修改权限,所以首先需要我们自己赋予权限

右击->熟悉->安全->选择用户->修改权限

2、 修改后如下

3、 测验一下

 

 能够成功是因为我们在浏览器输入url进行访问时,浏览器将我们的url解析后,第一步是去hosts文件查看缓存(IP — 域名),若有对应的域名,则会直接返回缓存的IP地址

公网域名配置与泛域名解析

Nginx虚拟主机域名配置

1、在根目录下创建一个文件夹www (路径和文件夹由你自己定义)

2、在www文件夹下再创建两个文件夹 www、vod,并分别在两个文件夹下创建index.html文件

3、编辑nginx配置文件

worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    #服务1
    server {
        listen       80;
        server_name  localhost;

    
        location / {
            #注意,路径要和你刚刚创建的文件的路径一致
            root   /www/www;
            index  index.html index.htm;
        }

      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    #服务2
    server {
        listen       88;
        server_name  localhost;

    
        location / {
            root   /www/vod;
            index  index.html index.htm;
        }

      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

4、测验

 

 servername匹配规则

基于域名的几种互联网企业需求

1、多用户二级域名

2、短网址

3、httpdns

反向代理

负载均衡

环境准备

1、克隆两台服务,并且修改网络配置(同安装部署篇操作步骤一致)

2、修改一下克隆的两台主机 /www/vod/index.html的内容

3、测验一下

 

修改配置文件

worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    #服务组
    upstream httpds {
       server 192.168.5.136:88;
       server 192.168.5.137:88;  
    }

    server {
        listen       80;
        server_name  localhost;

    
        location / {
		proxy_pass http://www.atguigu.com;
            #root   /www/www;
            #index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    server {
        listen       88;
        server_name  localhost;

    
        location / {
		proxy_pass http://httpds;
            #root   /www/vod;
            #index  index.html index.htm;
        }

      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

测验

负载均衡——权重

worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream httpds {
       server 192.168.5.136:88 weight=8;
       server 192.168.5.137:88 weight=2;  
    }

    server {
        listen       80;
        server_name  localhost;

    
        location / {
		proxy_pass http://www.atguigu.com;
            #root   /www/www;
            #index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    server {
        listen       88;
        server_name  localhost;

    
        location / {
		proxy_pass http://httpds;
            #root   /www/vod;
            #index  index.html index.htm;
        }

      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

测验

自己多访问几次,会发现192.168.5.136:88的访问次数会高很多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值