Nginx1.9.xx以后版支持4层tcp转发

Centos 6.10


# 安装epel源
yum -y install epel-release
# 安装Nginx
yum -y install nginx

nginx -V

  再次确认一下模块

[root@localhost ~]# grep modules /etc/nginx/nginx.conf
include /usr/share/nginx/modules/*.conf;
[root@localhost ~]# ls /usr/share/nginx/modules/
mod-http-geoip.conf mod-http-image-filter.conf mod-http-perl.conf mod-http-xslt-filter.conf mod-mail.conf mod-stream.conf
[root@localhost ~]# more /usr/share/nginx/modules/mod-stream.conf
load_module "/usr/lib64/nginx/modules/ngx_stream_module.so";
[root@localhost ~]# ls /usr/lib64/nginx/modules/

[root@localhost conf.d]# more /etc/nginx/nginx.conf
user nginx; worker_processes
4; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; worker_rlimit_nofile 65520; include /usr/share/nginx/modules/*.conf; events { use epoll; worker_connections 10240; }
# 4层TCP转发配置文件,本地的3306端口转发到192.168.1.229的3306端口 stream { server { listen 3306; proxy_connect_timeout 30m; proxy_timeout 60m; proxy_pass 192.168.1.229:3306; } }
# 7层http转发配置文件 http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; #gzip 压缩传输 gzip on; gzip_min_length 1k; #最小1K gzip_buffers 16 64K; #gzip_http_version 1.1; gzip_comp_level 6; gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png; gzip_vary on; include /etc/nginx/mime.types; default_type application/octet-stream; proxy_intercept_errors on; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_buffering off; client_max_body_size 20m; client_body_buffer_size 10240k; proxy_connect_timeout 120; proxy_read_timeout 120; proxy_send_timeout 120; proxy_buffer_size 3096k; proxy_buffers 8 800k; proxy_busy_buffers_size 4096k; proxy_temp_file_write_size 4096k; # Load modular configuration files from the /etc/nginx/conf.d directory. # See
http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; }

!!!请注意!!!stream配置不能放到http内,即不能放到/etc/nginx/conf.d/,因为stream是通过tcp层转发,而不是http转发。

!!!请注意!!!stream配置不能放到http内,即不能放到/etc/nginx/conf.d/,因为stream是通过tcp层转发,而不是http转发。

!!!请注意!!!stream配置不能放到http内,即不能放到/etc/nginx/conf.d/,因为stream是通过tcp层转发,而不是http转发。

如配置在http内,启动nginx会报如下错误:

nginx: [emerg] "server" directive is not allowed here
# 检查nginx服务

nginx -t

# 启动Nginx 服务

service nginx start
原文地址:https://www.cnblogs.com/eos666/p/12509337.html