大网数据广告活动
服务器运维问答

Improve performance of Nginx with http auth and a lot of requests

· 52CE 我爱测

I've got an PHP side running via fpm behind an Nginx server. For $reasons we need an http base auth in front of that setup, so I ended up in a setup like: #… server section …. auth_basic "Restricted"; auth_basic_user_file /path/t…

问题原文

原帖链接 · frlan · CC BY-SA 3.0

I've got an PHP side running via fpm behind an Nginx server. For $reasons we need an http base auth in front of that setup, so I ended up in a setup like:

#… server section ….

    auth_basic "Restricted";
    auth_basic_user_file /path/to/htpasswd;

#… some more locations …

location ~ \.php$ {


    fastcgi_pass 127.0.0.1:9001;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;
    fastcgi_param APPLICATION_ENV production;
}

Which is working — but very slow. It's only processing one request after the other by 100% cpu utilization. If I remove the http_auth it's working fast.

My question is: How to improve the setup to ensure even with http_auth the performances is okish?

For reference:

# nginx -V
nginx version: nginx/1.8.1
built with OpenSSL 1.0.2j  26 Sep 2016
TLS SNI support enabled
configure arguments: --prefix=/usr --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error_log --pid-path=/run/nginx.pid --lock-path=/run/lock/nginx.lock --with-cc-opt=-I/usr/include --with-ld-opt=-L/usr/lib --http-log-path=/var/log/nginx/access_log --http-client-body-temp-path=/var/lib/nginx/tmp/client --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --with-ipv6 --with-libatomic --with-pcre --with-http_realip_module --add-module=external_module/ngx_devel_kit-0.2.19 --add-module=external_module/lua-nginx-module-0.9.15 --add-module=external_module/modsecurity-2.9.1-nginx-207c85d/nginx/modsecurity --with-http_ssl_module --without-mail_imap_module --without-mail_pop3_module --without-mail_smtp_module --user=nginx --group=nginx

已采纳解答

原帖链接 · frlan · CC BY-SA 3.0

The root cause for my issue was not direct Nginx caused, but algorithm I used for htpasswd. As this file is checked again and again in my config above, it's kind of important to use an algorithm not being this resource hungry. I was originally using a sha512-based algorithm invoked by Python hashlib

passlib.hash.sha512_crypt.encrypt(password)

which was too much. When changing to simpler algorithm via direct call of htpasswd

htpasswd /path/to/passwdfile myusername 

the performance issue were gone.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-11-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。