nginx stream

#stream模块,就跟http模块一样 
stream {
	#日志记录模块
	log_format basic '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time';

	access_log /var/log/nginx/access.log basic buffer=32k;

	upstream ssh {
                server 127.0.0.1:22;
        }
	#里面可以有多个监听服务,配置监听端口和代理的ip和端口就可以进行tcp代理了。
    server {  
        listen 9922;
        proxy_pass ssh;
        proxy_connect_timeout 1h;
        proxy_timeout 1h;
    }
	server {
		listen 13306;
		#allow 127.0.0.1;
		#deny all;
		proxy_pass 127.0.0.1:3306;
		proxy_connect_timeout 1h;
		proxy_timeout 1h;
	}
}

其他日志格式

log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" 
                  "$upstream_connect_time"';


log_format proxy '[$time_local] remote_addr:$remote_addr $protocol status:$status $bytes_sent $bytes_received s_time:$session_time upstream_addr:$upstream_addr u_conn_time:$upstream_connect_time';

配置经验

  • 测试发现nginx会等待session结束才会记录到日志文件;
  • session日志只是tcp层面的记录,包括session时间,发送接收字节数等等;
  • session内部发送日志(比如一个socket连接建立起来以后,多次发送心跳数据)需要在应用层面才能记录;

重启nginx 可以将缓存日志刷新到文件中。

  • 日志参数
参数 说明 示例
$remote_addr 客户端地址 211.28.65.253
$remote_user 客户端用户名称 --
$time_local 访问时间和时区 18/Jul/2012:17:00:01 +0800
$request 请求的URI和HTTP协议 "GET /article-10000.html HTTP/1.1"
$http_host 请求地址,即浏览器中你输入的地址(IP或域名) www.0.com192.168.100.100
$status HTTP请求状态 200
$upstream_status upstream状态 200
$body_bytes_sent 发送给客户端文件内容大小 1547
$http_referer url跳转来源 百度一下,你就知道
$http_user_agent 用户终端浏览器等信息 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;
$ssl_protocol SSL协议版本 TLSv1
$ssl_cipher 交换数据中的算法 RC4-SHA
$upstream_addr

后台upstream的地址,即真正提供服务的主机地址;

当ngnix做负载均衡时,可以查看后台提供真实服务的设备

10.10.10.100:80
$request_time 整个请求的总时间 0.205
$upstream_response_time 请求过程中,upstream响应时间 0.002

参考 nginx 代理ssh - ahuo - 博客园 

---------------stream 反向代理 mysql------------------

1、-hlocalhost 不会被 deny

2、-h127.0.0.1 会被 deny

3、-h本机ip 会被 deny

4、来源ip 被 deny 连接报错:

ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

4、直连和代理 不同账户的连接区别

CREATE USER 'test'@'127.0.0.1' IDENTIFIED BY '123456';
grant all privileges on test.* to 'test'@'127.0.0.1';

mysql -ujyxt -P3306 -h127.0.0.1 -p'123456'  能连接
mysql -ujyxt -P3306 -hlocalhost -p'123456'  不能连接
mysql -ujyxt -P3306 -h192.168.1.1 -p'123456' 不能连接

mysql -ujyxt -P13306 -h127.0.0.1 -p'123456'  能连接
mysql -ujyxt -P13306 -hlocalhost -p'123456'  不能连接
mysql -ujyxt -P13306 -h192.168.1.1'123456' -p 能连接


CREATE USER 'test'@'localhost' IDENTIFIED BY '123456';
grant all privileges on test.* to 'test'@'localhost';

mysql -ujyxt -P3306 -h127.0.0.1 -p'123456'  能连接
mysql -ujyxt -P3306 -hlocalhost -p'123456'  能连接
mysql -ujyxt -P3306 -h192.168.1.1 -p'123456' 不能连接

mysql -ujyxt -P13306 -h127.0.0.1 -p'123456'  能连接
mysql -ujyxt -P13306 -hlocalhost -p'123456'  能连接
mysql -ujyxt -P13306 -h192.168.1.1 -p'123456' 能连接

CREATE USER 'test'@'%' IDENTIFIED BY '123456';
grant all privileges on test.* to 'test'@'%';

mysql -ujyxt -P3306 -h127.0.0.1 -p'123456'  能连接
mysql -ujyxt -P3306 -hlocalhost -p'123456'  能连接
mysql -ujyxt -P3306 -h192.168.1.1 -p'123456' 能连接

mysql -ujyxt -P13306 -h127.0.0.1 -p'123456'  能连接
mysql -ujyxt -P13306 -hlocalhost -p'123456'  能连接
mysql -ujyxt -P13306 -h192.168.1.1 -p'123456' 能连接

猜你喜欢

转载自blog.csdn.net/yonghutwo/article/details/122324663
今日推荐