andy27367451 发表于 2018-11-12 08:53:44

Nginx应用实践入门

  一、HTTP协议包含很多功能
  www是http功能之一
  www服务端口默认是80,OSI 第7层 应用层协议
  二、实现WWW服务的常用Web软件
  产品:nginx,apache(静态Web软件)
  三、经典的web组合
  LAMP(Linux apache mysql php)==》经典
  LNMP(Linux Nginx mysql php)==》国内非常流行
  四、Nginx介绍
  Nginx www服务软件,俄罗斯人开发,开源,性能很高
  Nginx本身是一款静态(html,js,css,jpg等)www软件,静态小文件(1M),高并发。
  同时占用的资源很少。3万并发进程,内存150M,不能解析PHP,JSP。
  Nginx 使用平台:unix,linux,windows即可使用。
  淘宝更改的nginx:tengine(http://tengine.taobao.org/)
# curl -I www.taobao.com  
HTTP/1.1 302 Found
  
Server: Tengine
  五、Nginx服务从大的方面的功能
  a. www web服务,邮件服务,邮件代理
  b. 负载均衡(反向代理proxy)
  c. webcache(web)缓存,相当于squid(CDN主要使用Squid)。
  六、Nginx特点
  1、配置简单,灵活,轻量。
  2、高并发(静态小文件),静态几万的并发
  3、占用资源少。2W并发,开10个线程服务,内存消耗几百M
  4、功能各类比较多(web,cache,proxy),每一个功能都不是特别强的功能
  5、支持epool模型。使得Nginix可以支持高并发。Apache选择的select模型。
  6、Nginx可以配合动态PHP服务(FastCGI接口)
  7、利用Nginx可以实现IP限制,可以限制连接数。
  它所具备 的其他WWW服务特性如下:
  支持基于名字、端口以及IP的多虚拟主机站点。
  可进行简单、方便、灵活的配置和管理。
  支持修改Nginx配置,并且在代码上线时,可以平滑重启,不中断业务访问。
  可自定义访问日志格式,临时缓冲写日志操作,快速日志轮训及通过syslog处理日志;
  可利用信息控制Nginix进程
  支持3xx-5xx http状态码重定向
  支持rewrite模块,支持URL重写及正规表达式匹配
  支持基于客户端IP地址和HTTP基本认证的访问控制
  支持PUT,DELETE,MKCOL,COPY以及MOVE等http请求
  方法:
  支持FLV流和MP4流技术产品应用;
  支持http响应速率限制
  支持同一IP地址的并发连接或请求数限制
  七、Nginx的应用场合
  1、提供静态服务器(图片、视频服务),另一个lighttpd。并发:几万并发
  2、提供动态服务,Nginx+Fastcgi的方式运行PHP,JSP。动态并发:500-1500
  3、提供反向代理,或称称为负载均衡。日PV2000万以下,并发1-2万都可以直接用Nginx做理。(相当于Haproxy,suid,F5,A10)
  4、提供缓存服务。类似Suid,varnish,ats。
  Nginx主机应用场景:
  1、Web服务器(首选)
  静态文件,nginx首选
  动态文件,配置fastcgi支持php
  2、反向代理(负载均衡),1000-2000W PV,并发6000
  3、缓存(使用的并不多)
  八、Nginx支持虚拟主机
  一个Server标签段就是一个虚拟主机
  1、基于域名的虚拟主机,通过域名来区分虚拟主机==》应用:外部网站
  2、基于端口的虚拟主机。通过端口来区分虚拟主机==》应用:公司内部网站,网站的后台。
  3、基于IP的虚拟主机。几乎不用。不支持ifconfig别名,配置文件可以。
  九、Nginx的安装
  Nginx官方地址:http://nginx.org/
  1、安装RCRE
  Pcre全称(Perl Compatible Regular Expressions)中文perl兼容正规表达示。
  官方地址:https://sourceforge.net/projects/pcre/files/pcre/
  下载的版本:pcre-8.30
  下载的地址:http://jaist.dl.sourceforge.net/project/pcre/pcre/8.30/pcre-8.30.tar.gz
  但是在生产环境中,我们一般使用yum安装。
  安装完成后,nginx会支持伪静态等perl的正规表达示。注:必装
  检查工作:检查自己的系统版本的内核版本,并检查是否此台服务器已经安装pcre了
  下面的结果已经显示出来pcre安装过了,但是没有安装pcre的开发包devel。
# cat /etc/redhat-release  
CentOS release 6.5 (Final)
  
# uname -r
  
2.6.32-431.23.3.el6.x86_64
  
# rpm -qa pcre pcre-devel
  
pcre-7.8-6.el6.x86_64
  yum安装pcre开发包
# yum -y install pcre pcre-devel  
Running Transaction
  
Updating   : pcre-7.8-7.el6.x86_64                                        1/3
  
Installing : pcre-devel-7.8-7.el6.x86_64                                  2/3
  
Cleanup    : pcre-7.8-6.el6.x86_64                                        3/3
  
Verifying: pcre-7.8-7.el6.x86_64                                        1/3
  
Verifying: pcre-devel-7.8-7.el6.x86_64                                  2/3
  
Verifying: pcre-7.8-6.el6.x86_64                                        3/3
  

  
Installed:
  
pcre-devel.x86_64 0:7.8-7.el6
  

  
Updated:
  
pcre.x86_64 0:7.8-7.el6
  

  
Complete!
  再次检查安装的pcre包,检查已经发现devel包已经安装成功,结果如下:
# rpm -qa pcre pcre-devel  
pcre-7.8-7.el6.x86_64
  
pcre-devel-7.8-7.el6.x86_64
  2、安装opnssl开发包
  该服务是支持SSL 443的服务,也是必须安装的
# yum install -y openssl-devel  
Installed:
  
openssl-devel.x86_64 0:1.0.1e-48.el6_8.1
  

  
Dependency Installed:
  
keyutils-libs-devel.x86_64 0:1.4-5.el6
  
krb5-devel.x86_64 0:1.10.3-57.el6
  
libcom_err-devel.x86_64 0:1.41.12-22.el6
  
libselinux-devel.x86_64 0:2.0.94-7.el6
  
libsepol-devel.x86_64 0:2.0.41-4.el6
  
zlib-devel.x86_64 0:1.2.3-29.el6
  

  
Dependency Updated:
  
e2fsprogs.x86_64 0:1.41.12-22.el6      e2fsprogs-libs.x86_64 0:1.41.12-22.el6
  
keyutils-libs.x86_64 0:1.4-5.el6       krb5-libs.x86_64 0:1.10.3-57.el6
  
libcom_err.x86_64 0:1.41.12-22.el6   libselinux.x86_64 0:2.0.94-7.el6
  
libselinux-utils.x86_64 0:2.0.94-7.el6 libss.x86_64 0:1.41.12-22.el6
  
openssl.x86_64 0:1.0.1e-48.el6_8.1
  

  
Complete!
  查安装的openssl包,检查已经发现devel包已经安装成功,结果如下:
# rpm -qa openssl*  
openssl-devel-1.0.1e-48.el6_8.1.x86_64
  
openssl-1.0.1e-48.el6_8.1.x86_64
  3、安装Nginx服务
  检查是否本台服务器安装了nginx,发现并没有安装,如下:
# rpm -qa nginx  
#
  本次版本使用1.6.3,stable是稳定版,也可以使用1.8.0
  开始准备安装
  下载地址:http://nginx.org/download/nginx-1.6.3.tar.gz
  在本地创建一个专于用下载的目录,并进行下载。
# mkdir /home/tools/ -p  
# cd /home/tools/
  
# wget http://nginx.org/download/nginx-1.6.3.tar.gz
  
--2016-06-25 21:34:07--http://nginx.org/download/nginx-1.6.3.tar.gz
  
Resolving nginx.org... 95.211.80.227, 206.251.255.63, 2001:1af8:4060:a004:21::e3
  
Connecting to nginx.org|95.211.80.227|:80... connected.
  
HTTP request sent, awaiting response... 200 OK
  
Length: 805253 (786K)
  
Saving to: “nginx-1.6.3.tar.gz”
  

  
100%[======================================>] 805,253      515K/s   in 1.5s
  

  
2016-06-25 21:34:10 (515 KB/s) - “nginx-1.6.3.tar.gz” saved
  

  
# ls
  
nginx-1.6.3.tar.gz
  创建nginx专用用户并检查
  -s /sbin/nologin是不允许登陆,-M是不创建家目录
# useradd nginx -s /sbin/nologin -M  
# cat /etc/passwd|grep nginx
  
nginx:x:500:500::/home/nginx:/sbin/nologin
  思路:解压、编译安装,并使用echo $?查看返回值是否为0,非0都是错误的。
# tar -xf nginx-1.6.3.tar.gz  
# cd nginx-1.6.3
  
# ls
  
auto   CHANGES.ruconfigurehtml   man   src
  
CHANGESconf      contrib    LICENSEREADME
  
# ./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
  

  
Configuration summary
  
+ using system PCRE library
  
+ using system OpenSSL library
  
+ md5: using OpenSSL library
  
+ sha1: using OpenSSL library
  
+ using system zlib library
  

  
nginx path prefix: "/app/application/nginx-1.6.3"
  
nginx binary file: "/app/application/nginx-1.6.3/sbin/nginx"
  
nginx configuration prefix: "/app/application/nginx-1.6.3/conf"
  
nginx configuration file: "/app/application/nginx-1.6.3/conf/nginx.conf"
  
nginx pid file: "/app/application/nginx-1.6.3/logs/nginx.pid"
  
nginx error log file: "/app/application/nginx-1.6.3/logs/error.log"
  
nginx http access log file: "/app/application/nginx-1.6.3/logs/access.log"
  
nginx http client request body temporary files: "client_body_temp"
  
nginx http proxy temporary files: "proxy_temp"
  
nginx http fastcgi temporary files: "fastcgi_temp"
  
nginx http uwsgi temporary files: "uwsgi_temp"
  
nginx http scgi temporary files: "scgi_temp"
  

  
# echo $?
  
0
  
make: Leaving directory `/home/tools/nginx-1.6.3'
  
# echo $?
  
0
  
# make install
  
make: Leaving directory `/home/tools/nginx-1.6.3'
  注:./config是可以--help的,查看你安装的模块
# ./configure --help  

  
--help                           print this message
  

  
--prefix=PATH                      set installation prefix
  
--sbin-path=PATH                   set nginx binary pathname
  
--conf-path=PATH                   set nginx.conf pathname
  
--error-log-path=PATH            set error log pathname
  
--pid-path=PATH                  set nginx.pid pathname
  
--lock-path=PATH                   set nginx.lock pathname
  

  
--user=USER                        set non-privileged user for
  
                                     worker processes
  
--group=GROUP                      set non-privileged group for
  
                                     worker processes
  
--with-http_ssl_module             enable ngx_http_ssl_module
  
--with-http_stub_status_module   enable ngx_http_stub_status_module
  解释:
  ./configure 是生成makefile文件,实际是个配置过程
  make 就是编译生成的配置makefile文件
  make install 是真正安装的过程
  参数说明:
  --prefix= 指定安装的目录
  --user= 指定nginx的用户是什么,不指定默认nobody
  --group= 指定nginx的组是什么,不指定默认nobody
  --with-http_ssl_module 指定nginx使用ssl模块,支持443端口的https
  --with-http_stub_status_module状态模块,比如检查连接数等等,可以用来监控。
  安装的最后于一步,创建软连接,ln一定要写全路径
# cd /application/  
# ls
  
nginx-1.6.3
  
# ln -s /application/nginx-1.6.3/ /application/nginx
  
# ll
  
total 4
  
lrwxrwxrwx 1 root root   25 Jun 25 21:59 nginx -> /application/nginx-1.6.3/
  
drwxr-xr-x 6 root root 4096 Jun 25 21:59 nginx-1.6.3
  4、启动Nginx,并检查
  执行完不报错,一般情况下说明启动成功了
  进程中有一个master可以理解成领导,实际上功作的是woker
# /application/nginx/sbin/nginx  
# ps -ef|grep nginx|grep -v grep
  
root      4597   10 22:08 ?      00:00:00 nginx: master process /application/nginx/sbin/nginx
  
nginx   459845970 22:08 ?      00:00:00 nginx: worker process
  检查端口,如下,80已经启动了
# netstat -lntup  
Active Internet connections (only servers)
  
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
  
tcp      0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4597/nginx
  本地使用curl命令检测是否启动成功
# curl 127.0.0.1  

  

  

  
Welcome to nginx!
  通过浏览器访问,如果不通,检查一下iptables

  5、查看nginx的编译过的参数
  如果到一家新公司,想查看安装时是如何编译的,使用-V,方法如下:
# /application/nginx/sbin/nginx -V  
nginx version: nginx/1.6.3
  
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
  
TLS SNI support enabled
  
configure arguments: --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
  6、排错
  错误1:可能是iptables规则不允许
  错误2:selinux
  排错日志:/var/log/messages
  /nginx/log/error.log
  十、部署一个WEB页面
  1、了解Nginx重要目录
  排除掉nginx目录下的tmp目录
  conf:配置文件目录
  html:默认网站目录
  sbin:启动命令
  logs:错误,访问,pid目录
# cd /application/nginx/  
# cd /
  
# cd /application/nginx/
  
# ls -v |grep -v temp
  
conf
  
html
  
logs
  
sbin
  2、部署一个默认站点(html目录)
  进入到html目录,将原来的index.html改名,并重新写一个index.html
# cd html/  
# ls
  
50x.htmlindex.html
  
# mv index.html index.html.bak
  
# ls
  
50x.htmlindex.html.bak
  
# vim index.html
  

  
Qinglin.s Nginx Server Test Page
  

  
Hi,This My Test Page!
  
My Blog address is Qinglin Blog
  

  

  改html的内容不需要重启nginx,现在测试一下页面是否可以访问

  十一、Nginx模块汇总
  模块文档:http://nginx.org/en/docs/
  http://tengine.taobao.org/documentation_cn.html
  常用模块汇总表:

  十二、Nginx目录结构
# tree nginx  
nginx
  
├── client_body_temp            ==> 客户端内容的临时文件
  
├── conf                        ==> 这是Nginx的所有配置文件的目录
  
│   ├── fastcgi.conf
  
│   ├── fastcgi.conf.default
  
│   ├── fastcgi_params          ==> 配置PHP的动态配置文件
  
│   ├── fastcgi_params.default==> 以.default都是nginx自动帮助备份的配置文件
  
│   ├── koi-utf
  
│   ├── koi-win
  
│   ├── mime.types
  
│   ├── mime.types.default
  
│   ├── nginx.conf            ==> 核心文件,主要配置nginx静态
  
│   ├── nginx.conf.default      ==> 以.default都是nginx自动帮助备份的配置文件
  
│   ├── scgi_params
  
│   ├── scgi_params.default
  
│   ├── uwsgi_params
  
│   ├── uwsgi_params.default
  
│   └── win-utf
  
├── fastcgi_temp
  
├── html                        ==> 默认的站点目录
  
│   ├── 50x.html                ==> 错误页面替代显示文件,例如502就会调用该页面
  
│   ├── index.html            ==> 首页文件在nginx定义好的默认html欢迎页
  
│   └── index.html.bak
  
├── logs                        ==> Nginx日志目录的默认路径,包括错误日志及访问日志
  
│   ├── access.log            ==> 访问日志
  
│   ├── error.log               ==> 错误日志,nginx故障问题会呈现到这里
  
│   └── nginx.pid               ==> Nginx的pid文件,Nginx进程启动后,会把PID号写到这里
  
├── proxy_temp                  ==> 代表的临时文件
  
├── sbin                        ==> 这是Nginx命令的目录,如Nginx的启动命令
  
│   └── nginx                   ==> Nginx的启动文件命令
  
├── scgi_temp                   ==> 临时文件
  
└── uwsgi_temp                  ==> 临时文件
  

  
9 directories, 22 files
  十三、Nginx主配置文件框架(nginx.conf)
# cat nginx.conf  

  
usernobody;             ==> 指定nginx使用的用户
  
worker_processes1;      ==> 指定几个worker进程有几个,填写时参考CPU核数
  

  
error_loglogs/error.log;         ==> 错误日志路径
  
error_loglogs/error.lognotice;   ==> 日志级别警告
  
error_loglogs/error.loginfo;   ==> 日志信息
  

  
pid      logs/nginx.pid;         ==> pid(进程标识符)
  

  

  
events {                           ==> 事件区块开始
  
    worker_connections1024;      ==> 每个work进程支持最大连接数,在这里设置,可以理解成并发,算并法是worker数*1024
  
}
  

  

  
http {                              ==> HTTP区块开始
  
    include       mime.types;         ==> 指定mime类型,类型mime.type定义
  
    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;==> 访问日志
  

  
    sendfile      on;               ==> 开启高效文件传输模式
  
    tcp_nopush   on;                  ==> 防止网络阻塞
  
    keepalive_timeout65;            ==> 长连接超时时间,单位是秒
  

  
    gzipon;                           ==> 开启gzip压缩输出
  

  
    server {                            ==> 一个server标签就是一个虚拟机
  
      listen       80;                ==> 服务的端口
  
      server_namelocalhost;         ==> 配置域名
  

  
      access_loglogs/host.access.logmain;
  

  
      location / {                  ==> 以/后开始的匹配的URI,比如/123/index.html
  
             root   html;               ==> 存放html的路径,默认在配置文件中的html里
  
            indexindex.html index.htm;==> 默认的首页文件,多个空格分开
  
      }
  
  注:如果location页面为/50x.html就优化在=号的location里找。
  十四、基于域名配置虚拟主机
  将配置文件最少化,grep排除掉备份使用的nginx.conf.default重定向到nginx.cnf,这样就没有注释的内容。
# grep -Ev "#|^$" nginx.conf.default >nginx.conf  
# cat nginx.conf
  
worker_processes1;
  
events {
  
    worker_connections1024;
  
}
  
http {
  
    include       mime.types;
  
    default_typeapplication/octet-stream;
  
    sendfile      on;
  
    keepalive_timeout65;
  
    server {
  
      listen       80;
  
      server_namelocalhost;
  
      location / {
  
            root   html;
  
            indexindex.html index.htm;
  
      }
  
      error_page   500 502 503 504/50x.html;
  
      location = /50x.html {
  
            root   html;
  
      }
  
    }
  
}
  创建两个不同域名虚拟主机,一个是qinglin.com和guanqinglin.com
  下列标红的为修改的重点内容,一个是指定域名,一个是指定html目录
# vim nginx.conf  
    worker_connections1024;
  
}
  
http {
  
    include       mime.types;
  
    default_typeapplication/octet-stream;
  
    sendfile      on;
  
    keepalive_timeout65;
  
    server {
  
      listen       80;
  
      server_namewww.qinglin.com;
  
      location / {
  
            root   html/qinglin;
  
            indexindex.html index.htm;
  
      }
  
      }
  
    server {
  
      listen       80;
  
      server_namewww.guanqinglin.com;
  
      location / {
  
            root   html/guanqinglin;
  
            indexindex.html index.htm;
  
      }
  
      }
  创建root定义的qinglin和guanqinglin的目录,并创建index.html文件
# mkdir /application/nginx/html/{qinglin,guanqinglin} -p  
# echo "qinglin" >/application/nginx/html/qinglin/index.html
  
# echo "guanqinglin" >/application/nginx/html/guanqinglin/index.html
  
# cat /application/nginx/html/qinglin/index.html
  
qinglin
  
# cat /application/nginx/html/guanqinglin/index.html
  
guanqinglin
  检查语法并平滑重启服务
# /application/nginx/sbin/nginx -t  
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
  
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  
# /application/nginx/sbin/nginx -s reload
  
#
  修改客户机hosts来校验结果


  注:虽然指定了不同域名不同主机,但是使用IP访问,他会给第一个虚拟主机的配置
  配置虚拟主机流程
  1)复制一个完整的server标签段,到结尾,注意:要放到http的结束大括号前,也就是server标签段放入httpd段落中。
  2)更改server_name及对应网页的root根目录。
  3)检查配置文件语法,平滑重启。
  4)创建server_name 对应网页的根目录,并且建立测试文件,如果没有index首页会出现404错误。
  5)在客户端对server_name的主机名对host解析或DNS配置,并检查(ping 域名看返回的IP对不对)。
  6)win32 浏览器访问,或者在linux客户端做host解析,用wget或curl访问。
  十四、基于端口配置虚拟主机
  配置虚拟主机监听的端口
# vim nginx.conf  
    worker_connections1024;
  
}
  
http {
  
    include       mime.types;
  
    default_typeapplication/octet-stream;
  
    sendfile      on;
  
    keepalive_timeout65;
  
    server {
  
      listen       8081;
  
      server_namewww.qinglin.com;
  
      location / {
  
            root   html/qinglin;
  
            indexindex.html index.htm;
  
      }
  
      }
  
    server {
  
      listen       8082;
  
      server_namewww.guanqinglin.com;
  
      location / {
  
            root   html/guanqinglin;
  
            indexindex.html index.htm;
  
      }
  
      }
  
"nginx.conf" 26L, 594C written
  检查语法并重新启动
# /application/nginx/sbin/nginx -t  
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
  
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  
# /application/nginx/sbin/nginx -s reload
  
#
  检查端口并测试
# netstat -lntup|grep 808*  
tcp      0      0 0.0.0.0:8081                0.0.0.0:*                   LISTEN      4597/nginx
  
tcp      0      0 0.0.0.0:8082                0.0.0.0:*                   LISTEN      4597/nginx


  十五、基于IP配置虚拟主机
# vim nginx.conf  
    worker_connections1024;
  
}
  
http {
  
    include       mime.types;
  
    default_typeapplication/octet-stream;
  
    sendfile      on;
  
    keepalive_timeout65;
  
    server {
  
      listen       192.168.1.1:80;
  
      server_namewww.qinglin.com;
  
      location / {
  
            root   html/qinglin;
  
            indexindex.html index.htm;
  
      }
  
      }
  
    server {
  
      listen       192.168.1.1:80;
  
      server_namewww.guanqinglin.com;
  
      location / {
  
            root   html/guanqinglin;
  
            indexindex.html index.htm;
  
      }
  
      }
  
"nginx.conf" 26L, 594C written


页: [1]
查看完整版本: Nginx应用实践入门