您的位置:首页 > 运维架构 > Nginx

nginx代理服务器,nginx四层调度,nginx的常见问题处理及优化

2020-07-13 04:17 531 查看

nginx代理服务器,nginx四层调度,nginx的常见问题处理及优化

nginx代理服务器
    负载均衡
    健康检查
                          ---web1服务器
用户---nginx代理服务器---|
                          ---web2服务器
一、部署web1和web2的服务器的实验环境
web1:
[root@web1 ~]# yum -y install httpd
[root@web1 ~]# echo 192.168.2.100 >/var/www/html/index.html
[root@web1 ~]# systemctl restart httpd
web2:
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# echo 192.168.2.200 >/var/www/html/index.html
[root@web2 ~]# systemctl restart httpd
二、部署nginx代理服务器
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf        ==>>在是server上添加如下内容
upstream webserver{                ==>>使用upstream定义后端服务器集群,服务器群的名称任意(webserver)
  server 192.168.2.100:80;         ==>>server定义具体服务器IP及端口
  server 192.168.2.200:80;
}
server{
        listen  80;
        server_name www.a.com;
        location / {
          proxy_pass http://webserver;        ==>>通过proxy_pass将用户的请求转发给webserver集群
}
}
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
加权轮询,失败时操作
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
upstream webserver{
  server 192.168.2.100:80 weight=2 max_fails=1 fail_timeout=20;   ==>>加权轮询weight,失败一次20秒内不在访问。
  server 192.168.2.200:80 weight=2;
}
server{
        listen  80;
        server_name www.a.com;
        location / {
         proxy_pass http://webserver;
          root www;
          index index.html index.htm;
}
}

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

nginx四层调度(四层反向代理)tcp/udp的调度器:
1.安装nginx加上TCP/UDP代理模块--with-stream
[root@proxy nginx-1.11.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-stream
[root@proxy nginx-1.11.2]# make && make install
2.修改配置文件
[root@proxy nginx]# cd /usr/local/nginx/conf/
[root@proxy conf]# vim nginx.conf             ==>>在http上面添加下面内容
stream{
    upstream backupssh{
      server 192.168.2.100:22;            ==>>后端ssh服务器的IP与端口号
      server 192.168.2.200:22;
}
    server{
      listen 12345;                     ==>>不要使用22与本机冲突
      proxy_pass backupssh;
}
}
[root@proxy conf]# /usr/local/nginx/sbin/nginx
测试时使用-p加端口
[root@client ~]# ssh  192.168.4.5 -p 12345
-------------------------------------------------------------------------------------
nginx的常见问题处理及优化

1.自定义报错
[root@proxy conf]# vim nginx.conf
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;                             ==>>支持中文
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page  404              /404.html;           ==>>出现404代码跳转到404.html文件
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

2.查看服务器的状态信息
安装nginx开启status状态模块--with-http_stub_status_module   
[root@proxy nginx-1.11.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-stream --with-http_stub_status_module
[root@proxy nginx-1.11.2]# make && make install
[root@proxy nginx-1.11.2]# cd /usr/local/nginx/conf/
[root@proxy conf]# vim nginx.conf
    server {
        listen       80;
        server_name  localhost;
        location /status {               ==>>插入这三行  开启状态页面
           stub_status on;
           allow 192.168.4.5;           ==>>允许ip
           deny 192.168.4.123;          ==>>拒绝IP
        }
        location / {
            root   html;
            index  index.html index.htm;
        }
}

3.优化nginx的并发数量
[root@proxy conf]# vim nginx.conf
worker_processes  1;                ==>>与CPU核心数量一致,lscpu查看CPU(s)内核数     
events {
    worker_connections  10000;
}
[root@proxy conf]# ulimit -a                 ==>>查看所有属性
[root@proxy conf]# ulimit -Hn 100000         ==>>设置硬限制(临时规则)不可超过
[root@proxy conf]# ulimit -Sn 100000         ==>>查看软限制(临时规则)达到时警告

4.优化nginx数据包头缓存
[root@proxy conf]# vim nginx.conf
http {
    client_header_buffer_size 1k;          ==>>默认请求包头信息的缓存
    large_client_header_buffers 4 4k;      ==>>大请求包头部信息的缓存个数与容量
}

5.本地缓存
[root@proxy conf]# vim nginx.conf
    server {
        listen       80;
        server_name  localhost;
        location /status {
           stub_status on;
        }
        location / {
            root   html;
            index  index.html index.htm;
        }
location ~* \.(jpg|gif|png|jpeg|css|js|ico|xml)$ {
   expires      30d;                                   ==>>定义客户端缓存时间为30天
}
}


 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: