nginx 端口转发配置 - 漂泊雪狼

时间:2024-03-09 09:44:49

nginx.conf

 

  1 #user  nobody;
  2 worker_processes  3;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  \'$remote_addr - $remote_user [$time_local] "$request" \'
 21     #                  \'$status $body_bytes_sent "$http_referer" \'
 22     #                  \'"$http_user_agent" "$http_x_forwarded_for"\';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 
 34  #服务器的集群  
 35     upstream  sqlserver1433{  #服务器集群名字  
 36        
 37     server    10.164.31.213:1433;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。
 38   
 39     }     
 40 
 41     server {
 42         listen       88;
 43         server_name  localhost;
 44 
 45         #charset koi8-r;
 46 
 47         #access_log  logs/host.access.log  main;
 48 
 49         #location / {
 50          #   root   html;
 51           #  index  index.html index.htm;
 52        # }
 53   location / {  
 54             proxy_pass http://localhost;  
 55             proxy_redirect default;  
 56             index index.aspx;
 57        #root C:\Website\demo1
 58         }  
 59 
 60         #error_page  404              /404.html;
 61 
 62         # redirect server error pages to the static page /50x.html
 63         #
 64         error_page   500 502 503 504  /50x.html;
 65         location = /50x.html {
 66             root   html;
 67         }
 68 
 69         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 70         #
 71         #location ~ \.php$ {
 72         #    proxy_pass   http://127.0.0.1;
 73         #}
 74 
 75         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 76         #
 77         #location ~ \.php$ {
 78         #    root           html;
 79         #    fastcgi_pass   127.0.0.1:9000;
 80         #    fastcgi_index  index.php;
 81         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 82         #    include        fastcgi_params;
 83         #}
 84 
 85         # deny access to .htaccess files, if Apache\'s document root
 86         # concurs with nginx\'s one
 87         #
 88         #location ~ /\.ht {
 89         #    deny  all;
 90         #}
 91     }
 92 
 93 
 94     # another virtual host using mix of IP-, name-, and port-based configuration
 95     #
 96     #server {
 97     #    listen       8000;
 98     #    listen       somename:8080;
 99     #    server_name  somename  alias  another.alias;
100 
101     #    location / {
102     #        root   html;
103     #        index  index.html index.htm;
104     #    }
105     #}
106 
107 
108     # HTTPS server
109     #
110     #server {
111     #    listen       443 ssl;
112     #    server_name  localhost;
113 
114     #    ssl_certificate      cert.pem;
115     #    ssl_certificate_key  cert.key;
116 
117     #    ssl_session_cache    shared:SSL:1m;
118     #    ssl_session_timeout  5m;
119 
120     #    ssl_ciphers  HIGH:!aNULL:!MD5;
121     #    ssl_prefer_server_ciphers  on;
122 
123     #    location / {
124     #        root   html;
125     #        index  index.html index.htm;
126     #    }
127     #}
128 
129 }
130 
131 include stream01.conf;
nginx.conf

 

 

stream01.conf

 

 1 stream {
 2     # 添加socket转发的代理
 3     upstream num_socket01 {
 4         hash $remote_addr consistent;
 5         # 转发的目的地址和端口
 6         server 10.164.31.213:1433;
 7     }
 8 
 9     # 提供转发的服务,即访问localhost:30001,会跳转至代理bss_num_socket指定的转发地址
10     server {
11        listen 1433;
12        proxy_connect_timeout 10s;
13        proxy_timeout 10m;
14        proxy_pass num_socket01;
15     }
16 
17 
18   upstream num_socket02 {
19         hash $remote_addr consistent;
20         # 转发的目的地址和端口
21         server 10.164.31.213:1521;
22     }
23 
24     # 提供转发的服务,即访问localhost:30001,会跳转至代理bss_num_socket指定的转发地址
25     server {
26        listen 1521;
27        proxy_connect_timeout 10s;
28        proxy_timeout 10m;
29        proxy_pass num_socket02;
30     }
31 
32   upstream num_socket03 {
33         hash $remote_addr consistent;
34         # 转发的目的地址和端口
35         server 10.164.31.213:3389;
36     }
37 
38     # 提供转发的服务,即访问localhost:30001,会跳转至代理bss_num_socket指定的转发地址
39     server {
40        listen 33890;
41        proxy_connect_timeout 10s;
42        proxy_timeout 10m;
43        proxy_pass num_socket03;
44     }
45 }
stream01.conf