1,安裝nginx,stream模塊默認(rèn)不安裝的,需要手動(dòng)添加參數(shù):--with-stream,官方下載地址:download,根據(jù)自己系統(tǒng)版本選擇nginx1.9或以上版本。
2,nginx.conf 配置,參考說(shuō)明:ngx_stream_core_module
請(qǐng)注意,stream配置不能放到http內(nèi),因?yàn)閟tream是通過(guò)tcp層轉(zhuǎn)發(fā),而不是http轉(zhuǎn)發(fā)。
如配置在http內(nèi),啟動(dòng)nginx會(huì)報(bào)如下錯(cuò)誤:
nginx: [emerg] "server" directive is not allowed here
例子:
stream {
? ? # 添加socket轉(zhuǎn)發(fā)的代理
? ? upstream socket {
? ? ? ? hash $remote_addr consistent;
? ? ? ? # 轉(zhuǎn)發(fā)的目的地址和端口
? ? ? ? server 127.0.0.1:3306 weight=5 max_fails=3 fail_timeout=30s;
? ? }
? ? server {
? ? ? listen 3000;
? ? ? proxy_connect_timeout 1s;
? ? ? proxy_timeout 3s;
? ? ? proxy_pass socket;
? ? }
}
可以把配置直接放到nginx.conf的最后,不要在http里。