模块很多,学不完的,瞅一眼,有个基本印象即可
| |
| |
| ngx_http_autoindex_module模块处理以斜杠字符('/')结尾的请求,并生成目录列表。 |
| |
| 当ngx_http_index_module模块找不到索引文件时,通常会将请求传递给ngx_http_autoindex_module模块。 |
| Syntax: autoindex on | off; |
| Default: autoindex off; |
| Context: http, server, location |
| [root@web01 ~] |
| server { |
| listen 80; |
| server_name www.autoindex.com; |
| charset utf8; |
| |
| location / { |
| root /code/autoindex; |
| autoindex on; |
| } |
| } |
| [root@web01 ~] |
| server { |
| listen 80; |
| server_name www.autoindex.com; |
| charset utf8; |
| |
| location / { |
| root /code/autoindex; |
| index index.html; |
| } |
| |
| location /download { |
| root /code/autoindex; |
| autoindex on; |
| } |
| } |
| |
| |
| [root@web01 ~] |
| [root@web01 ~] |
| |
| |
| http://www.autoindex.com/ 为主站 |
| http://www.autoindex.com/download/ 为下载文件的目录 |
| |
| Syntax: autoindex_exact_size on | off; |
| Default: autoindex_exact_size on; |
| Context: http, server, location |
| |
| |
| Syntax: autoindex_localtime on | off; |
| Default: autoindex_localtime off; |
| Context: http, server, location |
| [root@web01 ~] |
| server { |
| listen 80; |
| server_name www.autoindex.com; |
| charset utf8; |
| |
| location / { |
| root /code/autoindex; |
| index index.html; |
| } |
| |
| location /download { |
| root /code/autoindex; |
| autoindex on; |
| autoindex_exact_size off; |
| autoindex_localtime on; |
| } |
| } |
| |
| Syntax: allow address | all; |
| Default: — |
| Context: http, server, location, limit_except |
| |
| |
| Syntax: deny address | all; |
| Default: — |
| Context: http, server, location, limit_except |
| |
| |
| [root@web01 ~] |
| server { |
| listen 80; |
| server_name www.autoindex.com; |
| charset utf8; |
| |
| location / { |
| root /code/autoindex; |
| index index.html; |
| } |
| |
| location /download { |
| root /code/autoindex; |
| autoindex on; |
| autoindex_exact_size off; |
| autoindex_localtime on; |
| deny 10.0.0.1; |
| allow all; |
| } |
| } |
| [root@web01 ~] |
| server { |
| listen 80; |
| server_name www.autoindex.com; |
| charset utf8; |
| |
| location / { |
| root /code/autoindex; |
| index index.html; |
| } |
| |
| location /download { |
| root /code/autoindex; |
| autoindex on; |
| autoindex_exact_size off; |
| autoindex_localtime on; |
| allow 10.0.0.1; |
| |
| deny all; |
| } |
| } |
| [root@web01 ~] |
| server { |
| listen 80; |
| server_name www.autoindex.com; |
| charset utf8; |
| |
| location / { |
| root /code/autoindex; |
| index index.html; |
| } |
| |
| location /download { |
| root /code/autoindex; |
| autoindex on; |
| autoindex_exact_size off; |
| autoindex_localtime on; |
| allow 10.0.0.1; |
| |
| deny 10.0.0.0/24; |
| } |
| } |
| |
| Syntax: auth_basic string | off; |
| Default: auth_basic off; |
| Context: http, server, location, limit_except |
| |
| |
| Syntax: auth_basic_user_file file; |
| Default: — |
| Context: http, server, location, limit_except |
| |
| [root@web01 ~] |
| New password: |
| Re-type new password: |
| Adding password for user lhd |
| |
| |
| [root@web01 ~] |
| New password: |
| Re-type new password: |
| Adding password for user egon |
| |
| |
| [root@web01 ~] |
| lhd:$apr1$A7d4BWYe$HzlIA7pjdMHBDJPuLBkvd/ |
| egon:$apr1$psp0M3A5$601t7Am1BG3uINvuBVbFV0 |
认证流程
| 当一个网站配置了基础认证(Basic Authentication),Nginx 作为 web 服务器会对客户端的请求进行检查。如果请求中不包含 Authorization 请求头,服务器会向客户端返回一个 401 Unauthorized 状态,并且返回一个 WWW-Authenticate 响应头,提示需要进行认证。 |
| 浏览器收到这个 401 响应后,会弹出一个登录窗口要求用户输入用户名和密码。用户输入正确的用户名和密码后,浏览器会将它们进行 Base64 编码,并在接下来对服务器的每个请求中,自动添加一个 Authorization 请求头,格式如下: |
| Copy |
| Authorization: Basic base64(username:password) |
| 在您提供的例子中,dG9tOjEyMw== 是 tom:123(用户名:密码)的 Base64 编码。所以一旦用户输入了认证信息,浏览器就会自动处理这个过程,将编码后的凭证作为 Authorization 请求头发送给服务器。 |
| 服务器收到请求后会解码 Authorization 的值,验证用户名和密码是否正确,来决定是否授权客户端访问请求的资源。如果用户名和密码验证失败,客户端通常会再次收到 401 响应,并重新请求输入凭证。如果凭证验证成功,则会收到所请求资源的响应。 |
| [root@web01 ~] |
| server { |
| listen 80; |
| server_name www.autoindex.com; |
| charset utf8; |
| access_log /var/log/nginx/www.autoindex.com.log main; |
| |
| location / { |
| root /code/autoindex; |
| index index.html; |
| } |
| |
| location /download { |
| root /code/autoindex; |
| autoindex on; |
| autoindex_exact_size off; |
| autoindex_localtime on; |
| auth_basic "egon say hello!"; |
| auth_basic_user_file /etc/nginx/auth_basic; |
| } |
| } |
| |
| |
| ngx_http_stub_status_module模块提供对nginx基本状态信息的访问。 |
| 应使用--with-http_stub_status_module配置参数启用它,1.26版默认启用,可用nginx -V查看 |
| Syntax: stub_status; |
| Default: — |
| Context: server, location |
| [root@web01 ~] |
| server { |
| listen 80; |
| server_name www.autoindex.com; |
| charset utf8; |
| access_log /var/log/nginx/www.autoindex.com.log main; |
| |
| location / { |
| root /code/autoindex; |
| index index.html; |
| } |
| |
| location /download { |
| root /code/autoindex; |
| autoindex on; |
| autoindex_exact_size off; |
| autoindex_localtime on; |
| auth_basic "性感荷官在线发牌!!!"; |
| auth_basic_user_file /etc/nginx/auth_basic; |
| } |
| |
| location = /basic_status { |
| stub_status; |
| } |
| } |
| |
| |
| |
| Active connections: 2 |
| server accepts handled requests |
| 4 4 21 |
| Reading: 0 Writing: 1 Waiting: 1 |
| |
| |
| Active connections |
| [root@web01 /usr/share/nginx/html] |
| tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN |
| tcp 0 0 192.168.71.112:80 192.168.71.7:65491 ESTABLISHED |
| tcp 0 0 192.168.71.112:80 192.168.71.7:65492 ESTABLISHED |
| |
| accepts |
| handled |
| requests |
| Reading |
| Writing |
| Waiting |
| |
| |
| keepalive_timeout 0; |
| keepalive_timeout 65; |
| |
| Syntax: limit_conn_zone key zone=name:size; |
| Default: — |
| Context: http |
| |
| limit_conn_zone |
| key |
| zone |
| =name |
| :size; |
| |
| |
| Syntax: limit_conn zone number; |
| Default: — |
| Context: http, server, location |
| |
| limit_conn |
| zone |
| number; |
| [root@web01 ~] |
| limit_conn_zone $remote_addr zone=conn_zone:10m; |
| server { |
| listen 80; |
| server_name www.autoindex.com; |
| charset utf8;; |
| limit_conn conn_zone 1; |
| |
| location / { |
| root /code/autoindex; |
| index index.html; |
| } |
| } |
测试的时候请把keepalive_timeout设置为0,排除干扰
| |
| Syntax: limit_req_zone key zone=name:size rate=rate [sync]; |
| Default: — |
| Context: http |
| |
| limit_req_zone |
| key |
| zone |
| =name |
| :size |
| rate=rate [sync]; |
| |
| |
| Syntax: limit_req zone=name [burst=number] [nodelay | delay=number]; |
| Default: — |
| Context: http, server, location |
| |
| limit_req |
| zone=name |
| [burst=number] |
| [nodelay | delay=number]; |
| [root@web01 ~] |
| |
| limit_conn_zone $remote_addr zone=conn_zone:10m; |
| limit_req_zone $remote_addr zone=req_zone:10m rate=1r/s; |
| server { |
| listen 80; |
| server_name www.autoindex.com; |
| charset utf8; |
| limit_conn conn_zone 1; |
| limit_req zone=req_zone; |
| |
| |
| location / { |
| root /code/autoindex; |
| index index.html; |
| } |
| } |