Nginx安装以及详解

简介

Nginx官网
Nginx是一款高性能的HTTP服务器和反向代理服务器,同时支持IMAP/POP3/SMTP代理服务

什么是Nginx

Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/pop3)代理服务器,在BSD-like 协议下发行。由俄罗斯的程序设计师伊戈尔·西索夫使用C语言开发,其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有,百度、京东、新浪、网易、腾讯、淘宝等。

nginx应用场景

  1. http服务器,Nginx是一个http服务,可以独立提供http服务。可以做网页静态服务器。
  2. 虚拟主机。可以实现在一台服务器虚拟出多个网站,例如个人网站使用的虚拟主机。
  3. 反向代理,负载均衡。当网站的 访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可使用Nginx做反向代理,并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

nginx安装

首先 创建文件 制作自动安装脚本
1、vim openresty.sh

yum install -y pcre-devel openssl-devel gcc curl
cd /usr/local/
wget https://openresty.org/download/openresty-1.17.8.2.tar.gz
cd /usr/local/
tar -zxvf openresty-1.17.8.2.tar.gz
cd /usr/local/
mv openresty-1.17.8.2 openresty
cd /usr/local/openresty/
 ./configure --with-luajit \
            --without-http_redis2_module \
            --with-http_iconv_module
cd /usr/local/openresty/ 
make && make install

2、赋予执行文件

chmod +x openresty.sh

3、运行脚本即可

./openresty.sh

4、然后修改环境

vim /etc/profile
#添加如下语句
export PATH=/usr/local/openresty/nginx/sbin:$PATH
#修改完成后执行,使配置文件生效
source /etc/profile

至此安装完成,可以在 cd /usr/local/openresty/ 目录下找到nginx 启动

./nginx

访问服务器ip显示如图,则表示nginx安装成功
在这里插入图片描述

nginx配置文件详解

nginx的配置文件在conf目录下的nginx.conf文件,我们使用cp nginx.conf nginx.conf.back 命令先将初始的配置文件进行备份,防止我们后续操作改坏。

模块详解

配置文件主要分为六个区域:
main(全局设置)作用域是全局
events(nginx工作模式)
upstream(负载均衡服务器设置)
http(http设置)
server(主机设置)
location(URL匹配)

配置文件详解

#设置用户的权限,root nobody 指定 用户名(虚拟机内用户)/Ip访问
#user  nobody; 
#设置工作进程数,一般为CPU核心数*2
worker_processes  1;
#日志输出参数
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#进程ID
#pid        logs/nginx.pid;


events {
#指定运行模型
	use epoll;
#工作连接数 默认512 根据自己的情况调整
    worker_connections  1024;
}

# http模块
http {
#能够支持的类型 在这个文件下写着 mime.types
    include       mime.types;
#默认的类型 在application/octet-stream;
    default_type  application/octet-stream;
#日志格式
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
#访问日志记录
    #access_log  logs/access.log  main;
#启动 发送文件
    sendfile        on;
#开启TCP推送
    #tcp_nopush     on;
#连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
#开启压缩文件
    #gzip  on;
#服务
#服务分组,反向代理的核心关键,此模块(upstream)初始配置文件中没有,需要自己添加
	upstream spring {
		#ip 方式 最大失败连接 间隔 30S 权重为5
		server:192.168.1.106:8081 max_fails=3 fail_timeout=30s weight=5;
		#根据ip 利用Hash算法决定访问哪台机器
		ip_hash;
	}

    server {
    #监听端口
        listen       80;
    #访问名称(默认不用)
        server_name  localhost;

        #charset koi8-r;
#访问日志记录,以及位置
        #access_log  logs/host.access.log  main;
#匹配位置,支持正则表达式
        location / {
        #寻找位置,默认在Nginx目录下的...类型
            root   html;
            index  index.html index.htm;
        }
#错误信息页面
        #error_page  404              /404.html;
#服务器错误页面重定向到静态页面/50.html
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
#实例入访问尾缀为 \.php跳转到127.0.0.1
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
#将PHP脚本传递正在侦听127.0.0.1:9000的FastCGI服务器
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
#拒绝访问.htaccess文件,如果Apache的文档根
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

再来一份文字版的配置文件描述

################### main区域 #################################
#user :来指定Nginx Worker进程运行用户以及用户组,默认由nobody账号运行。也可以创建nginx用户指定用户。
#    创建www用户,在nginx配置文件中把user noboby noboby;-->user www www;即可
#    /usr/sbin/groupadd www 
#    /usr/sbin/useradd -g www www
#worker_processes:来指定了Nginx要开启的子进程数。每个Nginx进程平均耗费10M~12M内存。根据经验,一般指定1个进程就足够了,如果是多核CPU,
#    建议指定和CPU的数量一样的进程数即可。我这里写2,那么就会开启2个子进程,总共3个进程。
#error_log:用来定义全局错误日志文件。日志输出级别有debug、info、notice、warn、error、crit可供选择,其中,debug输出日志最为最详细,而crit输出日志最少。
#pid:用来指定进程id的存储文件位置。
#worker_rlimit_nofile:用于指定一个nginx进程可以打开的最多文件描述符数目,这里是65535,需要使用命令“ulimit -n 65535”来设置。

user  nobody;
worker_processes  1;
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
pid        logs/nginx.pid;


#####################event 区域###############################
#use:用来指定Nginx的工作模式。Nginx支持的工作模式有select、poll、kqueue、epoll、rtsig和/dev/poll。
#    其中select和poll都是标准的工作模式,kqueue和epoll是高效的工作模式,不同的是epoll用在Linux平台上,
#    而kqueue用在BSD系统中,对于Linux系统,epoll工作模式是首选。
#worker_connections:用于定义Nginx每个进程的最大连接数,即接收前端的最大请求数,默认是1024。
#    最大客户端连接数由worker_processes和worker_connections决定,即Max_clients=worker_processes*worker_connections,
#   在作为反向代理时,Max_clients变为:Max_clients = worker_processes * worker_connections/4。 
#    进程的最大连接数受Linux系统进程的最大打开文件数限制,在执行操作系统命令“ulimit -n 65536”后worker_connections的设置才能生效。

events {
    use epoll;
    worker_connections  1024;
}
######################### http设置#####################################
#    http模块负责HTTP服务器相关属性的配置,有server和upstream两个子模块
http {
#include :来用设定文件的mime类型,类型在配置文件目录下的mime.type文件定义,来告诉nginx来识别文件类型。
#default_type:设定了默认的类型为二进制流,也就是当文件类型未定义时使用这种方式,例如在没有配置asp的locate环境时,Nginx是不予解析的,此时,用浏览器访问asp文件就会出现下载了。
#log_format:用于设置日志的格式,和记录哪些参数,这里设置为main,刚好用于access_log来纪录这种类型。
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  0;
    keepalive_timeout  65;
    gzip  on;

######################### server设置#####################################
#server用来定一个虚拟主机,标志定义虚拟主机开始。
#listen:用于指定虚拟主机的服务端口。
#server_name:用来指定IP地址或者域名,多个域名之间用空格分开。
#root :表示在这整个server虚拟主机内,全部的root web根目录。注意要和locate {}下面定义的区分开来。
#index :全局定义访问的默认首页地址。注意要和locate {}下面定义的区分开来。
#charset:用于设置网页的默认编码格式。
#access_log:用来指定此虚拟主机的访问日志存放路径,最后的main用于指定访问日志的输出格式。
    server {
        listen       80;
        server_name  localhost;
        root   /Users/hk/www;
        index  index.php index.html index.htm; 
        charset utf-8;
        access_log  logs/host.access.log  main;
        aerror_log  logs/host.error.log   main;

######################### location设置#####################################
# location模块 负载均衡,反向代理,虚拟域名等配置。是来定位的,定位URL,解析URL,它也提供了强大的正则匹配功能,也支持条件判断匹配,
#    可以通过location指令实现Nginx对动,静态网页进行过滤处理。
#/表示匹配访问根目录。
#root指令用于指定访问根目录时,虚拟主机的web目录,这个目录可以是相对路径(相对路径是相对于nginx的安装目录)。也可以是绝对路径。
#proxy_pass:代理转发,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。
#proxy_set_header:允许重新定义或者添加发往后端服务器的请求头。
#include:加载配置文件,后面介绍nginx多个配置文件时候会提到。
#root:定位localtion匹配的url资源路径。
#index:定义页面显示html,一般和alias配合使用。
        location / {       
            root   html;    
            index  index.html index.htm;
        }

        error_page  404              /404.html;       
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        
        #反向代理配置
        location /jyb {
            proxy_pass http://qurt/;
            proxy_read_timeout 1800s;
            proxy_set_header   Host $host:$server_port;
            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; 
         }
     
        
         #采用uwsgi方式
         location /python/ {
             include uwsgi_params;
             uwsgi_pass 127.0.0.1:33333;
         }
        
        # FastCGI方式
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
        
        #访问nginx本机目录的文件
        location / {
            root   /home/hk/;
            index  index.html index.htm;
        }
        
        location  /static/ {
             alias /var/static/;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    server {
        listen       8000;
        listen       somename:8080;
        server_name  somename  alias  another.alias;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }


    # HTTPS server    
    server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      cert.pem;
        ssl_certificate_key  cert.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
    
##############upstram 模块################
# upstream 模块  负载均衡模块,通过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡。
#Nginx的负载均衡模块目前支持4种调度算法:
#    weight 轮询(默认)。每个请求按时间顺序逐一分配到不同的后端服务器,如果后端某台服务器宕机,故障系统被自动剔除,使用户访问不受影响。
#        weight指定轮询权值,weight值越大,分配到的访问机率越高,主要用于后端每个服务器性能不均的情况下。
#    ip_hash。每个请求按访问IP的hash结果分配,这样来自同一个IP的访客固定访问一个后端服务器,有效解决了动态网页存在的session共享问题。
#    fair。比上面两个更加智能的负载均衡算法。此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,
#        也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配。Nginx本身是不支持fair的,如果需要使用这种调度算法,必须下载Nginx的upstream_fair模块。
#    url_hash。按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,可以进一步提高后端缓存服务器的效率。Nginx本身是不支持url_hash的,
#        如果需要使用这种调度算法,必须安装Nginx 的hash软件包。

#在HTTP Upstream模块中,可以通过server指令指定后端服务器的IP地址和端口,同时还可以设定每个后端服务器在负载均衡调度中的状态。常用的状态有:
#    down,表示当前的server暂时不参与负载均衡。
#    backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
#    max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
#    fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails可以和fail_timeout一起使用。

#注意 当负载调度算法为ip_hash时,后端服务器在负载均衡调度中的状态不能是weight和backup。
#备注: nginx的worker_rlimit_nofile达到上限时,再有客户端链接报502错误. 用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径。
    
    upstream server_group {
        ip_hash;
        server 192.168.123.1:80;
        server 192.168.123.2:80 down;
        server 192.168.123.3:8080  max_fails=3  fail_timeout=20s;
        server 192.168.123.4:8080;
    }
    
    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://server_group/;
        }
    }

}


######################nginx 中location中root和alias的区别 ####################
    nginx指定文件路径有两种方式root和alias,这两者的用法区别,使用方法总结了。
    root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
    [root]
        语法:root path
        默认值:root html
        配置段:http、server、location、if

    [alias]
        语法:alias path
        配置段:location

    root实例:

        location ^~ /t/ {
            root /www/root/html/;
        }
        如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/t/a.html的文件。

    alias实例:
        location ^~ /t/ {
            alias /www/root/html/new_t/;
        }
        如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/new_t/a.html的文件。注意这里是new_t,
        因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。
    注意:
        1. 使用alias时,目录名后面一定要加"/"2. alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
        3. alias只能位于location块中。(root可以不放在location中)

nginx代理

修改配置文件为

#1、增加upstream模块
upstream spring {
        server 192.168.1.106:8081;
}
#2、修改server模块
 server {
        listen       80;
        server_name  nginx.test.com;#我这里配置为自定义域名,要想在客户端通过此域名访问,需要在客户端配置host文件,我是在window配置的,在路径C:\Windows\System32\drivers\etc下的host文件增加IP:域名。(IP为服务器IP,域名为server_name自定义的域名)
        ..........省略
}
#3、修改location模块
 location / {
            proxy_pass http://spring/; #自定义,但是要与upstream模块名保持一致
            root   html;
            index  index.html index.htm;
        }

我用的springboot搭建的demo
修改完配置文件执行以下操作:

cd /usr/local/openresty/nginx/sbin
#检查配置文件语法是否有误,执行完之后显示如下,表示语法检查正确
#nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
#nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
./nginx -t

#使配置文件生效
./nginx -s reload

接下来我们访问:http://nginx.test.com/spring/hello/initializr,显示成功
在这里插入图片描述

nginx负载均衡

负载均衡的意思是在服务器集群中,需要有一台服务器作为调度者,客户端所有的请求都由调度者接收,调度者再根据每台服务器的负载情况,将请求分配给对应的服务器去处理。在这个过程中,调度者如何合理分配任务,保证所有服务器将性能充分发挥,从而保证服务器集群的整体性能最优,这就是负载均衡的问题了。

nginx负载均衡的方式

轮询

轮询 方式是nginx负载默认的方式,顾名思义,所有的请求都按照时间的顺序分配到不同的服务器上,如果某个服务宕机,可以自动剔除,我这里使用springboot分别启用了3个服务分别为8081、8082、8083,配置文件增加如下配置即可。

upstream spring {
        server 192.168.1.106:8081;
        server 192.168.1.106:8082;
        server 192.168.1.106:8083;
    }

效果如下:
第一次访问:
在这里插入图片描述
第二次访问:
在这里插入图片描述
第三次访问:
在这里插入图片描述

权重

权重 指定每个服务的权重比例,weight访问比率成正比,通常用于后端服务器性能不统一,将性能好的分配权重高发挥服务器的最大性能,如下配置后8081服务的访问比率是8082的2倍。

upstream spring {
        server 192.168.1.106:8081 weight=2;
        server 192.168.1.106:8082 weight=1;
    }

共刷新3次效果如下:
第一次:
在这里插入图片描述
第二次:
在这里插入图片描述
第三次:
在这里插入图片描述

iphash

每个请求都根据访问ip的hash结果进行分配,经过这样的处理,每个访客固定访问一个后端服务,如下配置(ip_hash可以和weight配合是借用)

upstream spring {
		ip_hash;
        server 192.168.1.106:8081 weight=2;
        server 192.168.1.106:8082 weight=1;
    }

因为每次访问的IP都是一样的所以,每次的访问的后台服务都是固定的效果如下:

在这里插入图片描述

nginx 限流熔断

令牌桶算法

在这里插入图片描述

算法思想是:

  • 令牌以固定的速率产生,并缓存到令牌桶中;
  • 令牌桶放满时,多余的令牌被丢弃
  • 请求要消耗等比例的令牌才能被处理
  • 令牌不够时,请求被缓存。
漏桶算法

在这里插入图片描述
算法思想是:

  • 水(请求)从上方倒入桶中,从水桶下方流出(被处理);
  • 来不及流出的水被存放在水桶中(缓冲),以固定的塑速率流出;
  • 水桶满后水溢出(丢弃)。
  • 这个算法的核心是:缓存请求、匀速处理、多余的请求被直接丢弃。相比令牌桶算法不同之处在于它不但有一直桶,还有一个队列,这个桶是用来存放令牌的,队列才是用来存放请求的。

从作用上来说,漏桶和令牌桶算法最明显区别就是是否允许突发流量的处理,漏桶算法能够强行限制数据的实时传输速率,对突发流量不做额外处理;而令牌桶算法能够在限制数据的平均速率的同时允许某种程度的突发传输

Nginx按请求速率模块使用的是漏桶算法,即能够强行保证请求的实时处理不会超出设置的阈值。

案例

配置文件中http模块增加:


#1、spring自定义的名称、10m是缓存10m 每秒处理两次请求
limit_req_zone $binary_remote_addr zone=spring:10m rate=2r/s;
#location模块增加
limit_req zone=spring;
location / {
            proxy_pass http://spring/;
            root   html;
            index  index.html index.htm;
            limit_req zone=spring;
}

保存后重启服务
当访问过快时出现,访问效果如下:
在这里插入图片描述
但是我们生产中不会这样匀速,很多情况下是在某个时刻请求很多我们就可以使用缓存,将超出的请求缓存起来,匀速请求我们修改以下设置

location / {
            proxy_pass http://spring/;
            root   html;
            index  index.html index.htm;
            limit_req zone=spring burst=4 #缓存4个请求,从0开始;
}

访问效果如下:当请求过快时会等待请求前面请求处理完再进行处理
在这里插入图片描述
我们因为加了缓存可以看出当我们请求过多时,请求时间过长这样用户体验不好,那我们就要给更改为,请求进入了缓存就能够进行处理,我们更改配置如下:

location / {
            proxy_pass http://spring/;
            root   html;
            index  index.html index.htm;
            limit_req zone=spring burst=5 nodelay; #请求不在等待
        }

访问效果:
在这里插入图片描述
当请求过快时,会返回一个错误页面这样很不友好,我们可以返回一个自定义状态

location / {
            proxy_pass http://spring/;
            root   html;
            index  index.html index.htm;
            limit_req zone=spring burst=5 nodelay;
            limit_req_status 598; #我这里先定义成598
        }

访问过快时效果:
在这里插入图片描述

nginx热备部署

那么当我们服务集群其中的主服务宕机了,nginx就要进行服务的主备切换
修改配置如下:

upstream spring {
        server 192.168.1.106:8081;
        server 192.168.1.106:8082 backup;# 再备用节点标注backup backup不能与ip_hash共用
    }

正常访问效果:
在这里插入图片描述
当主节点宕机之后再次访问效果:
在这里插入图片描述
当主节点再次启动会将请求再次转到master节点上
在这里插入图片描述

nginx安全认证

ngx_http_auth_basic_module允许通过使用"HTTP基本身份认证"协议验证用户名和密码来限制对资源的访问。坦白点来说,如果想对某目录设置访问权限,可以使用ngx_http_auth_basic_module提供的功能。

#基本身份认证模块 语法及语义# **auth_basic**
   #语法:auth_basic string | off;
   #语义:使用"HTTP基本身份认证"协议启用用户名和密码的验证。指定的参数用作realm,参数值可以包含变量(1.3.10、1.2.7)。设置特殊值off将关闭身份认证。
   #参数值会作为提示显示在认证对话框标题栏中。
  auth_basic_user_file
   #语法:auth_basic_user_file file;
   #语义:
   #指定存储用户名和密码的文件格式:
   # comment
name1:password1
name2:password2:comment
name3:password3
1234

密码支持以下类型:

· 使用crypt()函数加密。可以使用Apache Http Server发行版中的“htpasswd”实用程序或“openssl passwd”命令生成。

· 使用基于MD5的密码算法(apr1)的Apache变体进行散列;可以使用相同的工具生成。

· 由RFC2307中描述的"{scheme}data"语法(1.0.3+)指定。当前实现方案包括文本(用于示例,不应使用)、SHA(1.3.13)(SHA-1哈希文本,不应使用)、SSHA(SHA-1加盐哈希,被OpenLDAP、Dovecot等软件包使用)。

htpasswd 生成密码文件

htpasswd是开源Http服务器Apache Http Server的一个命令工具,所以本机如果没有该命令,需要先安装。

yum install httpd-tools -y
1

htpasswd指令用来创建和更新用于基本认证的用户认证密码文件。htpasswd指令必须对密码文件有读写权限,否则会返回错误码。

htpasswd参数列表:

参数参数说明
-b密码直接写在命令行中,而非使用提示输入的方式
-c创建密码文件,若文件存在,则覆盖文件重新写入
-n不更新密码文件,将用户名密码进行标准输出
-m使用MD5算法对密码进行处理
-d使用CRYPT算法对密码进行处理
-s使用SHA算法对密码进行处理
-p不对密码进行加密处理,使用明文密码
-D从密码文件中删除指定用户记录

htpasswd生成Nginx密码文件:

# htpasswd 需要安装httpd-tools工具
[root@sun vhost]htpasswd -c -d /usr/local/openresty/nginx/conf/nginxpasswd ops  //创建web认证账号,ops为用户名 
New password: *******  //认证密码
Re-type new password: ********  //再次确认认证密码 
Adding password for user ops

此时查看/usr/local/openresty/nginx/conf/nginxpasswd文件:

Securitit:$apr1$nuJ/GIEt$nH8z8kk0EFVq5oo9.qRzI/
1

若要在已有Nginx密码文件中追加用户,则无需-c参数:

htpasswd -b /usr/local/nginx/conf/nginxpasswd Csdn 111111
1

此时查看/usr/local/openresty/nginx/conf/nginxpasswd文件:

Securitit:$apr1$nuJ/GIEt$nH8z8kk0EFVq5oo9.qRzI/
Csdn:$apr1$1IWZsiJl$q1K5CwAboegG1LO18Jdta0
12

基本身份认证模块 示例

基于默认nginx.conf进行修改,使用上面生成的密码文件进行认证:

worker_processes  1;

error_log  logs/error.log;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  securitit;

        location / {
	    	auth_basic "Please Input UserName And Password!";
		
	    	auth_basic_user_file nginxpasswd;
        }

    }
}

展示效果:
在这里插入图片描述

输入上述操作设置的用户名密码即可访问:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南P

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值