网页502 Bad Gateway nginx/1.20.1报错的原因与解决方法

2024-04-16 0 277
目录
  • 网页报错的原理
  • 查到的502 Bad Gateway报错的原因
  • 出现的问题和尝试解决
    • 问题
    • 解决
  • 总结

    网页报错的原理

    网页显示502 Bad Gateway 报错原理是用户访问服务器时,nginx代理服务器接收用户信息,但无法反馈给服务器,而出现的报错。

    查到的502 Bad Gateway报错的原因

    • 上游服务器故障:当 Nginx 作为代理服务器时,它将请求转发给上游服务器处理,并将上游服务器的响应返回给客户端。如果上游服务器出现故障、崩溃或无法访问,Nginx 将无法获取有效的响应,从而导致 "502 Bad Gateway" 错误。

    • 连接超时:如果 Nginx 在与上游服务器建立连接时遇到超时问题,它将无法获取响应并返回 "502 Bad Gateway" 错误。这可能是由于上游服务器响应时间过长、网络连接问题或 Nginx 配置中的超时设置不足引起的。

    • 错误的代理配置:Nginx 作为代理服务器时,需要正确配置代理规则和请求头信息,以便将请求正确转发给上游服务器。如果代理配置有误,Nginx 将无法与上游服务器进行有效通信,导致 "502 Bad Gateway" 错误。

    • DNS 解析问题:如果 Nginx 配置中使用了上游服务器的主机名,而 DNS 解析无法将主机名解析为正确的 IP 地址,那么 Nginx 将无法连接到上游服务器,从而导致 "502 Bad Gateway" 错误。

    • 防火墙/安全组限制:如果防火墙或安全组配置限制了 Nginx 与上游服务器之间的通信,例如阻止了特定端口或协议的流量,那么 Nginx 将无法与上游服务器建立连接,从而导致 "502 Bad Gateway" 错误

    出现的问题和尝试解决

    问题

    我自己的情况,用服务器是ip地址加网关可以访问,但是使用域名访问就报错了。

    网页502 Bad Gateway nginx/1.20.1报错的原因与解决方法

    网页502 Bad Gateway nginx/1.20.1报错的原因与解决方法

    1.开始想会不会是硬盘空间不够,但还有80%以上的可用空间

    网页502 Bad Gateway nginx/1.20.1报错的原因与解决方法

    2.然后查资料可能是代理缓冲区设置过小,然后找到存放nginx.conf脚本的文件。在http下面加上了这三句结果还是不行。

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    3.然后尝试编辑nginx.conf,将错误日志输入到/usr/local/nginx/log/error_nginx.log ,更改为info级别然后检查日志文件error_log。但也没报错提醒。

    解决

    最后,就想会不会是脚本的问题nginx.conf。我把它重新梳理了一遍,发现果然是写漏了。

    忘记需改网关。改成访问服务器的网关后就可以使用域名访问了。

    网页502 Bad Gateway nginx/1.20.1报错的原因与解决方法

    网页502 Bad Gateway nginx/1.20.1报错的原因与解决方法

    最后附上nginx.conf脚本源码(有几处需要根据自己的实际情况修改)

    # For more information on configuration, see:
    # * Official English Documentation: http://nginx.org/en/docs/
    # * Official Russian Documentation: http://nginx.org/en/docs/
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    #Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;

    events {
    worker_connections 1024;
    }

    http {
    log_format main \’$remote_addr – $remote_user [$time_local] \”$request\” \’
    \’$status $body_bytes_sent \”$http_referer\” \’
    \’\”$http_user_agent\” \”$http_x_forwarded_for\”\’;

    access_log /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 4096;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    proxy_buffering off;
    upstream chatgpt-web {
    server 127.0.0.1:网关 weight=1;
    }

    server {
    listen 80;
    server_name www.域名;
    location / {
    rewrite ^(.*)$ https://www.域名;
    }
    }

    server {
    listen 443 ssl;
    server_name www.域名;
    ssl_certificate /etc/nginx/(SSL的pem文件名);
    ssl_certificate_key /etc/nginx/(SSL的key文件名);
    location / {
    proxy_pass http://chatgpt-web;
    }
    }

    # Settings for a TLS enabled server.
    #
    # server {
    # listen 443 ssl http2;
    # listen [::]:443 ssl http2;
    # server_name _;
    # root /usr/share/nginx/html;
    #
    # ssl_certificate \”/etc/pki/nginx/server.crt\”;
    # ssl_certificate_key \”/etc/pki/nginx/private/server.key\”;
    # ssl_session_cache shared:SSL:1m;
    # ssl_session_timeout 10m;
    # ssl_ciphers HIGH:!aNULL:!MD5;
    # ssl_prefer_server_ciphers on;
    #
    # # Load configuration files for the default server block.
    # include /etc/nginx/default.d/*.conf;
    #
    # error_page 404 /404.html;
    # location = /40x.html {
    # }
    #
    # error_page 500 502 503 504 /50x.html;
    # location = /50x.html {
    # }
    # }

    }

    检查

    nginx -t

    网页502 Bad Gateway nginx/1.20.1报错的原因与解决方法

    总结

    到此这篇关于网页502 Bad Gateway nginx/1.20.1报错的原因与解决方法的文章就介绍到这了,更多相关502 Bad Gateway nginx/1.20.1报错内容请搜索悠久资源以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源!

    收藏 (0) 打赏

    感谢您的支持,我会继续努力的!

    打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
    点赞 (0)

    悠久资源 Nginx服务器 网页502 Bad Gateway nginx/1.20.1报错的原因与解决方法 https://www.u-9.cn/server/nginx/184801.html

    常见问题

    相关文章

    发表评论
    暂无评论
    官方客服团队

    为您解决烦忧 - 24小时在线 专业服务