nginx socket转发设置
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; } }