用Nginx TCP反向代理作mail邮件代理

服务器
Nginx功能强大,此文也是给想使用TCP反向代理的朋友作个示例参考吧。
  • 1. 背景
  • 2. Nginx安装(包括nginx_upstream_check_module)
  • 3. Nginx配置
  • 4. 总结

1. 背景

新版本nginx有TCP反向代理功能,nginx的mail proxy配置认证又太麻烦,于是就想用TCP反向功能作mail代理。

2. Nginx安装(包括nginx_upstream_check_module)

[[202493]]

  1. cd /tmp 
  2. tar zxf pcre-8.35.tar.gz 
  3. cd pcre-8.35/ 
  4. ./configure --prefix=/usr/local/pcre 
  5. make 
  6. make install 
  7.  
  8. cd /tmp 
  9. tar zxf openssl-1.0.2g.tar.gz 
  10. cd openssl-1.0.2g/ 
  11. ./config enable-tl***t 
  12. make 
  13. make install 
  14. mv -f /usr/bin/openssl /usr/bin/openssl.old 
  15. mv -f /usr/include/openssl /usr/include/openssl.old 
  16. ln -sf /usr/local/ssl/bin/openssl /usr/bin/openssl 
  17. ln -sf /usr/local/ssl/include/openssl /usr/include/openssl 
  18.  
  19. cd /tmp 
  20. git clone git@github.com:yaoweibin/nginx_upstream_check_module.git 
  21.  
  22. cd /tmp 
  23. tar zxf nginx-1.13.4.tar.gz 
  24. cd nginx-1.13.4/ 
  25. patch -p1 < ../nginx_upstream_check_module/check_1.12.1+.patch 
  26. ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-stream=dynamic --with-stream_ssl_module --with-pcre=../pcre-8.35 --with-http_ssl_module --with-openssl=../openssl-1.0.2g --add-module=../nginx_upstream_check_module 
  27. make 
  28. make install 

3. Nginx配置

  1. cat nginx.conf 
  2.  
  3. user  www; 
  4. worker_processes  8; 
  5.  
  6. error_log  logs/info.log  info; 
  7.  
  8. #pid        logs/nginx.pid; 
  9.  
  10. load_module modules/ngx_stream_module.so;  # 此处要添加模块 
  11.  
  12. events { 
  13.     worker_connections  1024; 
  14.  
  15. http { 
  16.     include       mime.types; 
  17.     default_type  application/octet-stream; 
  18.  
  19.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
  20.     #                  '$status $body_bytes_sent "$http_referer" ' 
  21.     #                  '"$http_user_agent" "$http_x_forwarded_for"'
  22.  
  23.     #access_log  logs/access.log  main; 
  24.  
  25.     sendfile        on
  26.     #tcp_nopush     on
  27.  
  28.     #keepalive_timeout  0; 
  29.     keepalive_timeout  65; 
  30.  
  31.     #gzip  on
  32.  
  33.     #设定请求缓冲   
  34.     server_names_hash_bucket_size 128;   
  35.     client_header_buffer_size 32k;   
  36.     large_client_header_buffers 4 32k;   
  37.     client_max_body_size 300m;   
  38.     #sendfile on;   
  39.     tcp_nopush     on;   
  40.     #keepalive_timeout 60;   
  41.     tcp_nodelay on;   
  42.     server_tokens off;   
  43.     client_body_buffer_size 512k;   
  44.     proxy_connect_timeout   20;   
  45.     proxy_send_timeout      60;   
  46.     proxy_read_timeout      20;   
  47.     proxy_buffer_size       16k;   
  48.     proxy_buffers           4 64k;   
  49.     proxy_busy_buffers_size 128k;   
  50.     proxy_temp_file_write_size 128k;    
  51.     client_header_timeout  3m;   
  52.     client_body_timeout    3m;   
  53.     send_timeout           3m;   
  54.  
  55.  
  56.     gzip on;#开启gzip,节省带宽   
  57.     gzip_min_length  1100;   
  58.     gzip_buffers     4 8k;   
  59.     gzip_types       text/plain text/css application/x-javascript image/bmp application/javascript;      
  60.  
  61.     output_buffers   1 32k;   
  62.     postpone_output  1460;   
  63.  
  64.     limit_rate_after 3m;#限速模块,前3M下载时不限速   
  65.     limit_rate 512k; #限速模块    
  66.  
  67.  
  68. include vhost/*.conf; 
  69.  
  70.  
  71. stream { 
  72. include stream/*.conf; 
  73. cat stream/mail_pro.conf 
  74.  
  75. ######### TCP 反向代理负载均衡设置 ############### 
  76. upstream mailsmtp_pro { 
  77.         server smtp.mxhichina.com:25; 
  78.  
  79. server { 
  80.         listen 25; # 对外提供服务TCP监听 
  81.         proxy_connect_timeout 5s; 
  82.         proxy_timeout 5s; 
  83.         proxy_pass mailsmtp_pro; 

4. 总结

Nginx功能强大,此文也是给想使用TCP反向代理的朋友作个示例参考吧。

责任编辑:武晓燕 来源: Linux社区
相关推荐

2022-07-01 07:33:24

nginx反向代理测试

2020-10-22 08:05:46

Nginx

2019-06-19 15:34:39

Nginx反向代理负载均衡

2018-11-12 12:17:00

2023-12-05 09:14:54

2014-04-29 14:54:48

Nginx反向代理

2020-08-06 08:23:24

Nginx反向代理Web安全

2023-09-13 07:16:31

Ngnix代理服务器

2016-09-07 18:57:48

2023-09-08 00:07:41

2017-12-18 12:04:02

Nginx代理均衡

2019-11-04 15:35:53

Nginx反向代理负载均衡

2018-01-10 10:15:48

NginxIP问题

2019-09-18 10:39:08

负载均衡反向代理TCP

2012-12-07 10:14:48

Nginx负载均衡

2010-03-29 17:56:20

Nginx反向代理

2011-08-30 11:32:53

UbuntuNginx

2019-08-26 10:31:12

正向代理反向代理安全

2021-07-29 11:15:25

Nginx网络服务器

2024-04-02 12:36:01

点赞
收藏

51CTO技术栈公众号