Nginx初识

## NGINX

  • Nginx简介

    • 是一款开源轻量级的高性能静态资源web服务器、也是非常优秀反向代理服务器、缓存服务器、邮件代理服务器
    • 最早由俄罗斯程序员伊戈尔·赛索耶夫开发,官方网站
    • 理念和apache不一样,占用内存少、并发能力强、在中国大陆绝大部分的网站服务器都在使用
    • 属于web服务器解决方案领域的新贵,市场份额一路上升。
  • Nginx作为web服务器的优点:

    • 在高连接,高并发情况下,Nginx是apache的很不错的替代品,理论支持超50000并发
    • 使用异步非阻塞的epoll模型,内存消耗很少。
    • 配置文件简单
    • Rewrite重写规则强大
    • 内置健康检查功能
    • 节省带宽,支持gzip压缩
    • 稳定性好
    • 模块化设计:模块可以动态编译
    • 热部署:不停机重载配置文件,性能好。
    • 成本低廉(尤其是作为负载均衡)
  • Nginx基本功能:

    • 静态资源的web服务器,http、smtp、pop3协议的反向代理服务器,缓存加速、负载均衡,支持fastcgi(fpm LNMP),uWSGI(python)等,模块化、过滤器zip、图像的大小调整,支持SSL(TLS)
    • 支持Sendfile和sendfile64
  • nginx的安装

    • 下载源码包

      image-20191111140613875

      image-20191111142529322

      wget http://nginx.org/download/nginx-1.17.5.tar.gz
      #或通过浏览器访问官网http://nginx.org/en/download.html进行下载
      #若使用最小化安装方式可使用yum install wget命令安装wget工具
      #wget默认下载到当前目录下,使用-O指定下载文件路径及名称
      
    • 安装依赖及源码安装所需包

      image-20191111142556885

    • 创建nginx运行所使用的系统用户

      image-20191111142612572

    • 解压缩

      image-20191111142649589

    • 编译安装

      image-20191111142821402

      image-20191111144015116

      cd nginx-1.17.5
      ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/aaccess.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio
      #--prefix=/usr/local/nginx 指定程序安装目录
      #--conf-path=/etc/nginx/nginx.conf 指定配置文件路径
      #--error-log-path=/var/log/nginx/error.log 错误日志存放路径
      #--http-log-path=/var/log/nginx/aaccess.log  访问日志存放路径
      #--pid-path=/var/run/nginx.pid 指定pid文件
      #--lock-path=/var/lock/nginx.lock 指定lock文件
      #--user=nginx --group=nginx 指定程序所属主和组
      #--with-http_ssl_module 启用ssl模块
      #--with-http_v2_module 启用http2模块
      #--with-http_dav_module 启用文件自动化管理模块
      #--with-http_stub_status_module 启用网页状态检测模块
      # --with-threads 启用线程池
      #--with-file-aio 启用异步I/O
      #更多参数参考官方文档:http://nginx.org/en/docs/configure.html
      #更多安装过程详情参考官方文档:https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/
      
    • 修改环境变量

      image-20191111155631989

      image-20191111155647109

      image-20191111155701272

    • 修改配置文件

      image-20191111150126847

      image-20191111155455084

      user  nginx nginx; #指定属主和属组
      worker_processes  4; #启用进程数建议<=CPU核心数
      worker_cpu_affinity 0001 0010 0100 1000; #将进程绑定到指定的cpu核心上
      #也可以使用八位二进制表表示,1表示使用该位置上的cpu核心,从右向左数,第几位表示第几个核心
      worker_rlimit_nofile 65535; #单个进程能够打开的最大文件句柄数,建议与系统设定保持一致
      work_priority -10; #调整进程的nice值,root调整范围[-20,19]普通用户[100,139],越小越优先
      
      
      #time_resolution #计时器解析值,减少gettimeofday()系统调用的次数,提升性能
      #error_log  logs/error.log; #指定错误日志路径和级别
      #error_log  logs/error.log  notice;
      #error_log  logs/error.log  info;
      
      #pid        logs/nginx.pid;
      
      
      events {  #进程相关配置
          use epoll; #指定进程所使用的事件模型
      #包括epoll、select、poll、rtsig
          worker_connections  10240; #每个进程最大响应连接数
      }
      
      
    • 使用nginx -t检查配置文件语法格式

      image-20191111155543459

    • 启动服务

      image-20191111155807912

      image-20191111155836736

      #常用操作命令
      nginx #启动服务
      nginx -s SINGLE #发送指定信号
      #常用信号:
      #	reload 重载服务
      #	stop 停止
      #	quit 退出
      #	reopen 重启
      
    • 配置防火墙规则

      image-20191111162426499

      image-20191111162649889

    • 浏览器访问测试

      image-20191111162619621

  • 基于端口的虚拟主机配置

    • 修改主配置文件

      image-20191111160419661

      image-20191111164952540

      image-20191111163149039

      http {
          include       mime.types;
          default_type  application/octet-stream;
      
          log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' #定义日志格式,使用main必须开启
          #                  '$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 {	#类似virtualhost
              listen       80; #监听端口
      #也可以监听指定ip地址的某个端口 Listen 192.168.10.124:8000
              server_name  web1.lg.com; #服务名
      # server_name可以指定一个或多个,名称可以使用正则表达式(~)或通配符
      #匹配规则:高精度优先
      	   charset utf-8; #使用字符集
              
              access_log  logs/host.access.log  main; #访问日志相对路径,
              
              location / { #资源路径映射
                  root   /var/www/html/web1; #网页所在路径(相对或绝对路径)
                  index  index.html index.htm; #主页类型
              }
      #location [=|~|~*|^~|@]
      #= 表示精确匹配
      #^~ 匹配URI的前半部分,不支持正则表达式
      #~ 正则表达式
      #~* 不区分大小写的正则表达式
      #匹配优先级 = ^~ ~ ~*
      #location中还可以使用alias定义别名
      
               error_page  404              /404.html; #404页面,相对路径,指定目录下
      
              error_page   500 502 503 504  /50x.html; #50X页面
              location = /50x.html {
                  root   html;
              }
      
           }   
           server {
              listen       8080;
              server_name web2.lg.com;
              charset utf-8;
          #    listen       somename:8080;
          #    server_name  somename  alias  another.alias;
          
              location / {
                  root  /var/www/html/web2;
                  index  index.html index.htm;
              }   
          }  
      }
      
      #相对路径相对于程序安装路径,这里是/usr/local/nginx/目录下
      
    • 创建指定的目录及文件
      image-20191111163955892

      image-20191111170509864

      image-20191111165449838

      image-20191111165330474

      image-20191111165512729

    • 重载服务

      image-20191111165558862

    • 客户机访问测试

      image-20191111170612235

      image-20191111170622233

  • 匹配配置

    • 修改配置如下:

      image-20191111193838981

    • 分别在指定的目录下创建对应文件夹及文件

      image-20191111190329187

      image-20191111190752714

      image-20191111191357222

    • 重载服务
      image-20191111191520156

    • 测试

      image-20191111192029189
      image-20191111192043807
      image-20191111193651044

  • 基于域名的虚拟主机配置

    • 修改配置文件
      image-20191111194301787
      image-20191111194248550

    • 重载服务
      image-20191111194334043

    • 修改客户机C:\Windows\System32\drivers\etc\hosts文件,添加ip和域名的映射关系

      image-20191111194500508

    • 访问测试
      image-20191111194709627

      image-20191111194723522

  • Nginx访问控制

    • 修改配置文件如下

      image-20191111195544846

      location / {
      	root   /var/www/html/web1;
      	index  index.html index.htm;
      	allow 192.168.10.0/24; #指定允许访问的ip或网段
      	deny all; #拒绝的ip或网络,all表示所有,即默认拒绝所有
      } 
      
    • 或使用用户访问控制
      image-20191111200334531

    • 创建指定的目录及文件
      image-20191111200626517

      image-20191111200708866

      image-20191111200807374

    • 重载服务
      image-20191111200940306

    • 测试

      image-20191111200907556

      image-20191111200930979

  • 搭建https

    • 搭建CA服务器

      • CA服务器生成私钥
        image-20191111201933482

      • 创建证书编号文件并写入序列编号
        image-20191111205656794

      • 进行自签
        image-20191111204340035

      • nginx服务端生成私钥
        image-20191111202504516

      • 生成签证请求
        image-20191111202822181

      • 将请求发送至CA服务器进行签证
        image-20191111202941817

      • CA服务器进行签证
        image-20191111205637056

      • 将签证证书发回nginx服务器
        image-20191111210259683

      • 将nginx服务器的私钥和证书存放至指定路径
        image-20191111210441065

      • 修改配置文件
        image-20191111211732589

        	server {
        		listen       443 ssl;
        server_name www.lg.com;
        		charset utf-8;
        		ssl_certificate /etc/nginx/ssl/nginx.crt;
        		ssl_certificate_key    /etc/nginx/ssl/nginx.private_key;
        
        		ssl_ciphers    HIGH:!aNULL:!MD5;
        		ssl_prefer_server_ciphers       on;
        		location / {
        			root  /var/www/html/www;
        			index  index.html index.htm;
        }   
        	} 
        
      • 创建测试页面
        image-20191111212225315

      • 重载服务
        image-20191111212050086

      • 测试
        image-20191111212346488

        image-20191111212402468
        image-20191111212414703

  • 启用stat服务状态页

    • 修改配置文件
      image-20191111212700950

    • 重载服务
      image-20191111212738652

    • 访问状态页
      image-20191111212851644

      Active connections: 1  #活动连接数量
      server accepts handled requests 
       94 94 452  #分别为接受的连接 处理过的连接 处理的请求
      Reading: 0 Writing: 1 Waiting: 0 
      #分别为 正在接受的请求 完成的请求,处于发送响应报文状态 等待的活动连接请求
      
  • URL重写

    • 修改配置文件定义重写规则
      image-20191111214452165

          server {
              listen       80;
              server_name web2.lg.com;
              charset utf-8;
          #    listen       somename:8080;
          #    server_name  somename  alias  another.alias;
              location / {
                  root  /var/www/html/web2;
                  index  index.html index.htm;
                  rewrite ^/img/(.*)$ /images/$1 break
                  #定义转发规则,将访问img的请求转到images/目录下
                  #break 表示重写结束跳出循环
                  #还可以使用
                  	#last 重写后不被其他规则处理
                  	#redirect 以302状态响应码返回新URL,属于临时重定向
                  	#以301状态响应码返回新URL,属于永久重定向
              }
          }
      
      
      
    • 创建对应目录及文件
      image-20191111214324850
      image-20191111215659408

    • 重载服务
      image-20191111214509602

    • 测试
      image-20191111215602188

  • if上下文

    • 通常定义在location或server上下文中

    • 语法:if (condition)

    • 使用容器:server,location

    • 组成:

      • 变量名(变量值为空时,或者以“0”开始,即为false,其他的均为true)
      • 以变量为基础的比较表达式
      • 可基于正则表达式模式匹配
        • ~:区分大小写模式匹配
        • ~*:不区分大小写的模式匹配检查
      • 测试文件是否存在 -f !-f
    • 测试指定目录是否存在 -d !-d

      • 测试文件是否存在:-e !-e
      • 检查文件是否有执行权:-x !-x
    • 修改配置文件如下

      image-20191111220735136

          server {
              listen       8080;
              server_name web2.lg.com;
              charset utf-8;
      
              location / {
                  root  /var/www/html/web2;
                  index  index.html index.htm;
                  if(!-e $document_root/img/index.html){ #如果不存在/img/index.html则执行重写
                       rewrite ^/img/(.*)$ /images/$1 break;
                  }
              }
          }
      
      
    • 重载配置

      image-20191111220755989

    • 测试

      image-20191111220556832

      image-20191111220818640

    • 为img目录添加index.html文件

      image-20191111220917117

    • 访问测试

      image-20191111221000183

      判断条件成立,不执行重定向

posted @ 2019-11-11 22:16  MirL  阅读(188)  评论(0编辑  收藏  举报