怎么给windows下的nginx添加tcp模块

怎么给windows下的nginx添加tcp模块,第1张

有台服务器为windows,现在要通过远程管理,但该服务器是在内网,只能通过前置机跳转,而前置机为linux,需要用到tcp代理,该服务器已安装了nginx-1.2.9,打算使用nginx的tcp代理模块来做,以下是安装配置过程

在root目录下载解压软件,得到nginx_tcp_proxy_module-master目录

wget https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master.zip

unzip master.zip

进入nginx原安装路径 ,将nginx进程停止,安装和编译模块

cd /root/nginx-1.2.9

killall nginx

patch -p1 </root/nginx_tcp_proxy_module-master

./configure --add-module=/root/nginx_tcp_proxy_module

make

make install

/usr/local/nginx/sbin/nginx -V

安装完成后,配置tcp代理,在nginx.conf文件最后添加如下内容,需要注意的是http和tcp不能使用相同端口

tcp {

upstream cluster {

# simple round-robin

server 192.168.1.23:3389

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 3389

proxy_pass cluster

access_log logs/3389_access.log

}

}

添加完成后,测试配置文件,运行nginx

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx

即可访问远程桌面。

Nginx配置Nginx的配置主要是修改/usr/local/nginx/conf/nginx,conf文件#配置用户和用户组userwwwwww#工作进程数,建议设置为CPU的总核数worker_processes2#全局错误日志定义类型,日志等级从低到高依次为:debug|info|notice|warn|error|criterror_loglogs/error.loginfo#记录主进程ID的文件pid/usr/local/nginx/nginx.pid#一个进程能打开的文件描述符最大值,理论上该值因该是最多能打开的文件数除以进程数。但是由于nginx负载并不是完全均衡的,#所以这个值最好等于最多能打开的文件数。执行sysctl-a|grepfs.file可以看到linux文件描述符。worker_rlimit_nofile65535#工作模式与连接数上限events{#工作模式,linux2.6版本以上用epolluseepoll#单个进程允许的最大连接数worker_connections65535}#设定http服务器,利用它的反向代理功能提供负载均衡支持http{#文件扩展名与文件类型映射表includemime.types#默认文件类型default_typeapplication/octet-stream#日志格式log_formatmain'$remote_addr-$remote_user[$time_local]"$request"''$status$body_bytes_sent"$http_referer"''"$http_user_agent""$http_x_forwarded_for"'#accesslog记录了哪些用户,哪些页面以及用户浏览器、ip和其他的访问信息access_loglogs/access.logmain#服务器名字的hash表大小server_names_hash_bucket_size128#客户端请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,#如果header过大,它会使用large_client_header_buffers来读取。#如果设置过小HTTP头/Cookie过大会报400错误nginx400badrequest#如果超过buffer,就会报HTTP414错误(URITooLong)#nginx接受最长的HTTP头部大小必须比其中一个buffer大,否则就会报400的HTTP错误(BadRequest)。client_header_buffer_size32klarge_client_header_buffers432k#客户端请求体的大小client_body_buffer_size8m#隐藏ngnix版本号server_tokensoff#忽略不合法的请求头ignore_invalid_headerson#指定启用除第一条error_page指令以外其他的error_page。recursive_error_pageson#让nginx在处理自己内部重定向时不默认使用server_name设置中的第一个域名server_name_in_redirectoff#开启文件传输,一般应用都应设置为on;若是有下载的应用,则可以设置成off来平衡网络I/O和磁盘的I/O来降低系统负载sendfileon#告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送。tcp_nopushon#告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,#这样发送一小块数据信息时就不能立即得到返回值。tcp_nodelayon#长连接超时时间,单位是秒keepalive_timeout65#gzip模块设置,使用gzip压缩可以降低网站带宽消耗,同时提升访问速度。gzipon#开启gzipgzip_min_length1k#最小压缩大小gzip_buffers416k#压缩缓冲区gzip_http_version1.0#压缩版本gzip_comp_level2#压缩等级gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml#压缩类型#upstream作负载均衡,在此配置需要轮询的服务器地址和端口号,max_fails为允许请求失败的次数,默认为1.#weight为轮询权重,根据不同的权重分配可以用来平衡服务器的访问率。upstreamhostname{server192.168.2.149:8080max_fails=0weight=1server192.168.1.9:8080max_fails=0weight=1}#主机配置server{#监听端口listen80#域名server_namehostname#字符集charsetutf-8#单独的access_log文件access_loglogs/192.168.2.149.access.logmain#反向代理配置,将所有请求为http://hostname的请求全部转发到upstream中定义的目标服务器中。location/{#此处配置的域名必须与upstream的域名一致,才能转发。proxy_passhttp://hostnameproxy_set_headerX-Real-IP$remote_addr}#启用nginxstatus监听页面location/nginxstatus{stub_statusonaccess_logon}#错误页面error_page500502503504/50x.htmllocation=/50x.html{roothtml}}}至此,nginx基本的负载均衡配置完成,实验中部署2台tomcat,然后访问时返回不同的结果,在浏览器中输入地址,确实能看到不同的返回结果。nginx配置文件的内容还有待于继续学习。

有些时候请求某些接口的时候需要返回指定的文本字符串或者json字符串,如果逻辑非常简单或者干脆是固定的字符串,那么可以使用nginx快速实现,这样就不用编写程序响应请求了,可以减少服务器资源占用,并且响应性能非常快!

nginx配置返回文本或json

https://www.cnblogs.com/senlinyang/p/10249421.html

nginx返回json或者文本格式的方法

https://www.jb51.net/article/137188.htm

http://www.voidcn.com/article/p-acfgxmbx-ct.html

nginx返回固定json或者文本格式的方法

https://www.shangmayuan.com/a/0f5ea168d92a4e4dacda449d.html

https://www.magentonotes.com/nginx-config-return-json.html


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11628519.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存