Linux Nginx——limit_rate 限制客户端传输数据的速度、Nginx 虚拟主机配置

  • 如何使用 limit_rate 限制客户端传输数据的速度

编辑Nginx主配置(conf)文件

vim /etc/nginx/nginx.conf
location / {
            root   /var/www/nginx/;
            index  index.html index.htm;
            limit_rate  2k;  #对每个连接的限速为2k/s
}
restart/reload重启/重新加载服务

配置文件中的每个语句必须以英文分号";"结尾


  • Nginx 虚拟主机配置

虚拟主机:
虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供web服务,实现一台主机对外提供多个web服务,单个虚拟主机是独立的,虚拟主机间互不影响。
在这里插入图片描述

Nginx可实现虚拟主机的配置,支持三种类型的虚拟主机配置:
1、基于域名的虚拟主机 (server_name来区分虚拟主机)
2、基于ip的虚拟主机(一个主机绑定多个ip地址)
3、基于端口的虚拟主机 (端口来区分虚拟主机)

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

[root@localhost ~]# cat /etc/nginx/nginx.conf
user nginx;
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.testvc.com;
        location / {
            root   /var/www/nginx/;
            index  index.html index.htm;
            limit_rate    2k;
            }
        }

    server {
        listen       80;
        server_name  web.vccloud.com;
        location / {
            root   /vccloud/html;
            index  index.html index.htm;
            }
        }
}

[root@localhost nginx]# cat /var/www/nginx/index.html 
hello tianyun

[root@localhost nginx]# cat /vccloud/html/index.html 
this is my vccloud

2、 为 web.vccloud.com 创建 index 文件
[root@localhost ~]# mkdir -p /vccloud/html
[root@localhost ~]# vim /vccloud/html/index.html
this is my vccloud

3、重新加载配置文件

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

4、客户端配置解析
在 Windows下C:\Windows\System32\drivers\etc\hosts 【快捷键Windows+R:drivers】文件中添加(linux:/etc/hosts)

10.0.10.199  web.testvc.com
10.0.10.199  web.vccloud.com

5、 测试访问

浏览器输入:http://web.testvc.com/
浏览器输入:http://web.vccloud.com/

基于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.10.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.10.200/24
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.10.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.10.200  netmask 255.255.255.0  broadcast 10.0.105.255
        ether 00:0c:29:17:f1:af  txqueuelen 1000  (Ethernet)

# 配置通过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       80;
        server_name  10.0.10.199;
        location / {
            root   /var/www/nginx/;
            index  index.html index.htm;
            limit_rate    2k;
        }

     server {
        listen       80;
        server_name  10.0.10.200;
        location / {
            root   /vccloud/html/;
            index  index.html index.htm;
            }
        }
}
# 重新加载配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
#测试访问
浏览器访问:http://10.0.10.199
浏览器访问:http://10.0.10.200

# 删除绑定的临时ip
[root@localhost ~]# ifconfig ens33:1 10.0.10.200/24 down
#重启一下nginx
[root@localhost ~]# systemctl restart nginx

基于端口的虚拟主机

[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.testvc.com;
        location / {
            root   /var/www/nginx/;
            index  index.html index.htm;
            limit_rate    2k;
        }


     server {
        listen       8080;
        server_name  web.testvc.com;
        location / {
            root   /vccloud/html/;
            index  index.html index.htm;
            }
        }
}
# 重新加载配置文件:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
# 测试访问:
浏览器访问:http://web.testvc.com/
浏览器访问:http://web.vccloud.om:8080
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值