Nginx配置优化

Nginx配置优化

一、socket() failed (24: Too many open files) while connecting to upstream

访问量高时,由于系统对于进程的最大文件打开数的限制(ulimit -n 默认65535),而nginx属于单进程多线程并发的服务,所以在访问量高时,连接数超过65535后,会被系统限制连接。

tail -f /usr/local/nginx/logs/error.log

nginx 出错:socket() failed (24: Too many open files) while connecting to upstream

解决办法:

vim /etc/security/limits.conf

nginx       soft    nofile  65535
nginx       hard    nofile  65535
vim /usr/local/nginx/conf/nginx.conf

worker_rlimit_nofile 65535;
#重启机器
reboot
#启动nginx
/usr/local/nginx/sbin/nginx -t
systemctl start nginx

二、net::ERR_CONTENT_LENGTH_MISMATCH 206 (Partial Content)

解决办法:

在ngxin.conf location下面添加配置参数:
proxy_buffer_size 1024k;
proxy_buffers 16 1024k;
proxy_busy_buffers_size 2048k;
proxy_temp_file_write_size 2048k;
server {
    listen 80;
    server_name xxxxx.xxxcloud.com;
    location / {
          proxy_pass http://xxxxx.xxxcloud.com;
          proxy_buffer_size 1024k;
          proxy_buffers 16 1024k;
          proxy_busy_buffers_size 2048k;
          proxy_temp_file_write_size 2048k;
          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_set_header X-Forwarded-Proto $scheme;

    }
}
#重启nginx
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

三、Request-URI Too Large

#ngxin.conf配置优化
client_header_buffer_size 32k;  # 请求行+请求头的标准大小为1k
large_client_header_buffers 4 32k;  # 请求行+请求头的最大大小为2k
#重启nginx
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

四、open() "/usr/local/nginx/client_body_temp/0000000316" failed (13: Permission denied)

问题产生原因:

  1. 请求包数据较大,导致nginx内存缓存不够需要临时写入文件
  2. 写入文件时提示error: 文件权限不够

解决办法一:

指定client_body_temp文件路径并赋予nginx用户权限

#创建client_body_temp并赋权
mkdir /data/client_body_temp
chown -R nginx:nginx /data/client_body_temp
chmod 755 -R nginx:nginx /data/client_body_temp
#ngxin.conf配置优化
client_max_body_size 100m;
client_body_temp_path /data/client_body_temp 3 2;
#重启nginx
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

解决办法二:

#ngxin.conf修改nginx启动用户,以root用户启动
user  root;
#重启nginx
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
posted @ 2023-03-10 16:42  wyanl  阅读(271)  评论(0)    收藏  举报