AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 816661
Accepted
frlan
frlan
Asked: 2016-11-24 07:58:40 +0800 CST2016-11-24 07:58:40 +0800 CST 2016-11-24 07:58:40 +0800 CST

通过 http auth 和大量请求提高 Nginx 的性能

  • 772

我在 Nginx 服务器后面通过 fpm 运行了一个 PHP 端。对于 $reasons,我们需要在该设置之前有一个 http 基本身份验证,所以我最终设置为:

#… 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;
}

哪个有效-但速度很慢。它仅以 100% 的 cpu 利用率处理一个又一个请求。如果我删除 http_auth 它的工作速度很快。

我的问题是:如何改进设置以确保即使使用 http_auth 性能也不错?

以供参考:

# 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
php nginx http-basic-authentication
  • 2 2 个回答
  • 1002 Views

2 个回答

  • Voted
  1. Best Answer
    frlan
    2016-11-25T00:49:25+08:002016-11-25T00:49:25+08:00

    我的问题的根本原因不是直接由 Nginx 引起,而是我用于 htpasswd 的算法。由于在我上面的配置中一次又一次地检查此文件,因此使用一种不会占用此资源的算法非常重要。我最初使用 Python hashlib 调用的基于 sha512 的算法

    passlib.hash.sha512_crypt.encrypt(password)
    

    这太多了。通过直接调用 htpasswd 更改为更简单的算法时

    htpasswd /path/to/passwdfile myusername 
    

    性能问题消失了。

    • 0
  2. RcoderNY
    2021-04-22T01:32:22+08:002021-04-22T01:32:22+08:00

    我在使用 Nginx 和基本 HTTP Auth 时遇到了同样的问题。我使用htpasswdbcrypt 选项的轮数太多。我尝试了 17 的最大值(带有选项-C 17),但服务器根本不喜欢这样。CPU 达到 100%,每个页面都需要一分钟才能加载。

    htpasswd -B -C 17 -c /etc/nginx/.htpasswd username
    

    该-C选项仅在使用 bcrypt 时-B使用,设置用于 bcrypt 算法的计算时间(越高越安全但速度越慢,默认值:5,有效:4 到 17)。计算 bcrypt 密码哈希值的成本随着-C选项指定的轮数而增加。

    默认加密算法htpasswd使用的是为 Apache 修改的 MD5 版本。但是通过 -B 选项,您可以使用 bcrypt。Bcrypt 现在被认为是非常安全的。

    我最终解决了7轮。这似乎运作良好,CPU 不会飙升,页面加载时间也很好。

    htpasswd -B -C 7 -c /etc/nginx/.htpasswd username
    
    • 0

相关问题

  • 用户特定的 Php.ini 当 php 作为模块运行时?

  • 使 php mail() 函数在 ubuntu-server 上工作的步骤是什么?

  • Web 服务器和数据库服务器位于完全不同的位置

  • PHP 作为 CGI 还是 Apache 模块?

  • 通过 VPN 连接什么是远程服务器 IP?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve