nginx 与上游服务器建立连接的相关设置
向上游服务建立联系
1 2 3 | Syntax: proxy_connect_timeout time; #设置TCP三次握手超时时间,默认60秒;默认超时后报502错误 Default: proxy_connect_timeout 60s ; Context: http, server, location |
超时更换上游服务器处理请求
1 2 3 | Syntax: proxy_next_upstream http_502 | ..; Default: proxy_next_upstream error timeout; Context: http, server, location |
上游连接启用TCP keepalive的长连接
1 2 3 | Syntax: proxy_socket_keepalive on | off; Default: proxy_socket_keepalive off; Context: http, server, location |
上游连接启用http长连接
1 2 3 4 5 6 7 | Syntax: keepalive connections; Default: — Context: upstream Syntax: keepalive_requests number; Default: keepalive_requests 100 ; Context: upstream |
修改tcp连接中的local address
1 2 3 4 5 6 7 8 9 | Syntax: proxy_bind address [transparent] | off; Default: — Context: http, server, location 可以使用的变量 proxy_bind $remote_addr; 可以使用不属于所在机器的IP proxy_bind $remote_addr transparent; |
当客户端关闭连接时
1 2 3 | Syntax: proxy_ignore_client_abort on | off; Default: proxy_ignore_client_abort off; Context: http, server, location |
向上游服务发送HTTP请求;超时时间
1 2 3 | Syntax: proxy_send_timeout time; Default: proxy_send_timeout 60s ; Context: http, server, location |
接收上游服务器HTTP响应头部
1 2 3 | Syntax: proxy_buffer_size size; #设置接收上游服务的头部最大值 Default: proxy_buffer_size 4k | 8k ; Context: http, server, location |
接收上游服务器HTTP包体大小
1 2 3 | Syntax: proxy_buffers number size; # 接收上游服务器响应包大小内存设置 Default: proxy_buffers 8 4k | 8k ; Context: http, server, location |
接收上游服务器的包体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Syntax: proxy_buffering on | off; X - Accel - Buffering 头部 Default: proxy_buffering on; #接收完完整的响应包在在响应给客户端 Context: http, server, location Syntax: proxy_max_temp_file_size size; #设之上游服务器响应包过大,后存入磁盘文件。文件大小的设置 Default: proxy_max_temp_file_size 1024m ; Context: http, server, location Syntax: proxy_temp_file_write_size size; # 每一次向磁盘写入的字节数 Default: proxy_temp_file_write_size 8k | 16k ; Context: http, server, location Syntax: proxy_temp_path path [level1 [level2 [level3]]]; # 设置存储文件的目录的路径 Default: proxy_temp_path proxy_temp; Context: http, server, location |
及时转发的包体
1 2 3 | Syntax: proxy_busy_buffers_size size; #虽然设置缓存上游服务器的响应报文;但也可以接缓存一部分时就发送给客户 Default: proxy_busy_buffers_size 8k | 16k ; Context: http, server, location |
接收上游服务器网络速率相关指令
1 2 3 4 5 6 7 8 9 10 | Syntax: proxy_read_timeout time; #两次读取超时时间 Default: proxy_read_timeout 60s ; Context: http, server, location Syntax: proxy_limit_rate rate; #限制读取上游服务的响应 Default: proxy_limit_rate 0 ; Context: http, server, location |
上游包体的持久化
1 2 3 4 5 6 7 8 9 10 | Syntax: proxy_store_access users:permissions ...; #从上游服务接收文件存入本地文件权限的配置方法 Default: proxy_store_access user:rw; Context: http, server, location Syntax: proxy_store on | off | string; # on的话会存到root对应的目录下;off不开启,string通过变量重新指定存放目录 Default: proxy_store off; Context: http, server, location |
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | [root@python vhast] # cat shangyou.conf server { listen 8011 ; default_type text / plain; return 200 '8011 server response.\n' ; } server { listen 8012 ; default_type text / plain; root html; location / { } location / test { return 200 ' 8012 server response. uri: $uri method: $request_method requset: $request http_name: $http_name \n'; } } [root@python vhast] # cat proxy_method.conf upstream haha{ #ip_hash; #hash user_$arg_username; #server 127.0.0.1:8011; server 127.0 . 0.1 : 8012 ; #keepalive 32; } server { server_name proxy_pass.com; error_log rrups_error.log info; proxy_set_header Connection ""; root / tmp; location / { #proxy_pass http://rrups/addurl; proxy_pass http: / / haha; proxy_store on; proxy_store_access user:rw group:rw all :r; } } [root@python vhast] # echo "qwertyuopuughgbbvvbaaa" > ../../html/a.txt [root@python vhast] # ll /tmp/ 总用量 0 drwx - - - - - - . 3 root root 17 7 月 5 12 : 35 systemd - private - 1c111c1c3c844bb594508d0cf90d1752 - chronyd.service - YE54pH drwx - - - - - - . 3 root root 17 7 月 8 12 : 40 systemd - private - 24aea6f6d72d4605a59e86dc56dacf2a - chronyd.service - 1Ocoyt drwx - - - - - - . 3 root root 17 7 月 9 12 : 05 systemd - private - d838c8aa5ea24b2d88e0e1c546dadbcf - chronyd.service - wNkAsB |
测试
1 2 3 4 5 6 7 8 | [root@python vhast] # curl proxy_pass.com/a.txt qwertyuopuughgbbvvbaaa [root@python vhast] # ll /tmp/ 总用量 4 - rw - rw - r - - . 1 nginx nginx 23 7 月 12 22 : 07 a.txt drwx - - - - - - . 3 root root 17 7 月 5 12 : 35 systemd - private - 1c111c1c3c844bb594508d0cf90d1752 - chronyd.service - YE54pH drwx - - - - - - . 3 root root 17 7 月 8 12 : 40 systemd - private - 24aea6f6d72d4605a59e86dc56dacf2a - chronyd.service - 1Ocoyt drwx - - - - - - . 3 root root 17 7 月 9 12 : 05 systemd - private - d838c8aa5ea24b2d88e0e1c546dadbcf - chronyd.service - wNkAsB |
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏