0

2334

Tcp“长连接”实战中的总结

因开发公司“灯控中转服务”(穿透客户端网络限制)过程中,用了tcp长连接,持续的踩坑,慢慢的对这种 网络多变网络不稳定 环境下的Tcp服务通信,有了更加清晰的认识,现总结一下。

一、理解:tcp是面向链接的,http是无状态的

tcp是建立在ip链接上,没有链接就没有通信。

http当然也是建立在tcp的基础上,但因为常用的是短链接,即链即用,所谓无状态是指“身份标识”,tcp也无身份标志,这都要靠协议自身的约定来保证,比如http为了身份状态识别,client端有cookie,server端有session,client在每次请求的报文中,都带上了cookie让server端能识别。

二、tcp在静默状态下,对链路状态无感知

tcp是面向链接的,但是,链接依赖的链路断了(也就是ip层),tcp两端其实并不会知道,只有当他们之间有报文往来的时候,才会发现报文无法到达对方(所依赖的网络链路已经发生变化)

实验:

对链接上服务的两端,中途快速把网线拔掉,然后迅速插上,此时双方ip未发送变化,服务两端的链接依然能通信~

……

乐果   发表于   2017 年 11 月 02 日 标签:tcp 继续阅读

0

3672

nginx配置中rewrite的坑

一、rewrite基本知识

在server块下,会优先执行rewrite部分,然后才会去匹配location块 server中的rewrite break和last没什么区别,都会去匹配location,所以没必要用last再发起新的请求,可以留空 location中的rewirte:

不写last和break - 那么流程就是依次执行这些rewrite

  1. rewrite break - url重写后,直接使用当前资源,不再执行location里余下的语句,完成本次请求,地址栏url不变
  2. rewrite last - url重写后,马上发起一个新的请求,再次进入server块,重试location匹配,超过10次匹配不到报500错误,地址栏url不变
  3. rewrite redirect – 返回302临时重定向,地址栏显示重定向后的url,爬虫不会更新url(因为是临时)
  4. rewrite permanent – 返回301永久重定向, 地址栏显示重定向后的url,爬虫更新url 使用last会对server标签重新发起请求

如果location中rewrite后是对静态资源的请求,不需要再进行其他匹配,一般要使用break或不写,直接使用当前location中的数据源,完成本次请求 如果location中rewrite后,还需要进行其他处理,如动态fastcgi请求(.php,.jsp)等,要用last继续发起新的请求 (根的location使用last比较好, 因为如果有.php等fastcgi请求还要继续处理) 使用alias指定源:必须使用last

……

乐果   发表于   2017 年 10 月 30 日 标签:nginx 继续阅读

0

2606

Ubuntu系统下源码安装MJPG-Streamer及使用

一、获取源码及编译

$sudo apt-get install libjpeg-dev subversion imagemagick  
$ svn checkout svn://svn.code.sf.net/p/mjpg-streamer/code/ mjpg-streamer-code  
$ cd mjpg-streamer/mjpg-streamer  
$ make clean all  

二、运行

$ export LD_LIBRARY_PATH=.
$ ./mjpg_streamer -i "input_uvc.so -y -d /dev/video0" -o "output_http.so -w ./www"

想要查看视频,请在web页面中输入地址:http://127.0.0.1:8080

三、安装

如果需要安装该应用,执行以下指令:

sudo make DESTDIR=/usr install

……

乐果   发表于   2017 年 10 月 27 日 标签:uvclinuxubuntu 继续阅读

0

4639

nginx+lua动态修改upstream实现动态代理

家里有台迷你主机作为家庭服务器,但是在公司或者其他地方想实时访问,以前常有的解决办法是用域名动态dns解析即 “花生壳” 来实现的,但问题是当家里的宽带ip发生变化时,“花生壳”虽然已经更新了dns的解析,但由于下游dns服务缓存的影响,生效会延迟,这样就存在临界时间段无法访问的问题。

最近,学习nginx+lua实现动态代理,即在外网服务器上搭一个代理,当家里ip发生改变时通知外网的代理服务;用户访问外网代理服务,间接访问家庭服务器时,已经知道ip发生变化了,这样即实现了无缝隙间断的访问~

nginx配置案例如下:

lua_shared_dict _ups_zone 64m;

server {
        listen       80;
        server_name  test.cn;
        access_log  off;

        location = /ups_update {
                default_type text/plain;
                content_by_lua '
                        local ups = ngx.req.get_uri_args()["ups"]
                        if ups == nil then
                                ngx.say("usage: /ups_update?ups=x.x.x.x")
                                return
                        end
                        local host = ngx.var.http_host
                        local ups_from = ngx.shared._ups_zone:get(host);
                        --ngx.log(ngx.WARN, host, " update upstream from ", ups_from, " to ", ups)
                        ngx.shared._ups_zone:set(host, ups);
                        ngx.say(host, " update upstream from ", ups_from, " to ", ups)
                ';
        }

        location / {
                set_by_lua $cur_ups '
                        local ups = ngx.shared._ups_zone:get(ngx.var.http_host)
                        if ups ~= nil then
                                --ngx.log(ngx.ERR, "get [", ups, "] from ngx.shared._ups_zone")
                                return ups
                        end
                '
                proxy_next_upstream off;
                proxy_set_header    Host $host;
                proxy_http_version  1.1;
                proxy_set_header    Connection  "";
                proxy_pass $scheme://$cur_ups;
        }
}

乐果   发表于   2017 年 09 月 30 日 标签:nginxlua 继续阅读

0

2521

lua_nginx_module常见使用文档

安装lua_nginx_module 模块

lua_nginx_module 可以一步步的安装,也可以直接用淘宝的OpenResty

Centos和debian的安装就简单了。。

这里说下freebsd的安装:

fetch http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
make freebsd
make install
cd ..
fetch https://github.com/chaoslawful/lua-nginx-module/zipball/v0.1.6rc2
fetch https://github.com/simpl/ngx_devel_kit/zipball/v0.2.17rc2
tar zxvf v0.1.6rc2
mv chaoslawful-lua-nginx-module-ccaf132 lua_nginx_module
tar zxvf v0.2.17rc2
mv simpl-ngx_devel_kit-bc97eea ngx_devel_kit
tar zxvf pcre-8.12.tar.gz
tar zxvf nginx-1.0.3.tar.gz
cd nginx-1.0.3
./configure --prefix=/data/soft/nginx --with-pcre=../pcre-8.12 --add-module=../ngx_devel_kit --add-module=../lua_nginx_module
make && make install

安装完成后,我们体验一下lua

第一个lua脚本

ngx.say 是打印的打印输出的意思。。。

location /echo {
    default_type text/plain;
    echo hello lua;
}
location /lua {
    default_type text/plain;
    content_by_lua 'ngx.say("hello world")';
}

用lua脚本做nginx的访问的限制…

……

乐果   发表于   2017 年 09 月 29 日 标签:luanginx 继续阅读

较旧的文章 较新的文章
热评文章