15_Nginx反向代理、Nginx的TCP/UDP调度器、Nginx常见问题处理

proxy  10.10.11.10
client 10.10.11.11
web1   10.10.11.12
web2   10.10.11.13

1.nginx反向代理
  使用Nginx实现Web反向代理功能,实现如下功能:
  后端Web服务器两台,使用httpd实现

1.1 配置两台web服务器,首页页面分别为:web1,web2
]# yum -y install httpd
]# systemctl start httpd
]# systemctl enable httpd
web1
]# echo "web1" > /var/www/html/index.html
web2
]# echo "web2" > /var/www/html/index.html
web1测试:
]# firefox 10.10.11.12
]# firefox 10.10.11.13
 
1.2 配置proxyHttp调度
]# vim /etc/nginx/nginx.conf
.. ..
http {
.. ..
#使用upstream定义后端服务器集群,集群名称任意(如webserver)
#使用server定义集群中的具体服务器和端口
upstream webserver {
       server 10.10.11.12:80;
       server 10.10.11.13:80;
        }
.. ..

]# vim /etc/nginx/conf.d/default.conf
server {
        listen     80;
        server_name  www.a.com;
#通过proxy_pass将用户的请求转发给webserver集群
        location / {
            proxy_pass http://webserver;
        }
...
]# nginx -s reload

client测试:(默认轮询)
]# curl http://10.10.11.10
web1
]# curl http://10.10.11.10
web2
]# curl http://10.10.11.10
web1
]# curl http://10.10.11.10
web2
 
1.3 配置upstream服务器集群池属性
设置失败次数,超时时间,权重
weight可以设置后台服务器的权重,
max_fails可以设置后台服务器的失败次数,
fail_timeout可以设置后台服务器的失败超时时间。
]# vim /etc/nginx/nginx.conf
http...
upstream webserver {
  server 10.10.11.12 weight=1 max_fails=1 fail_timeout=30;
  server 10.10.11.13 weight=2 max_fails=2 fail_timeout=30;
  server 10.10.11.14 down;
 }

#weight设置服务器权重值,默认值为1
#max_fails设置最大失败次数
#fail_timeout设置失败超时时间,单位为秒
#down标记服务器已关机,不参与集群调度

1.3.1 client测试权重:
]# curl http://10.10.11.10
web1
]# curl http://10.10.11.10
web2
]# curl http://10.10.11.10
web2
]# curl http://10.10.11.10
web1
]# curl http://10.10.11.10
web2
]# curl http://10.10.11.10
web2

1.3.2 关闭一台后端服务器(如web1)
]# systemctl stop httpd
client测试轮询(只有web2)

开启web1测试轮询
]# systemctl start httpd (正常)
 
1.4 配置upstream服务器集群的调度算法
]# vim /etc/nginx/nginx.conf
http...
upstream webserver {
#通过ip_hash设置调度规则为:相同客户端访问相同服务器
   ip_hash;
   server 10.10.11.12 weight=1 max_fails=2 fail_timeout=10;
   server 10.10.11.13 weight=2 max_fails=2 fail_timeout=10;
server...
]# nginx -s reload

client测试:(只有web2)
]# curl http://10.10.11.10
web2
]# curl http://10.10.11.10
web2
 
2.Nginx的TCP/UDP调度器(源码安装的时候要with-stream)
# vim /etc/nginx/nginx.conf
...
stream {
  upstream backend {
    server 10.10.11.12:22;
    server 10.10.11.13:22;
     }
  server {
    listen 12345;
    proxy_connect_timeout 1s;
    proxy_timeout 3s;           # ssh登陆之后多久退出
    proxy_pass backend;
     }
}
http...
]# nginx -s reload

client检测:
]# vim /etc/ssh/ssh_config
   StrictHostKeyChecking no
   UserKnownHostsFile /dev/null
]# systemctl restart sshd

]# ssh 10.10.11.10 -p 12345
[email protected]'s password:
web1 ~]#

]# ssh 10.10.11.10 -p 12345
[email protected]'s password:
web1 ~]#
 
3.Nginx常见问题处理(将nginx还原,初始化安装状态)
3.1 修改Nginx配置文件,自定义报错页面

client: 访问一个不存在的页面
]# firefox http://10.10.11.10/xxxxx

proxy本机:
]# vim /etc/nginx/conf.d/default.conf
...
 error_page  404  /404.html;(去掉#)
...
]# echo "Lossing..." > /usr/share/nginx/html/404.html
]# firefox 10.10.11.10/txxxxxx(查看效果)
**********************
常见http状态码:
200 一切正常
301 永久重定向
302 临时重定向
401 用户名或密码错误
403 禁止访问(客户端IP地址被拒绝)
404 文件不存在
414 请求头过长
500 服务器内部错误
502 Bad Gateway
*************************

3.2 修改Nginx配置文件,定义状态页面,查看状态页面信息
需要:--with-http_stub_status_module //开启status状态页面
]# vim /etc/nginx/conf.d/default.conf
...
location /status {
   stub_status on;
   #allow IP地址;
   #deny IP地址;
}
...
]# nginx -s reload
***************************
ss命令可以查看系统中启动的端口信息,该命令常用选项如下:
-a显示所有端口的信息
-n以数字格式显示端口号
-t显示TCP连接的端口
-u显示UDP连接的端口
-l显示服务正在监听的端口信息,如httpd启动后,会一直监听80端口
-p显示监听端口的服务名称是什么(也就是程序名称)
注意:在RHEL7系统中可以使用ss命令替代netstat命令,功能一样,选项一样。
**************************
优化后,查看状态页面信息(本机)
]# curl http://10.10.11.10/status
Active connections: 1
server accepts handled requests
 2 2 2
Reading: 0 Writing: 1 Waiting: 0
****************************
Active connections:当前活动的连接数量。
Accepts:已经接受客户端的连接总数量。
Handled:已经处理客户端的连接总数量。
(一般与accepts一致,除非服务器限制了连接数量)。
Requests:客户端发送的请求数量。
Reading:当前服务器正在读取客户端请求头的数量。
Writing:当前服务器正在写响应信息的数量。
Waiting:当前多少客户端在等待服务器的响应。
*******************************

3.3 优化Nginx并发量
3.3.1 ab高并发测试
]# yum -y install httpd-tools
]# ab -n 2000 -c 2000 http://10.10.11.10/
...
socket: Too many open files (24)
3.3.2 修改Nginx配置文件,增加并发量
]# vim /etc/nginx/nginx.conf
.. ..
worker_processes  2; # 与CPU核心数量一致
events {
worker_connections 65535; # 每个worker最大并发连接数
use epoll;
}
.. ..
]# nginx -s reload

优化Linux内核参数(最大文件数量)
临时修改:
]# ulimit -a         # 查看所有属性值
]# ulimit -Hn 100000 # 设置硬限制(临时规则)
]# ulimit -Sn 100000 # 设置软限制(临时规则
]# ab -n 2000 -c 2000 http://10.10.11.10/
永久修改:
]# vim /etc/security/limits.conf
  .. ..
#用户或组  硬限制或软限制  需要限制的项目 限制的值
*  soft nofile 100000
*  hard nofile 100000
测试:
]# ab -n 99999 -c 2000 http://10.10.11.10/
]# curl http://10.10.11.10/status
Active connections: 1
server accepts handled requests
 285718 285718 282548
Reading: 0 Writing: 1 Waiting: 0
 
4. 优化Nginx数据包头缓存
问题:414 Request-URI Too Large
]# vim /etc/nginx/nginx.conf
.. ..
http {
     # 默认请求包头信息的缓存    
    client_header_buffer_size    1k;
     # 最大请求包头部信息的缓存个数与容量,生产环境4k足以
    large_client_header_buffers  4 6m;
.. ..
]# nginx -s reload
 
5.浏览器本地缓存静态数据

查看firefox缓存信息
浏览器输入:about:cache

清空全部历史记录后
]# vim /etc/nginx/conf.d/default.conf
新增加一个location
location...
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
    expires  30d; //定义客户端静态缓存时间为30天,一定要有;号
}
...
]# nginx -s reload

client访问:(先有1.png)
]# firefox http://10.10.11.10/1.png
查看图片缓存
 
6.日志切割
]# vim /usr/share/nginx/logbak.sh
#!/bin/bash
date=`date +%Y%m%d`
logpath=/var/log/nginx
mv $logpath/access.log $logpath/access-$date.log
mv $logpath/error.log $logpath/error-$date.log
kill -USR1 $(cat /run/nginx.pid)
]# crontab -e
03 03 * * 5  /usr/share/nginx/logbak.sh
 
7.对页面进行压缩处理
]# vim /etc/nginx/nginx.conf
   http {...
   gzip on;               # 开启压缩
   gzip_min_length 1000;  # 小文件不压缩
   gzip_comp_level 4;     # 压缩比率
    # 对特定文件压缩,类型参考mime.types
   gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
]# nginx -s reload
 
8.服务器内存缓存
]# vim /etc/nginx/nginx.conf
http {
...
open_file_cache  max=2000  inactive=20s;
    open_file_cache_valid    60s;
    open_file_cache_min_uses 5;
    open_file_cache_errors   off;
# 设置服务器最大缓存2000个文件句柄,关闭20秒内无请求的文件句柄
# 文件句柄的有效时间是60秒,60秒后过期
# 只有访问次数超过5次会被缓存
]# nginx -s reload

猜你喜欢

转载自www.cnblogs.com/luwei0915/p/12154068.html