博文大纲:
1.Nginx简介
2.Nginx的核心特色
3.Nginx平滑升级
4.修改Nginx版本信息
5.Nginx虚拟主机配置
6.nginx配置文件location选项的做用
7.配置https访问nginx
8.开启Nginx访问认证css
Nginx是一款轻量级的网页服务器、反向代理服务器以及电子邮件代理服务器。因它的稳定性、丰富的功能集、实例配置文件和低系统资源消耗而闻名。html
Nginx已经在俄罗斯最大的门户网站上运行,同时俄罗斯有超过20%的虚拟主机平台采用Nginx做为反向代理服务器;在国内,Nginx已经运行在淘宝、新浪、网易等多家网站使用Nginx做为Web服务器或反向代理服务器。前端
- (1)跨平台:Nginx能够在大多数OS编译运行,并且也有Windows版本;
- (2)配置异常简单、很是容易上手;
- (3)非阻塞、高并发链接;官方测试可以支撑5万的并发链接,在实际环境中能够达到2~3万并发链接数。(这得益于Nginx使用了最新的epoll模型);
- (4)事件驱动:采用epoll模型,支持更大的并发链接;
非阻塞经过不断检查事件的状态来判断是否进行读写操做,这样带来的开销很大,所以就有了异步非阻塞的事件处理机制。这种机制让你能够同时监控多个事件,调用他们是非阻塞的,但能够设置超时时间,在超时时间以内,若是有事件准备好了,就返回。这种机制解决了上面阻塞调用与非阻塞调用的两个问题。
以 epoll 模型为例:当事件没有准备好时,就放入 epoll(队列)里面。若是有事件准备好了,那么就去处理;当事件没有准备好时,才在 epoll 里面等待。这样,咱们就能够并发处理大量的并发请求了,固然,这里的并发请求,是指未处理完的请求。线程只有一个,因此同时能处理的请求固然只有一个了,只是在请求之间进行不断地切换而已,切换也是由于异步事件未准备好,而主动让出的。这里的切换是没有任何代价,你能够理解为循环处理多个准备好的事件。
多线程方式相比,这种事件处理方式是有很大的优点的,不须要建立线程,每一个请求占用的内存也不多,没有上下文切换, 事件处理很是的轻量级,并发数再多也不会致使无谓的资源浪费(上下文切换)。对于 apache 服务器,每一个请求会独占一个工做线程,当并发数上到几千时,就同时有几千的线程在处理请求了。这对操做系统来讲,是个不小的挑战:由于线程带来的内存占用很是大,线程的上下文切换带来的 cpu 开销很大,天然性能就上不去,从而致使在高并发场景下性能降低严重。
总结:经过异步非阻塞的事件处理机制,Nginx 实现由进程循环处理多个准备好的事件,从而实现高并发和轻量级。- (5)Master/Worker 结构:一个 master 进程,生成一个或多个worker 进程,如图:
Master-Worker设计模式主要包含两个主要组件Master和Work,Master维护者Worker队列,将请求下发到多个Worker并行执行,Worker主要进行实际逻辑计算,并将结果返回给Master。
采用独立的进程,可让互相之间不会影响,一个进程退出后,其余进程还在工做,服务不会中断,Master进程则很快从新启动新的Worker进程。固然,Worker进程的异常退出,确定是程序中有bug了,异常退出,会致使当前Worker上的全部请求失败,不过不会影响到全部的请求,因此下降了风险;- (6)内存消耗小:处理高并发的请求内存消耗很是小。在3万并发链接下,开启的10个Nginx进程才消耗150M内存;
- (7)内置的健康检查工做:若是Nginx代理的后端某台Web服务器宕机了,不会影响前端的访问;
- (8)节省带宽:支持GZIP压缩,能够添加到浏览器本地缓存的Header头;
- (9)稳定性高:用于反向代理,宕机的几率微乎其微;
本篇博文中所需使用的软件包都已经打包了,能够直接下载Nginx软件包nginx
所谓Nginx平滑升级就是当前服务器正在运行Nginx服务,想将当前运行的Nginx服务的版本进行升级,且在服务不中止的前提进行升级。web
实现思路:正则表达式
- 在不中止老进程的状况下,启动新进程;
- 老进程负责处理仍然没有处理完成的请求,但再也不接收处理请求;
- 新进程接收新请求;
- 老进程处理完全部请求,关闭全部链接后,中止;
实现步骤:apache
[root@localhost ~]# yum -y install pcre-devel openssl-devel //安装nginx所需依赖 [root@localhost ~]# tar zxf nginx-1.14.0.tar.gz -C /usr/src [root@localhost ~]# cd /usr/src/nginx-1.14.0/ [root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module && make && make install //编译安装nginx1.14版本,因为实验环境,配置项较少 [root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin //建立符号连接 [root@localhost ~]# nginx //启动nginx服务 [root@localhost ~]# nginx -v //查看Nginx服务的版本信息 nginx version: nginx/1.14.0 [root@localhost ~]# tar zxf nginx-1.2.4.tar.gz -C /usr/src [root@localhost ~]# cd /usr/src/nginx-1.2.4/ [root@localhost nginx-1.2.4]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module && make //配置、编译nginx1.2.4版本,注意不要进行安装,能够根据须要添加配置项,可是本来的配置必须存在 [root@localhost ~]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old //备份旧版本的nginx的执行程序 [root@localhost ~]# cp /usr/src/nginx-1.2.4/objs/nginx /usr/local/nginx/sbin/ //替换旧的Nginx的执行程序 [root@localhost ~]# netstat -anpt | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4655/nginx: master [root@localhost ~]# kill -USR2 4655 //建议针对nginx的进程号进行操做,不建议针对nginx的pid文件进行操做 //生成新的进程去接收客户端请求,执行完成后nginx安装目录下logs目录会出现一个nginx.pid.old文件,用来存放旧版的pid信息 [root@localhost ~]# nginx -s reload //从新加载新版的nginx配置 [root@localhost ~]# kill -HUP 4655 //平滑的重启新版的nginx进程 [root@localhost ~]# nginx -v //查看nginx版本信息 nginx version: nginx/1.2.4 [root@localhost ~]# curl -I 127.0.0.1 HTTP/1.1 200 OK Server: nginx/1.14.0 //头部信息版本还未更改 Date: Sun, 01 Dec 2019 06:04:10 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Sun, 01 Dec 2019 05:59:29 GMT Connection: keep-alive ETag: "5de356c1-264" Accept-Ranges: bytes [root@localhost ~]# kill -QUIT 4655 ////平滑的关闭旧版的nginx进程 [root@localhost ~]# nginx -v //查看nginx版本信息 nginx version: nginx/1.2.4 [root@localhost sbin]# curl -I 127.0.0.1 HTTP/1.1 200 OK Server: nginx/1.2.4 //注意版本信息,已经成功发生改变 Date: Sat, 30 Nov 2019 14:47:53 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Sat, 30 Nov 2019 14:42:09 GMT Connection: keep-alive Accept-Ranges: bytes
注意:整个过程当中,建议针对进程号进行平滑升级、重启、关闭等操做!vim
关于nginx使用kill命令经常使用的参数:后端
- QUIT 平滑关闭
- HUP 平滑重启,从新加载配置文件
- USR1 从新打开日志文件
- USR2 平滑升级可执行程序
- WINCH 平滑关闭工做进程
[root@localhost ~]# vim /usr/src/nginx-1.2.4/src/core//nginx.h ……………… //省略部份内容 #define nginx_version 1002004 #define NGINX_VERSION "8.8.8.8" //根据实际状况修改成本身想要的信息 #define NGINX_VER "lzj/" NGINX_VERSION //同上,注意修改完的lzj [root@localhost ~]# vim /usr/src/nginx-1.2.4/src/http/ngx_http_header_filter_module.c ……………… //省略部份内容 static char ngx_http_server_string[] = "Server: lzj" CRLF; //与上一个文件中修改的名称同样(lzj) static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF; [root@localhost ~]# vim /usr/src/nginx-1.2.4/src/http/ngx_http_special_response.c ……………… //省略部份内容 static u_char ngx_http_error_tail[] = "<hr><center>lzj</center>" CRLF //注意与上两个文件中修改的lzj要一致 "</body>" CRLF "</html>" CRLF ; [root@localhost ~]# cd /usr/src/nginx-1.2.4/ [root@localhost nginx-1.2.4]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module && make [root@localhost ~]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak [root@localhost ~]# cp /usr/src/nginx-1.2.4/objs/nginx /usr/local/nginx/sbin/ [root@localhost ~]# nginx -s stop //中止nginx服务 [root@localhost ~]# nginx //开启nginx服务 [root@localhost ~]# curl -I 127.0.0.1 HTTP/1.1 200 OK Server: lzj/8.8.8.8 //查看版本信息 Date: Sat, 30 Nov 2019 15:06:32 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Sat, 30 Nov 2019 14:42:09 GMT Connection: keep-alive Accept-Ranges: bytes
注意:修改nginx版本信息,就须要重启服务,因此若是想要修改则尽可能在安装以前就进行修改!设计模式
在nginx的配置文件中,有一个http{}的段落,在http{}中还包含了server{},其中一个server{}则表明一个虚拟主机,实现方法以下:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf //编辑Nginx的主配置文件,实现相同IP,不一样域名进行访问 …………………………………… //省略部份内容 server { listen 80; server_name www.lzj.com; location / { root /lzj; index index.html index.htm; } } server { listen 80; server_name www.zhj.com; location / { root /zhj; index index.html index.htm; } } [root@localhost ~]# mkdir /lzj //自行建立各自的首页文件 [root@localhost ~]# echo "www.lzj.com" >> /lzj/index.html [root@localhost ~]# mkdir /zhj [root@localhost ~]# echo "www.zhj.com" >> /zhj/index.html [root@localhost ~]# echo "192.168.1.8 www.lzj.com" >> /etc/hosts //在本地hosts文件添加相应的域名 [root@localhost ~]# echo "192.168.1.8 www.zhj.com" >> /etc/hosts [root@localhost ~]# nginx -t //检查nginx配置文件有无语法错误 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost ~]# nginx -s reload //从新加载nginx配置文件 [root@localhost ~]# curl www.lzj.com //验证效果 www.lzj.com [root@localhost ~]# curl www.zhj.com www.zhj.com
主要介绍一下nginx配置文件server{}段落中的location的详细配置
“=”号表示绝对匹配,访问网页的根目录能够,可是访问后面天啊及参数就不能够了,好比:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf //编辑Nginx主配置文件 ……………………………… //省略部份内容 server { listen 80; server_name localhost; location = /test { //寻找网页根目录下的test目录中的内容 root test; //寻找的路径为/usr/lcoal/nginx/html/test/目录中的首页文件 index index.html index.htm; } [root@localhost ~]# mkdir /usr/local/nginx/html/test [root@localhost ~]# echo "test" >> /usr/local/nginx/html/test/index.html //建立测试文件 [root@localhost ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost ~]# nginx -s reload
客户端访问效果:
root:实际访问的文件会被拼接URL的路径;
alias:实际访问的文件路径不会拼接URL的路径;
使用root路径:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf location ^~ /www { //^表示以什么开头,~表示使用正则表达式 root html; //root:实际访问的文件路径会拼接URL的路径,这里的html是相对路径 index index.html index.htm; //那么访问的路径就是/usr/lcoal/nginx/html/www } [root@localhost ~]# mkdir /usr/local/nginx/html/www [root@localhost ~]# echo "www" >> /usr/local/nginx/html/www/index.html //建立测试文件 [root@localhost ~]# nginx -s reload //从新加载nginx的配置文件
访问效果以下:
使用alias路径:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf location ^~ /www { alias html; //alias:实际访问的路径不会拼接URL的路径 index index.html index.htm; } [root@localhost ~]# nginx -s reload //从新加载nginx的配置文件
访问效果以下:
实例一:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf ............... //省略部份内容 location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { root /www; //当用户访问的是gif、jpg等文件时,去/www目录下寻找 index index.html index.htm; } location / { root html; index index.html index.htm; } [root@localhost ~]# ls /www a.jpg [root@localhost ~]# nginx -s reload //从新加载配置文件
客户端访问:
实例二:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf ............... //省略部份内容 location ~* .(gif|jpg|jpeg|png|css|js|ico)$ { rewrite .(gif|jpg) /error.png; //当客户端访问的是jpg等结尾的文件时,自动跳转到error.png } //error.png的存在位置就是网页根目录,由于是“/error.png” [root@localhost ~]# ls /usr/local/nginx/html/ 50x.html error.png index.html [root@localhost ~]# nginx -s reload
客户端访问测试:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf ............... //省略部份内容 if ($request_method = BDQN) { return 666; //当客户端访问BDQN的方式访问时,返回状态码为666 } [root@localhost ~]# nginx -s reload //从新加载配置文件
访问效果以下:
curl命令经常使用参数:
- -X:请求的方式;
- -I:返回服务器响应头部报文
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf ............... //省略部份内容 if ($host != 'www.test.com') { rewrite ^/(.*)$ https://www.baidu.com/$1; } //当客户端不是经过www.test.com 方式访问就会跳转到百度的页面 [root@localhost ~]# nginx -s reload //从新加载配置文件
访问效果以下:
咱们都知道http是80端口,https是443端口,因为https更加安全,因此如今大多数web服务都是经过https方式进行访问的,接下来,就配置一下https访问nginx服务器。
因为互联网认证的CA证书须要付费购买,实验环境因此这里就本身作一个,没有通过互联网认证的CA证书。方法以下:
[root@localhost ~]# mkdir /usr/local/nginx/ca //建立一个目录用于存放ca证书、秘钥 [root@localhost ~]# cd /usr/local/nginx/ca/ [root@localhost ca]# openssl genrsa -out ca.key 4096 //生成秘钥文件 Generating RSA private key, 4096 bit long modulus ..............................++ .....................................................................................................++ e is 65537 (0x10001) [root@localhost ca]# openssl req -new -x509 -days 7304 -key ca.key -out ca.crt //经过密钥生成证书文件,如下内容随意填写 You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:zh State or Province Name (full name) []:beijing Locality Name (eg, city) [Default City]:beijing Organization Name (eg, company) [Default Company Ltd]:beijing Organizational Unit Name (eg, section) []:beijing Common Name (eg, your name or your server's hostname) []:beijing Email Address []:beijing [root@localhost ca]# ls //确认目录下有这两个文件 ca.crt ca.key [root@localhost ca]# vim /usr/local/nginx/conf/nginx.conf ............... //省略部份内容 server { listen 443 ssl; //使用ssl加密 server_name localhost; ssl on; //启用ssl ssl_certificate /usr/local/nginx/ca/ca.crt; //证书存放路径 ssl_certificate_key /usr/local/nginx/ca/ca.key; //秘钥存放路径 ssl_session_timeout 5m; //session会话超时时间 ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; } } //配置文件末尾存放,启用便可! [root@localhost ~]# nginx -s reload //重载nginx配置文件
访问效果:
有些时候,咱们web服务的一些页面,不方便对全部人开放,这事,能够开启该网页的访问认证,开启后,就须要使用用户名密码进行登陆,才可看到相应的页面。
若是不开启认证方式的话,用户就能够直接访问网站内容,以下:
开启认证,方法以下:
[root@localhost ~]# yum -y install httpd-tools //安装htpassword工具 [root@localhost ~]# htpasswd -c /usr/local/nginx/.passwd lzj New password: Re-type new password: Adding password for user lzj //用户认证信息存放路径是/usr/local/nginx/.passwd //若要向.passwd中添加第二个用户,须要省略“-c”选项,不然会覆盖以前的全部用户。 [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf ............... //省略部份内容 location / { root html; index index.html index.htm; auth_basic "请输入登陆帐号"; //添加提示语句 auth_basic_user_file /usr/local/nginx/.passwd; //认证信息存放路径 } [root@localhost ~]# nginx -s reload //重载nginx配置文件
访问测试:
———————— 本文至此结束,感谢阅读 ————————