当前位置: 首页 > 科技观察

Nginx静态资源访问和负载均衡的使用!

时间:2023-03-12 06:46:22 科技观察

本文转载自微信公众号《txp玩转Linux》,作者txp。转载本文请联系txp玩Linux公众号。1.前言最近,我的空闲时间有点少。平时都是晚上下班后看书,所以更新没那么快!还有音视频接口和解封装框架。我几乎把它理顺了。其实之前很多人问接口,可以去ffmpeg官网查看文档说明,上面有很多demo测试用例说明:差不多下周,音视频学习笔记继续更新;今天的文章主要是总结过年期间复习的nginx负载平衡一些实用的演示,配置简单!2、nginx的常见使用这里我主要演示nginx的源码安装和相应的模块安装,然后讲解负载均衡的原理并通过实战进行演示,以及静态资源的访问(比如有很多网上介绍什么是nginx以及它是做什么用的,这里就不赘述了,我会着重实战来分享!1.nginx及相关模块的源码安装:先说Modules源码需要--nginx-1.13.7.ta??r.gz--openssl-1.1.0g.tar.gz--pcre-8.41.tar.gz--zlib-1.2.11.tar.gz对应下载地址:wgethttp://nginx.org/download/nginx-1.13.7.ta??r.gzwgethttps://www.openssl.org/source/openssl-1.1.0g.tar.gzwgethttp://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gzwgethttp://www.zlib.net/zlib-1.2.11.tar.gz(1)解压nginx:(2)解压openssl:(3)解压pcre:(4)解压zlib:(5)配置,先进入nginx,然后然后执行以下语句:./configure--prefix=/usr/local/nginx--with-http_realip_module--with-http_addition_module--with-http_gzip_static_module--with-http_secure_link_module--with-http_stub_status_module--with-stream--with-pcre=/home/txp/share/nginx/pcre-8.41--with-zlib=/home/txp/share/nginx/zlib-1.2.11--with-openssl=/home/txp/share/nginx/openssl-1.1.0g然后直接make:然后sudomakeinstall:最后我们你在/usr/local/nginx/目录下可以看到安装好的nginx:现在我们可以尝试运行nginx并访问它(下面访问成功):这里总结一下:很多开源软件的安装步骤大概就是这样差不多就是下面的套路(比如我们接下来要安装的模块也是这样安装的,这里就不造轮子了)--./cofigure--make--sudomakeinstall2,自己写conf文件在平时的开发过程中,我们主要需要配置它的conf文件夹下的nginx.conf文件root@ubuntu:/usr/local/nginx#lsclient_body_tempconffastcgi_temphtmllogsproxy_tempsbinscgi_tempuwsgi_temp这个文件原来的内容是:#usernobody;worker_processes1;#error_loglogs/错误。日志;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events{worker_connections1024;}http{includemime.types;default_typeapplication/octet-stream;#log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'#'$status$body_bytes_sent"$http_referer"'#'"$http_user_agent""$http_x_forwarded_for"';#access_loglogs/access.logmain;sendfileon;#tcp_nopushon;#keepalive_timeout0;keepalive_timeout65;#gzipon;server{listen80;server_namelocalhost;#charsetkoi8-r;#access_loglogs/host.access.logmain;location/{roothtml;indexindex.htmlindex.htm;}#error_page404/404.html;#redirectservererrorpagestothestaticpage/50x.html#error_page500502503504/50x.html;location=/50x.html{roothtml;}#proxythePHPscriptstoApachelisteningon127.0.0.1:80##location~\.php${#proxy_passhttp://127.0.0.1;#}#passthePHPscriptstoFastCGIserverlisteningon127。0.0.1:9000##location~\.php${#roothtml;#fastcgi_pass127.0.0.1:9000;#fastcgi_indexindex.php;#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;#includefastcgi_params;#}#denyaccessto.htaccessfiles,ifApache'sdocumentroot#concurswithnginx'sone##location~/\.ht{#denyall;#}}#anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration##server{#listen8000;#listensomename:8080;#server_namesomenamealiasanother.alias;#location/{#roothtml;#indexindex.htmlindex.htm;#}#}#HTTPSserver##server{#listen443ssl;#server_namelocalhost;#ssl_certificatecert.pem;#ssl_certificate_keycert.key;#ssl_session_cacheshared:SSL:1m;#ssl_session_timeout5m;#ssl_ciphersHIGH:!aNULL:!MD5;#ssl_prefer_server_cipherson;#location/{#roothtml;#indexindex.html#index.htm;}#}}这里内容很多,我们写个配置文件实现nginx访问:txp@ubuntu:/usr/local/nginx$sudomkdirdemo_conftxp@ubuntu:/usr/local/nginx$cddemo_conf/txp@ubuntu:/usr/local/nginx$vimdemo_conf/demo.confworker_processes4;#进程数events{worker_connections1024;#并发访问数}http{server{listen8888;#监听端口server_namelocalhost;#服务器名client_max_body_size100m;#访问数location/{root/usr/local/nginx/html/#在本机访问这个html页面}}}下面看看我们自己的配置是否成功,先关闭之前的nginx:./sbin/nginx-sstop然后执行这个配置文件:./sbin/nginx-cdemo_conf/demo.conf这里扩展一下基础知识点:Nginx是由配置文件中指定的指令控制的模块组成。指令分为简单指令和块指令。简单指令由空格分隔的名称和参数组成,并以分号(;)结尾。块指令与简单指令具有相同的结构,但它们不是以分号结尾,而是以一组由大括号({和})包围的附加指令结尾。如果块指令可以在大括号内包含其他指令,则称为上下文(例如:事件、http、服务器和位置)。放置在配置文件中任何上下文之外的指令被认为是主要上下文。事件和http指令位于主上下文中,服务器位于http中,位置位于http块中。3、负载均衡、反向代理和访问静态资源的演示:--反向代理原理(ReverseProxy):是指使用代理服务器接受互联网上的连接请求,然后将请求转发给内网服务器.并将从服务器得到的结果返回给互联网上请求连接的客户端。简单的说就是外网不能直接访问真实服务器。如果要访问它,必须要经过代理,如下图:上图中有两个网关,一个是nginx应用层网关,一个是路由器硬件网关,nginx和各个服务器都在同一个局域网;router做了端口映射(nat)直接访问nginx,给人的感觉是nginx在公网,注意这里的服务器不对外提供服务,通过nginx代理对外提供服务;外部访问是公网ip,然后通过端口映射找到nginx现在我们使用nginx做代理配置(比如我这里用143本机代理141本机):worker_processes4;events{worker_connections1024;}http{server{listen8888;server_namelocalhost;client_max_body_size100m;location/{root/usr/local/nginx/html/;proxy_passhttp://192.168.29.141;}}}注:141的机器也安装了nginx,当我访问143的机器时,我实际访问141的机器的内容,就是代理的使用:--负载均衡:从负载均衡四个字来看,肯定是用来降低访问压力的服务器;会崩溃(比如淘宝每年双十一都会用nginx的负载均衡功能,不然那天淘宝上活跃的用户那么多,服务器肯定承受不住!)。因此,为了避免服务器崩溃,让用户有更好的体验,我们采用负载均衡来分担服务器压力。我们可以架设很多很多的服务器,组成一个服务器集群。当用户访问一个网站时,首先访问一个中间服务器(也就是我们的nginx),让中间服务器在服务器集群中选择一个压力较小的服务器,然后将访问请求引入服务器。这样,每次用户访问时,都会保证服务器集群中每台服务器的压力均衡,分担服务器压力,避免服务器崩溃。下面我演示一个负载均衡案例:worker_processes4;events{worker_connections1024;}http{upstreambackend{server192.168.29.142weight=2;//weight表示权重server192.168.29.141weight=1;}server{listen8888;server_namelocalhost;client_max_body_size100m;location/{#root/usr/local/nginx/html/;#proxy_passhttp://192.168.29.141;proxy_passhttp://backend;}}}注:权重意味着访问次数多,因为我三台机器都安装了nginx,所以有内容显示没有区别。实际上142机器被访问了两次,141机器被访问了一次。我这里有三台机器:141、142、143:--访问静态资源(图片和视频)这里我在143机器上放了几张图片,然后在/usr/local/nginx目录下创建了一个images文件夹,然后将143机器上的图片复制到images中:root@ubuntu:/usr/local/nginx#lsclient_body_tempfastcgi_tempimagesproxy_tempscgi_tempvip_confconfhtmllogssbinuwsgi_temproot@ubuntu:/usr/local/nginx#mv/home/txp/share/nginx/*.pngimages/root@ubuntu:/usr/local/nginx#lsclient_body_tempfastcgi_tempimagesproxy_tempscgi_tempvip_confconfhtmllogssbinuwsgi_temproot@ubuntu:/usr/local/nginx#cdimages/root@ubuntu:/usr/local/nginx/images#ls1.png2.png3.png然后配置conf文件:worker_processes4;events{worker_connections1024;}http{upstreambackend{server192.168.29.142weight=2;server192.168.29.141weight=1;}server{listen8888;server_namelocalhost;client_max_body_size100m;location/{#root/usr/local/nginx/html/;#proxy_passhttp://192.168.29.141;proxy_passhttp://backend;}location/images/{root/usr/local/nginx/;}}}实现结果如下:下面我将演示视频访问。同样,我创建一个media目录,然后将143机器上的test.mp4复制到media目录下:root@ubuntu:/usr/local/nginx#mv/home/txp/share/nginx/test.mp4media/root@ubuntu:/usr/local/nginx#cdmedia/root@ubuntu:/usr/local/nginx/media#lstest.mp4conf文件配置:worker_processes4;events{worker_connections1024;}http{upstreambackend{server192.168.29.142weight=2;server192.168.29.141weight=1;}server{listen8888;server_namelocalhost;client_max_body_size100m;location/{#root/usr/local/nginx/html/;#proxy_passhttp://192.168.29.141;proxy_passhttp://backend;}location/images/这里使用了{root/usr/local/nginx/;}location~\.(mp3|mp4)#正则表达式root/usr/local/nginx/media/;}}},结果如下:3.总结今天就总结这么多吧,cgi和fastcgi的使用和区别暂时不说了,如果哪天如果有用,我们来做个实战演示;最后,如果你热爱技术,可以加我微信,进入技术交流群交流学习,共同进步;一般有问题会及时交流学习:tu18879499804站在巨人的肩膀上:https://blog.csdn.net/X1021333506/article/details/80975462?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-10.baidujs&dist_request_id=6c30ed21-fb2d-462c-a4d8-7c5581c1b504&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-10.baidujshttps://juejin.cn/post/6844903944267759624https://ke.qq.com/webcourse/index.html#cid=420945&term_id=102189254&taid=8498121076534353&vid=5285890803239999870