Nginx操作(负载均衡,动静分离,会话保持,防盗链)

1.通过nginx命令控制nginx服务

nginx -c /path/nginx.conf               # 以特定目录下的配置文件启动nginx:
nginx -s reload                             # 修改配置后重新加载生效
nginx -s reopen                            # 重新打开日志文件
nginx -s stop                               # 快速停止nginx
nginx -s quit                               # 完整有序的停止nginx
nginx -t                                      # 测试当前配置文件是否正确
nginx -t -c /path/to/nginx.conf   # 测试特定的nginx配置文件是否正确
 

2.通过stub_status模块监控nginx的工作状态(etc/nginx/conf.d/default.conf)

#添加以下内容~~ 
location /nginx-status { 
      stub_status on; 
      access_log    /var/log/nginx/nginxstatus.log;    #设置日志文件的位置 
      auth_basic    "nginx-status";    #指定认证机制(与location后面的内容相同即可) 
      auth_basic_user_file    /etc/nginx/htpasswd;     #指定认证的密码文件 
      }

3.创建认证口令文件并添加用户mm,密码用md5加密

yum -y install  httpd-tools  

#htpasswd 是开源 http 服务器 apache httpd 的一个命令工具,用于生成 http 基本认证的密码文件

htpasswd  -c  -m /etc/nginx/htpasswd   mm

4. 使用limit_rate限制客户端传输数据的速度

location / {
            root   /var/www/nginx/; 
            index  index.html index.htm;
            limit_rate  2k;  #对每个连接的限速为2k/s
        }

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

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

6.nginx负载均衡

当你的nginx服务器给2台web服务器做代理,负载均衡算法采用轮询,当web1坏掉后,nginx服务器首先请求web1,再响应超时后,把请求分发给web2,来继续运转

此代码为两台web机的配置

upstream testapp { 
      server 10.0.105.199:8081;
      server 10.0.105.202:8081;
    }
 server {
        listen 80;
        server_name localhost;
        location / {         
           proxy_pass  http://testapp;  #请求转向 testapp 定义的服务器列表         
        } 
upstream mysvr { 
      server  http://10.0.105.199:8081;
      server  http://10.0.105.202:8081;
    }
 server {
        listen 80;
        server_name localhost;
        location  / {         
           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表         
        } 

1.热备:在第二台虚拟机ip端口后加上 backup;代表热备

2.轮询:nginx默认为轮询其权重都默认为1,服务器请求数据:abababab...

3.加权轮询:server 172.17.14.2:8080 weight=1;  在后面加上weight
                     server 172.17.14.3:8080 weight=2;

4.ip_hash:nginx会让相同的客户端ip请求相同服务器

5.nginx负载均衡配置状态参数

  • down,表示当前的server暂时不参与负载均衡。

  • backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。

  • max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。

  • fail_timeout,在经历了max_fails次失败后,暂停服务的时间单位秒。max_fails可以和fail_timeout一起使用。

 upstream myweb { 
      server 172.17.14.2:8080 weight=2 max_fails=2 fail_timeout=2;
      server 172.17.14.3:8080 weight=1 max_fails=2 fail_timeout=1;    
    }

7.nginx会话保持

ip_hash(哈希算法)

upstream backend {
    ip_hash;
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com down;
}

ip_hash使用源地址哈希算法,将同一客户端的请求总是发往同一个后端服务器,除非该服务器不可用。

当后端服务器宕机后,session会丢失; 来自同一局域网的客户端会被转发到同一个后端服务器,可能导致负载失衡; 不适用于CDN网络,不适用于前段还有代理的情况。

8.nginx 实现动静分离

使用正则表达式匹配过滤,然后交给不同的服务器

(动态资源需要使用php,先下载好php)

upstream static {
        server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=60s;
        }
upstream php {
        server 10.0.105.200:80 weight=1 max_fails=1 fail_timeout=60s;
        }
     server {
        listen      80;
        server_name     localhost;
        #动态资源加载
        location ~ \.(php|jsp)$ {
            proxy_pass http://php;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                }
        #静态资源加载
        location ~ .*\.(html|gif|jpg|png|bmp|swf|css|js)$ {
            proxy_pass http://static;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                }
        }

9.nginx防盗链问题

两个网站 A和B,A网站引用了B网站上的图片,这种行为为盗链,防盗链为防止A引用B的图片

noon:允许没有http_refer的请求访问资源;加noon的话本地可以访问,其他人无法访问

blocked:允许不是http://开头的,不带协议的请求访问资源;

server_names:只允许指定ip/域名来请求访问资源(白名单)

实验:准备两台机器,

server {
    listen       80;
    server_name  localhost;

    location / {
         root   /usr/share/nginx/html;
         index  index.html index.htm;

         valid_referers none blocked *.cc.com 10.0.105.202;
                if ($invalid_referer) {
                   return 502;
                }
        }
    location ~  .*\.(gif|jpg|png|jpeg)$ {
         root  /usr/share/nginx/html;

         valid_referers  qf.com 10.0.105.202;
                if ($invalid_referer) {
                   return 403;
                }
        }
}

第二台机器客户端
配置nginx访问页面
创建页面
[root@nginx-server nginx]# vim index.html
<html>
<head>
    <meta charset="utf-8">
    <title>qf.com</title>
</head>
<body style="background-color:red;">
    <img src="http://10.0.105.202/test.jpg"/>
</body>
</html>

测试不带http_refer:
[root@nginx-server nginx]# curl -I "http://10.0.105.202/test1.png"
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Thu, 27 Jun 2019 16:21:13 GMT
Content-Type: image/png
Content-Length: 235283
Last-Modified: Thu, 27 Jun 2019 11:27:11 GMT
Connection: keep-alive
ETag: "5d14a80f-39713"
Accept-Ranges: bytes

测试带非法http_refer:
[root@nginx-server nginx]# curl -e http://www.baidu.com -I "http://10.0.105.202/test.jpg"
HTTP/1.1 403 Forbidden
Server: nginx/1.16.0
Date: Thu, 27 Jun 2019 16:22:32 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive

测试带合法的http_refer:
[root@nginx-server nginx]# curl -e http://10.0.105.202 -I "http://10.0.105.202/test.jpg"
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Thu, 27 Jun 2019 16:23:21 GMT
Content-Type: image/jpeg
Content-Length: 27961
Last-Modified: Thu, 27 Jun 2019 12:28:51 GMT
Connection: keep-alive
ETag: "5d14b683-6d39"
Accept-Ranges: bytes

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值