我这套 nginx 反向代理设置有问题吗?帮忙看看各位大佬。

321 天前
 mikaelson
web 应用是 vue+java
在内网服务器

nginx.conf 配置如下:
#user nobody;
worker_processes auto;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 65535;
}


http {
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_min_length 1000;
gzip_disable "msie6";
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 206 60m;
proxy_cache_valid 404 1m;
keepalive_timeout 65;
keepalive_requests 100;
tcp_nopush on;
client_max_body_size 200m;
include mime.types;
default_type application/octet-stream;
sendfile on;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
include vhost/*.conf;
}

vhost 里的文件是这样写的:
server {
listen 8081;
server_name localhost;
client_max_body_size 100M;

location / {
root /data/xxx/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
location ^~/mall_war/ {
proxy_pass http://localhost:8900;
}
}

然后我在腾讯云上的公网服务器的 nginx 对他做反向代理,nginx.conf 配置一样,然后我的反向代理文件是这样写的
upstream test{
server xxx.xxx.xxx.xxx:8081;
}
server {
listen 443 ssl;
server_name test.abc.com;
ssl_certificate "/usr/local/nginx/ssl/test.abc.com.pem";
ssl_certificate_key "/usr/local/nginx/ssl/test.abc.com.key";
location / {
proxy_pass http://test;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_request_headers on;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}




现在就是几个人访问也一直报错
upstream prematurely closed connection while reading response header from upstream
一直报这个错误

是网络问题?还是 nginx 配置有问题?
请大佬帮忙看看,实在不懂咋搞了。
1640 次点击
所在节点    NGINX
6 条回复
leaves615
321 天前
根据错误信息,是反代 nginx 在等待读取 upstream 的响应时,连接已断开。 初步判断是反代服务器到内网服务器的连接超时小于反代 nginx proxy read timeout 。

---
看你配置,有两个时间配置不对等:
keepalive_timeout 65; proxy_read_timeout 300s;
mikaelson
321 天前
@leaves615
keepalive_timeout:65s ##设置 nginx 开启 keepalive ,超时时间为 65 秒,也就是说:如果客户端 65 秒内没有后续的请求过来,nginx 就会断掉这个 TCP 连接,设置为 0 表示禁用 keepalive 。

keepalive_requests :100 ##设置长连接能够处理请求的次数,100 表示:一个长连接 nginx 最多处理 100 次请求就会关闭。

这 2 个不对等好像不影响呀?
q8515620
321 天前
我也不懂,但你说的这两个跟 1 楼说的不一样
mikaelson
321 天前
@q8515620 只看了前半个,后半个看错了。
如果你的后端服务器响应时间较长,你可以考虑将 proxy_read_timeout 设置得比 keepalive_timeout 更大,以确保反向代理服务器有足够的时间读取完整的响应。
如果你希望尽快释放后端服务器的连接以释放资源,你可以将 keepalive_timeout 设置得比 proxy_read_timeout 更小,以便更快地关闭持久连接。

我觉得我这样设置也没错呀,如果 proxy_read_timeout 设置得比 keepalive_timeout 更大,那么反向代理服务器有足够的时间去读取完整的响应。这样理解应该没错呀。
splxsg
320 天前
"upstream prematurely closed connection while reading response header from upstream" 这个错误通常发生在 Nginx 试图读取来自上游服务器的响应时,上游服务器已经关闭了连接。

这个问题可能的原因有很多种,包括:

应用服务器问题:您的 Java 应用可能存在某些问题,导致其无法正确响应请求,或者在响应过程中崩溃。这种情况下,你需要检查 Java 应用的日志,看看是否有任何错误或异常。

网络问题:网络连接不稳定或者带宽不足,也可能导致此问题。尤其是在你的场景中,内网服务器和腾讯云服务器之间的网络可能存在问题。可以尝试通过 ping 或者 traceroute 命令检查网络连接的稳定性。

配置问题:你的 Nginx 配置看起来没有什么问题,但是值得一试的是增加 proxy_read_timeout 的值。这个参数决定了 Nginx 等待上游服务器响应的时间。如果你的应用处理请求需要很长时间,那么可能需要增加这个值。例如,你可以尝试将其设置为 600s 或者更长。

还有,你可以在你的反向代理文件中添加 proxy_next_upstream 配置项,设置其值为 error timeout invalid_header http_500 http_502 http_503 http_504 ,这样,当遇到这些错误时,Nginx 将会尝试使用其他的服务器。

希望以上的建议能对你有所帮助。
mikaelson
320 天前
@splxsg 谢谢大佬。我照你的方法试试。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/950865

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX