Nginx的TCP反向代理设置

Posted qinjinhua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx的TCP反向代理设置相关的知识,希望对你有一定的参考价值。

转载自我的新浪博客:​​http://blog.sina.com.cn/s/blog_4fa20e8c0102vhbr.html​

Centos 6.6的64位系统,我的nginx软件在/srv下,会装到/usr/local/nginx目录

1,下载nginx之后解压

2,yum -y install git  patch openssl openssl-devel

3,git clone ​​https://github.com/yaoweibin/nginx_tcp_proxy_module.git​​ (如果你已经有这个模块就省略这个步骤了,git 也不用安装了)

4,cd /srv/nginx.1.6.2

     patch -p1 < ./nginx_tcp_proxy_module/tcp.patch

   ./configure --prefix=/usr/local/nginx --add-module=./nginx_tcp_proxy_module/

   make && make install

5,tcp的模块和http是同级的,这是我的nginx.conf

 user nobody;

worker_processes 4;

events  worker_connections 768; # multi_accept on;

tcp    

upstream app_server            

server 域名或主机:端口号;        

check interval=3000 rise=2 fall=5 timeout=1000;

        #check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello;

        #check interval=3000 rise=2 fall=5 timeout=1000 type=http;            #check_http_send "GET / HTTP/1.0\\r\\n\\r\\n";       

#check_http_expect_alive http_2xx http_3xx;  

server        

listen 80;       

proxy_pass app_server;   

  

http

 ## # Basic Settings ##

 sendfile on; 

tcp_nopush on; 

tcp_nodelay on; 

keepalive_timeout 65; 

types_hash_max_size 2048; 

# server_tokens off;       

 large_client_header_buffers 128 2048k;

 # server_names_hash_bucket_size 64; 

# server_name_in_redirect off;

 include /usr/local/nginx/conf/mime.types; 

default_type application/octet-stream;

 ## 

# Logging Settings ##

 access_log /var/log/nginx/access.log; 

error_log /var/log/nginx/error.log;

 ## # Gzip Settings 

##

 gzip on; 

gzip_disable "msie6";

 gzip_vary on; 

# gzip_proxied any; 

gzip_comp_level 6;       

gzip_buffers 16 8k;       

gzip_http_version 1.1; 

gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/octet-stream application/x-shockwave-flash;

 ## # nginx-naxsi config ## 

# Uncomment it if you installed nginx-naxsi ##

 #include /etc/nginx/naxsi_core.rules;

 ## 

# nginx-passenger config 

## 

# Uncomment it if you installed nginx-passenger ##  #passenger_root /usr; #passenger_ruby /usr/bin/ruby;

 ## 

# Virtual Host Configs 

##

 include /etc/nginx/conf.d/*.conf; 

include /etc/nginx/sites-enabled/*;

        upstream nodeapp_server            

                           server 127.0.0.1:8003;       

       

upstream nodeapp1_server            

                             server 127.0.0.1:8013;       

        server                   

                listen 8000;           

                server_name 127.0.0.1;       

index index.html index.htm index.jsp default.jsp index.do default.do;        root /var/www/public;

        location /           

proxy_set_header X-Real-IP $remote_addr;          

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;           proxy_set_header Host $http_host;          

proxy_set_header X-NginX-Proxy true;

proxy_pass http://nodeapp_server/;          

proxy_redirect off;       

    location ~* \\.

(js|css|json|bson|plist|gif|jpg|jpeg|png|bmp|swf|icn|pdata|fnt|lug|map|mdata|sam|mp3)$       

   expires -1;  

add_header Access-Control-Allow-Origin "*"; 


access_log off; 

   

以上是关于Nginx的TCP反向代理设置的主要内容,如果未能解决你的问题,请参考以下文章

nginx实现MySQL负载均衡

nginx之TCP反向代理

nginx之TCP反向代理

使用Nginx实现UDP反向代理

基于nginx和tengine的tcp反向代理,负载均衡 安装和配置

Nginx的TCP反向代理设置