首页主机资讯Ubuntu Nginx错误代码怎么解决

Ubuntu Nginx错误代码怎么解决

时间2025-11-19 21:05:04发布访客分类主机资讯浏览903
导读:Ubuntu 上 Nginx 错误代码排查与修复 一、快速定位与通用排查 查看错误日志:优先检查 /var/log/nginx/error.log,它通常直接指出错误原因与文件行号。 验证配置语法:执行 sudo nginx -t,必要时...

Ubuntu 上 Nginx 错误代码排查与修复

一、快速定位与通用排查

  • 查看错误日志:优先检查 /var/log/nginx/error.log,它通常直接指出错误原因与文件行号。
  • 验证配置语法:执行 sudo nginx -t,必要时指定配置 sudo nginx -t -c /etc/nginx/nginx.conf
  • 查看服务状态与端口占用:用 sudo systemctl status nginx 查看运行状态;用 sudo netstat -tulnp | grep :80(或 ss -tulnp | grep :80)确认端口是否被占用。
  • 动态生效与重启:修改后执行 sudo systemctl reload nginx;完全重启用 sudo systemctl restart nginx
  • 防火墙放行:Ubuntu 常用 sudo ufw allow 80,443/tcp;如使用 firewalld 则 sudo firewall-cmd --permanent --add-service=http --add-service=https & & sudo firewall-cmd --reload

二、常见错误代码与修复对照表

错误码或报错 典型原因 快速修复
403 Forbidden 目录无索引且未开启目录列表;文件/目录权限或属主不对;Nginx 用户无访问权 在对应 location 添加 index index.html index.php; 或开启 autoindex on; ;修正权限 chmod -R 755 /var/www/htmlchown -R www-data:www-data /var/www/html;确认 /etc/nginx/nginx.conf 中的 user www-data; 与目录属主一致
404 Not Found root/alias 路径错误;请求的文件不存在;未使用 try_files 核对 root 是否指向正确目录;确认文件存在;在 location 中使用 try_files $uri $uri/ =404;
500 Internal Server Error 后端脚本异常(如 PHP 致命错误);磁盘空间满文件句柄数限制过低;配置语法/路径错误 查看 /var/log/nginx/error.log 定位具体行;执行 df -lh 检查磁盘;提升句柄数:ulimit -n 65535,在 /etc/security/limits.conf 设置 soft/hard nofile 65535,在 /etc/sysctl.conf 设置 fs.file-max=65536,在 /etc/nginx/nginx.confworker_processes 后添加 worker_rlimit_nofile 65535; ;用 nginx -t 检查配置与路径
502 Bad Gateway 上游服务(如 PHP-FPM/Node.js)未启动或端口不对;连接被拒/超时;防火墙阻断 确认上游运行:systemctl status php-fpm 或 **ps aux
503 Service Unavailable 上游过载或崩溃;反向代理 max_fails/fail_timeout 触发熔断 检查上游健康与负载;优化或扩容上游;在 upstream 中调整 max_failsfail_timeout 并重试
504 Gateway Timeout 上游处理耗时过长;Nginx 超时过短 增大 proxy_read_timeout / proxy_connect_timeout;优化上游性能或异步化处理
SSL 相关错误(如证书不匹配) 证书/私钥不匹配或证书过期;TLS 配置不当 核对 ssl_certificatessl_certificate_key 是否匹配且未过期;可用 openssl s_client -connect yourdomain.com:443 -servername yourdomain.com 测试;必要时用 certbot --nginx -d yourdomain.com 重新签发
端口被占用(启动报错:bind() to 0.0.0.0:80 failed (98: Address already in use) 其他服务(如 apache2)占用 80/443 查占用进程 **netstat -tulnp
配置文件语法错误(如 unknown directive 指令拼写错误、缺少分号/括号、路径错误 执行 sudo nginx -t 定位文件与行号;修正语法与路径后重载 sudo systemctl reload nginx

三、实用命令清单

  • 服务管理:sudo systemctl start|stop|restart|reload|status nginx
  • 配置与版本:sudo nginx -t,必要时 sudo nginx -t -c /path/nginx.confnginx -vnginx -V
  • 端口与进程:sudo netstat -tulnp | grep :80ss -tulnp | grep :80ps aux | grep nginx
  • 日志与连通性:tail -f /var/log/nginx/error.logtelnet 127.0.0.1 9000curl -I http://localhost
  • 防火墙:sudo ufw allow 80,443/tcp;或 sudo firewall-cmd --permanent --add-service=http --add-service=https & & sudo firewall-cmd --reload
  • 权限与属主:ls -l /var/www/htmlchmod -R 755 /var/www/htmlchown -R www-data:www-data /var/www/html

四、最小可用配置示例

  • 静态站点
server {
    
    listen 80;
    
    server_name your.domain local.test;
    
    root /var/www/html;
    
    index index.html index.php;


    location / {
    
        try_files $uri $uri/ =404;

    }


    location ~ \.php$ {
    
        include snippets/fastcgi-php.conf;
    
        fastcgi_pass 127.0.0.1:9000;
       # 与 PHP-FPM 一致
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
        include fastcgi_params;

    }

}

  • 反向代理到上游
upstream backend {
    
    server 127.0.0.1:3000;

    # 可按需增加 max_fails/fail_timeout
}


server {
    
    listen 80;
    
    server_name api.local;


    location / {
    
        proxy_pass http://backend;
    
        proxy_set_header Host $host;
    
        proxy_set_header X-Real-IP $remote_addr;
    
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
        proxy_connect_timeout 5s;
    
        proxy_read_timeout 30s;

    }

}
    

修改后执行:sudo nginx -t & & sudo systemctl reload nginx

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Ubuntu Nginx错误代码怎么解决
本文地址: https://pptw.com/jishu/751572.html
如何在Debian上使用Laravel队列系统 Ubuntu Nginx如何配置HTTPS跳转

游客 回复需填写必要信息