设为首页 收藏本站
查看: 962|回复: 0

[经验分享] nginx网站服务(上)

[复制链接]

尚未签到

发表于 2018-11-8 13:18:24 | 显示全部楼层 |阅读模式
第1章 web网站服务介绍
1.1 常用来提供静态Web服务的软件
  u Apache:这是中小型Web服务的主流,Web服务器中的老大哥。
  u Nginx:大型网站Web服务的主流,曾经Web服务器中的初生牛犊,现已长大。
  Nginx的分支Tengine(http://tengine.taobao.org/)目前也在飞速发展。
  u Lighttpd:这是一个不温不火的优秀Web软件,社区不活跃,静态解析效率很高。在Nginx流行前,
  它是大并发静态业务的首选,国内百度贴吧、豆瓣等众多网站都有Lighttpd奋斗的身影。
1.2 常用来提供动态服务的软件
  u PHP(FastCGI):大中小型网站都会使用,动态网页语言PHP程序的解析容器。它可配合Apache解析动态程序,不过,这里的PHP不是FastCGI守护进程模式,而是mod_php5.so(module)。也可配合Nginx解析动态程序,此时的PHP常用FastCGI守护进程模式提供服务。(.php)
  u Tomcat:中小企业动态Web服务主流,互联网Java容器主流(如jsp、do)。
  u Resin:大型动态Web服务主流,互联网Java容器主流(如jsp、do)。
  u IIS(Internet information services):微软windows下的Web服务软件(如asp、aspx)
第2章 nginx网站服务软件
2.1 Nginx是什么
  如果你听说或使用过Apache软件,那么很快就会熟悉Nginx软件,与Apache软件类似, Nginx(“engine x”)是一个开源的,支持高性能、高并发的WWW服务器和代理服务软件。
  Nginx因具有高并发(特别是静态资源)、占用系统资源少等特性,且功能丰富逐渐流行起来。
  在功能应用方面,Nginx不但是一个优秀的web服务软件,还具有反向代理负载均衡功能和缓存服务功能。在反向代理负载均衡功能方面,它类似与大名鼎鼎的LVS负载均衡及Haproxy等专业的缓存服务软件。
  Nginx可以运行在UNIX、Linux、BSD、Mac OS X、Solarls,以及Microsoft Windows等操作系统中。随着Nginx在国内很多大型网站中的稳定高效运行,近两年它也逐渐被越来越多的中小型网站所使用。当前流行的Nginx Web组合被称为LNMP或LEMP(即Linux Nginx MySql PHP),其中LEMP里的E取自Nginx(“engine x”)
  Nginx的官网:http://nginx.org/
2.2 Nginx的特色及优点
2.2.1 Nginx的重要特性
  u 可针对静态资源高速高并发访问及缓存
  u 可使用反向代理加速,并且可进行数据缓存
  u 具有简单负载均衡、节点健康检查和容错功能
  u 支持远程FastCGI服务的缓存加速
  u 支持FastCGI、Uwsgi、SCGI、Memcached Servers的加速和缓存
  u 支持SSL、TLS、SNI。
  u 具有模块化的架构:过滤器包括gzip压缩、ranges支持、chunked响应、XSLT、SSI及图像缩放等功能。在SSI过滤器中,一个包含多个SSI的页面,如果经由FastCGI或反向代理处理,可被并行处理。
2.2.2 Nginx HTTP服务器的特色及优点(重要)
  u 支持高并发:能支持几万并发连接(特别是静态小文件业务环境)
  u 资源消耗少:在3万并发连接下,开启10个Nginx线程消耗的内存不到200MB(ab webbenh)
  u 可以做HTTP反向代理及加速缓存、即负载均衡功能,内置对RS节点服务器健康检查功能,这相当于专业的Haproxy软件或LVS的功能。
  u 具备Squid等专业缓存软件等的缓存功能。
  u 支持异步网络I/O事件模型epoll(Linux 2.6+)(apache select)
2.3 Nginx软件的主要企业功能应用
2.3.1 作为Web服务软件
  Nginx是一个支持高性能、高并发的Web服务软件,它具有很多优秀的特性,作为Web服务器,与Apache相比,Nginx能够支持更多的并发连接访问,但占用的资源却更少,效率更高,在功能上也强大了很多,几乎不逊于Apache。
2.3.2 反向代理或负载均衡服务
  在反向代理或负载均衡服务方面,Nginx可以作为Web服务,PHP等动态服务及Memcached缓存的代理服务器,它具有类似专业代理软件(如Haproxy)的功能,同时也是一个优秀的邮件代理服务软件(最早开发这个产品的目的之一就是作为邮件代理服务)。Nginx的代理功能在逐渐增强。
2.3.3 前端业务数据缓存服务
  在web缓存服务方面,Nginx可通过自身的proxy_cache模块实现类Squid等专业缓存软件的功能。
  Nginx的这三大功能(web服务、反向代理或负载均衡服务、前端业务数据缓存服务)是国内使用Nginx的主要场景,特别是前两个。
2.4 为什么Nginx总体性能比Apache高
  Nginx使用最新的epoll(Linux 2.6内核)和Kqueue(freebsd)异步网络I/O模型,
  而Apache使用的是传统的select模型,处理大量连接的读写时,Apache所采用的select网络I/O模型比较低效。
  目前Linux先能够承受高并发访问的Squid、Memcached软件采用的都是epoll模型。
  Apache select 和Nginx epoll的技术对比
  指标
  select
  epoll
  性能
  随着连接数的增加性能急剧下降。处理成千上万并发连接数,性能很差
  随着连接数的增加,性能基本上没有下降。处理成千上万并发连接时,性能很好。
  连接数
  连接数有限制,处理的最大连接数不超过1024,如果要处理的连接数超过1024个,则需要修改FD_SETSIZE宏,并重新编译
  连接数无限制
  内在处理机制
  线性轮询
  回调callback
  开发复杂性
  低
  中
第3章 部署nginx软件(编译安装)
3.1 第一个里程碑:进行nginx软件下载
  cd /server/tools
  wget http://nginx.org/download/nginx-1.12.2.tar.gz
  [root@web02 ~]# cd /server/tools/
  [root@web02 tools]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
  --2018-02-02 18:26:50--  http://nginx.org/download/nginx-1.12.2.tar.gz
  Resolving nginx.org... 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3, ...
  Connecting to nginx.org|206.251.255.63|:80... connected.
  HTTP request sent, awaiting response... 200 OK
  Length: 981687 (959K) [application/octet-stream]
  Saving to: “nginx-1.12.2.tar.gz”
  100%[===================================================>] 981,687     23.8K/s   in 43s
  2018-02-02 18:27:40 (22.1 KB/s) - “nginx-1.12.2.tar.gz” saved [981687/981687]
3.2 第二个里程碑:安装依赖软件
  yum install -y pcre-devel openssl-devel
  [root@web02 tools]# yum install -y pcre-devel openssl-devel
  说明,如果安装系统没有安装兼容程序库、开发工具的,请安装一下基础依赖包
  yum install gcc gcc-c++ automake autoconf -y
3.3 第三个里程碑:解压软件,创建虚拟用户,进行编译安装(三步曲)
3.3.1 解压软件,创建虚拟用户
  tar xf nginx-1.12.2.tar.gz
  useradd -M -s /sbin/nologin www
  [root@web02 tools]# tar xf nginx-1.12.2.tar.gz
  [root@web02 tools]# ls
  nginx-1.12.2  nginx-1.12.2.tar.gz
  [root@web02 tools]# useradd -M -s /sbin/nologin www

  [root@web02 tools]#>  uid=502(www) gid=502(www) groups=502(www)
3.3.2 编译第一步:进行配置
  ./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
  配置参数说明
  --prefix=PATH
  set installation prefix
  指定软件程序安装的路径信息
  --user=USER
  set non-privileged user for
  worker processes
  创建一个虚拟用户,用于管理nginx服务的worker进程
  --group=GROUP
  set non-privileged group for
  worker processes
  创建一个虚拟用户组,用于管理nginx服务的worker进程
  --with-http_ssl_module
  enable ngx_http_ssl_module
  让nginx服务可以支持https访问
  --with-http_stub_status_module
  enable ngx_http_stub_status_module
  便于监控软件监视nginx服务运行状态
  [root@web02 tools]# cd nginx-1.12.2
  [root@web02 nginx-1.12.2]# pwd
  /server/tools/nginx-1.12.2
  [root@web02 nginx-1.12.2]# ./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
3.3.3 编译第二步:编译(翻译)
  说明:编译过程实质是将各种程序语言转换为系统可以识别的二进制信息
  make
  [root@web02 nginx-1.12.2]# make
  make -f objs/Makefile
  make[1]: Entering directory `/server/tools/nginx-1.12.2'
  ……
3.3.4 编译第三步:编译安装
  make install
  [root@web02 nginx-1.12.2]# make install
  make -f objs/Makefile install
  make[1]: Entering directory `/server/tools/nginx-1.12.2'
  ……
3.4 第四个里程碑:创建程序目录软链接文件
  ln -s /application/nginx-1.12.2/ /application/nginx
  [root@web02 nginx-1.12.2]# ln -s /application/nginx-1.12.2/ /application/nginx
  [root@web02 nginx-1.12.2]# cd /application/
  [root@web02 application]# ll
  total 4
  lrwxrwxrwx 1 root root   26 Feb  2 19:30 nginx -> /application/nginx-1.12.2/
  drwxr-xr-x 6 root root 4096 Feb  2 19:28 nginx-1.12.2
3.5 第五个里程碑:网站服务启动成功
  /application/nginx/sbin/nginx
  [root@web02 application]# /application/nginx/sbin/nginx
3.6 第六个里程碑:检查测试
  [root@web02 application]# ps -ef |grep nginx
  root       4781      1  0 19:32 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
  www        4782   4781  0 19:32 ?        00:00:00 nginx: worker process
  root       4784   2022  1 19:32 pts/0    00:00:00 grep nginx
  [root@web02 application]# netstat -lntup |grep nginx
  tcp        0      0 0.0.0.0:80                  0.0.0.0:*                 LISTEN      4781/nginx
  说明:master进程表示nginx主进程,负责nginx服务的启动 停止等操作
  worker进程表示真正处理用户请求的进程
第4章 部署nginx软件可能会遇到的问题  
  1. 依赖包软件没有安装
  2. 启动Nginx时如下报错“nginx:[emerg]getpwnam(“nginx”)failed”
  解答:这是因为没有对应的Nginx服务用户,执行useradd nginx -s /sbin/nologin -M创建Nginx用户即可。
  3. 如何查看Nginx编译时的参数?
  解答:可采用如下命令查看:/application/nginx/sbin/nginx -V
  [root@web02 application]# /application/nginx/sbin/nginx -V
  nginx version: nginx/1.12.2
  built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
  built with OpenSSL 1.0.1e-fips 11 Feb 2013
  TLS SNI support enabled
  configure arguments: --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
第5章 nginx软件程序目录结构
5.1 nginx软件程序目录结构
  [root@web02 nginx]# ll
  total 36
  drwx------ 2 www  root 4096 Feb  2 19:32 client_body_temp
  drwxr-xr-x 2 root root 4096 Feb  2 19:28 conf                                  ---配置文件目录
  drwx------ 2 www  root 4096 Feb  2 19:32 fastcgi_temp
  drwxr-xr-x 2 root root 4096 Feb  2 19:28 html                                 ---站点目录
  drwxr-xr-x 2 root root 4096 Feb  2 19:32 logs                                 ---日志目录
  drwx------ 2 www  root 4096 Feb  2 19:32 proxy_temp
  drwxr-xr-x 2 root root 4096 Feb  2 19:28 sbin                                 ---保存服务命令目录
  drwx------ 2 www  root 4096 Feb  2 19:32 scgi_temp
  drwx------ 2 www  root 4096 Feb  2 19:32 uwsgi_temp
5.2 配置文件目录说明
DSC0000.jpg

  mime.types        --- 媒体资源类型文件
  nginx.conf        --- nginx服务主配置文件
  
DSC0001.jpg

5.3 精简nginx配置文件信息
  grep -Ev "#|^$" nginx.conf.default >nginx.conf
  [root@web02 conf]# ll
  total 60
  -rw-r--r-- 1 root root 1077 Feb  2 19:28 fastcgi.conf
  -rw-r--r-- 1 root root 1077 Feb  2 19:28 fastcgi.conf.default
  -rw-r--r-- 1 root root 1007 Feb  2 19:28 fastcgi_params
  -rw-r--r-- 1 root root 1007 Feb  2 19:28 fastcgi_params.default
  -rw-r--r-- 1 root root 2837 Feb  2 19:28 koi-utf
  -rw-r--r-- 1 root root 2223 Feb  2 19:28 koi-win
  -rw-r--r-- 1 root root 3957 Feb  2 19:28 mime.types
  -rw-r--r-- 1 root root 3957 Feb  2 19:28 mime.types.default
  -rw-r--r-- 1 root root  484 Feb  2 20:13 nginx.conf
  -rw-r--r-- 1 root root 2656 Feb  2 19:28 nginx.conf.default
  -rw-r--r-- 1 root root  636 Feb  2 19:28 scgi_params
  -rw-r--r-- 1 root root  636 Feb  2 19:28 scgi_params.default
  -rw-r--r-- 1 root root  664 Feb  2 19:28 uwsgi_params
  -rw-r--r-- 1 root root  664 Feb  2 19:28 uwsgi_params.default
  -rw-r--r-- 1 root root 3610 Feb  2 19:28 win-utf
  [root@web02 conf]# grep -Ev "#|^$" nginx.conf.default >nginx.conf
  [root@web02 conf]# cat nginx.conf
  worker_processes  1;
  events {
  worker_connections  1024;
  }
  http {
  include       mime.types;
  default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  server {
  listen       80;
  server_name  localhost;
  location / {
  root   html;
  index  index.html index.htm;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
  root   html;
  }
  }
  }
  nginx配置文件区域分类
  main区块
  event区块
  http区块
  server区块(可以有多个)
  location区块(可以有多个)
  扩展命令说明
  vimdiff nginx.conf nginx.conf.default    --- 比较两个文件之间配置区别
5.4 nginx服务命令参数说明

  /application/nginx/sbin/nginx -s>  信号信息:reload stop
  /application/nginx/sbin/nginx -t          --- 检查配置文件语法信息
  [root@web02 nginx-1.12.2]# /application/nginx/sbin/nginx -h
  nginx version: nginx/1.12.2
  Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
  Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing

  -s signal     : send signal to a master process: stop, quit, reopen,>  -p prefix     : set prefix path (default: /application/nginx-1.12.2/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file
5.5 实践配置编写配置文件
  [root@web02 conf]# vim nginx.conf
  worker_processes  1;
  events {
  worker_connections  1024;
  }
  http {
  include       mime.types;
  default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  server {
  listen       80;
  server_name  www.wuhuang.com;
  location / {
  root   html/www;
  index  wuhuang.html;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
  root   html;
  }
  }
  }
  编写静态访问页面文件信息
  [root@web02 www]# pwd
  /application/nginx/html/www      站点目录
  [root@web02 www]# cat wuhuang.html
  
  
  
  吾皇
  
  
  吾皇
  
   01 吾皇
   02 巴扎黑
   03 糙汉
  
  
  
  
  
  
  [root@web02 www]# ls
  wuhuang.html  wuhuang.jpg
  修改本地的hosts文件
  C:\Windows\System32\drivers\etc\hosts
  10.0.0.8    www.wuhuang.com
  验证(通过IP地址访问)
DSC0002.jpg

  通过域名访问
DSC0003.jpg



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-632459-1-1.html 上篇帖子: ELK日志服务器的快速搭建并收集nginx日志 下篇帖子: nginx 图片缓存
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表