1.添加依賴模塊,如下
--with-stream --with-stream_ssl_module
2.nginx.conf 配置,參考說明:ngx_stream_core_module
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { ................. } # tcp層轉發的配置文件夾 include /etc/nginx/tcp.d/*.conf;
請注意,stream配置不能放到http內,即不能放到/etc/nginx/conf.d/,因為stream是通過tcp層轉發,而不是http轉發。
如配置在http內,啟動nginx會報如下錯誤:
nginx: [emerg] "server" directive is not allowed here
3.在tcp.d下新建個bss_num_30001.conf文件,內容如下:
stream {
#添加socket轉發的代理 upstream proxy_forward { hash $remote_addr consistent; server 139.196.172.xx:80; #server 139.224.18.xx:80; #ip_hash; } #提供轉發服務,即訪問forward.xxx.cn:80,並配置好請求頭,會調整至proxy_forward指定的地址 server { #listen 127.0.0.1:80; listen forward.xxx.cn:80; proxy_pass xdaili_forward; } }