nginx tcp代理_Nginx安装和使用

Nginx

Nginx是一个俄罗斯人开发的一款开源的工具,可作为HTTP服务器、反向代理服务器和邮件服务器等。以稳定性、功能集、示例配置文件和低系统资源消耗等特点,被全球12%的网站选用--------其实是我今天发现我的电脑莫名的在向一些未知的IP发送80/443端口的报文,所以去问候了一下这些IP, 发现回的错误消息里面都带了nginx的字样,所以就好奇查了一下,顺便学习记录一下。

  • HTTP服务器

就是Web服务器,apache, IIS这些提供Web站点服务软件和设备.

  • 反向代理服务器

搞清楚反向前,先弄清楚正向代理。

所谓正向代理可以这么理解:古时候找媳妇除了强抢民女外就只能靠媒婆,作为唯一的婚介中心,媒婆这个代理靠着一张嘴能左右你的余生------张飞是个明白人,知道把命运掌握在自己的手上,去抢。 有请人帮忙带小纸条而最后忧桑地发现他们走到一起的小朋友就知道: 代理有风险,慎入!

而反向代理就很简单了。写了一天代码的你疲惫不堪、浑身皮痒,来到按摩中心:妈妈桑,把你们这手重的妹妹来一打,好好蹂躏一下我。这个妈妈桑就是反向代理,你其实并不关心谁在为你服务,你知道妈妈桑能提供你需要的服务。

Nginx安装

  • 安装步骤
#安装库文件准备
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
#获得最新的nginx并解压
wget https://nginx.org/download/nginx-1.9.9.tar.gz 
tar -zxvf nginx-1.9.9.tar.gz
#编译安装
cd nginx-1.9.9
./configure --prefix=/opt/nginx
make
make install
#检查nginx是否安装成功
cd /opt/gninx/sbin
[root@172-12-0-100 sbin]# ./nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
  • 配置nginx作为Web Server
vim /opt/nginx/conf/nginx.conf

启动nginx

/opt/gninx/sbin/nginx

放开防火墙端口

firewall-cmd --permanent --add-port=8080/tcp
systemctl restart firewalld.service

编辑一个index.html文件放到/opt/html/目录下,使用浏览器测试该站点

使用webbench测试一下该站点的并发能力

webbench -c 5000 -t 120 http://10.10.11.250:8080/
1625a8638c0b2ab10b0333a5b80f1757.png

#部分失败的原因是linux下的syn并发限制所致

将nginx设置为自举服务

创建并编辑nginx.service文件

vim /lib/systemd/system/nginx.service

输入:

[Unit]

Description=nginx

After=network.target

[Service]
Type=forking
ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/opt/nginx/sbin/nginx -s quit
PrivateTmp=true
 [Install]
WantedBy=multi-user.target

开启服务:

systemctl enable nginx

#停止进程启动方式

/opt/nginx/sbin/nginx -s stop

#服务方式启动

systemctl start nginx.service
  • 内核参数优化:

net.ipv4.tcp_fin_timeout = 2

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_keepalive_time = 600

net.ipv4.ip_local_port_range = 4000 65000

net.ipv4.tcp_max_syn_backlog = 16384

net.ipv4.tcp_max_tw_buckets = 36000

net.ipv4.route.gc_timeout = 100

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_synack_retries = 1

net.core.somaxconn = 16384

net.core.netdev_max_backlog = 16384

net.ipv4.tcp_max_orphans = 16384

#以下参数是对iptables防火墙的优化,防火墙不开会提示,可以忽略不理。

net.ipv4.ip_conntrack_max = 25000000

net.ipv4.netfilter.ip_conntrack_max=25000000

net.ipv4.netfilter.ip_conntrack_tcp_timeout_established=180

net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait=120

net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait=60

net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait=120

配置nginx为负载均衡器

编辑配置文件,增加需要负载均衡的Web Server

  • 轮询模式
http {
    upstream mywebs {
        server 10.10.11.247:80;
        server 10.10.11.246:80;
     } 
    server {
         listen 8081;
         location / {
           proxy_pass http://mywebs;
         }
}

防火墙放开8081端口访问

firewall-cmd --permanent --add-port=8081/tcp
systemctl restart firewalld.service

浏览器进行测试,发现流量重定向到了不同的Server

上述配置为轮训方式,即一个请求发现server1, 接着发给server2再server1,如此轮训

  • 配置热备Server
a08c4d1bf7476bbeda43364040a24628.png

这时,请求始终送往10.10.11.246:

4ac21cf605e0f40c21c11d814f26fb54.png

停止246上的apache服务:

/etc/init.d/apache2 stop

请求立刻送往247:

60bf68c03907b5f673041151afd16261.png
  • 配置权重负载均衡
upstream mysvr {
      server 10.10.11.246:80 weight=1;
      server 10.10.11.247:80 weight=2;
}
  • 相同的客户端IP请求同一个服务器
upstream mysvr {
      server 10.10.11.246:80;
      server 10.10.11.247:80;
      ip_hash;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值