• Nginx入门


    网站服务

    想必我们大多数人都是通过访问网站而开始接触互联网的吧。我们平时访问的网站服务 就是 Web 网络服务,一般是指允许用户通过浏览器访问到互联网中各种资源的服务。

    Web 网络服务是一种被动访问的服务程序,即只有接收到互联网中其他主机发出的 请求后才会响应,最终用于提供服务程序的 Web 服务器会通过 HTTP(超文本传输协议)或 HTTPS(安全超文本传输协议)把请求的内容传送给用户。

    目前能够提供 Web 网络服务的程序有 IIS、Nginx 和 Apache 等。其中,IIS(Internet Information Services,互联网信息服务)是 Windows 系统中默认的 Web 服务程序

    2004 年 10 月 4 日,为俄罗斯知名门户站点而开发的 Web 服务程序 Nginx 横空出世。 Nginx 程序作为一款轻量级的网站服务软件,因其稳定性和丰富的功能而快速占领服务器市 场,但 Nginx 最被认可的还当是系统资源消耗低且并发能力强,因此得到了国内诸如新浪、 网易、腾讯等门户站的青睐。

    web服务器和web框架的关系

    web服务器(nginx):接收HTTP请求(例如www.pythonav.cn/xiaocang.jpg)并返回数据

    web框架(django,flask):开发web应用程序,处理接收到的数据

    nginx

    命令:curl -I baidu.com
    
    [root@localhost ~]# curl -I baidu.com
    HTTP/1.1 200 OK
    Date: Tue, 24 Sep 2019 19:24:44 GMT
    Server: Apache
    Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
    ETag: "51-47cf7e6ee8400"
    Accept-Ranges: bytes
    Content-Length: 81
    Cache-Control: max-age=86400
    Expires: Wed, 25 Sep 2019 19:24:44 GMT
    Connection: Keep-Alive
    Content-Type: text/html
    
    web服务器(nginx):接收HTTP请求(例如www.pythonav.cn/xiaocang.jpg)并返回数据
    web服务器,仅仅就是接受一个web请求,返回一个磁盘上的静态资源(jpg,mp4...)
    
    错误码
    50x 服务器错误,django flask后台蹦了
    40x 客户端错误,权限不足,资源不存在等等...
    30x 资源重定向
    20x 请求正确返回
    
    技术栈:
    贵:
    Java+apache(web服务器,处理静态资源)+oracle(数据库)+tomcat(处理Java应用)+svn(代码托管)+js+jQuery +redhat
    为了省钱,切换开源技术栈
    java + perl + python + nginx + mysql + git + js + centos 
    
    web框架(django,flask):开发web应用程序,处理接收到的数据
    
    nginx安装步骤,源码编译安装(源码编译,开源自定制更多功能)
    1.解决软件正常运转所需依赖包
    yum install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y
    2.下载源代码
    [root@localhost ~]# cd /opt
    [root@localhost opt]# wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
    3.解压缩源码
    [root@localhost opt]# tar -zxvf nginx-1.12.0.tar.gz
    4.进入源码目录,编译安装
    [root@localhost opt]# cd nginx-1.12.0/
    [root@localhost nginx-1.12.0]# ./configure --prefix=/opt/nginx112
    [root@localhost nginx-1.12.0]# make && make install 
    5.进入Nginx功能目录
    cd /opt/nginx112
    6.学习Nginx功能目录
    [root@localhost nginx112]# ls
    conf  配置文件nginx.conf
    html  存放前端页面
    logs  存放Nginx的运行日志,错误日志
    sbin   存放Nginx可执行程序的目录
    7.卸载原本的yum装的Nginx
    [root@localhost nginx112]# yum remove nginx -y
    8.启动
    [root@localhost nginx112]# netstat -tunlp | grep 80
    [root@localhost nginx112]# ps -ef|grep nginx
    root      15678  13010  0 00:45 pts/3    00:00:00 grep --color=auto nginx
    [root@localhost nginx112]# ls
    conf  html  logs  sbin
    [root@localhost nginx112]# cd sbin
    [root@localhost sbin]# ls
    nginx
    [root@localhost sbin]# ./nginx
    [root@localhost sbin]# !ps
    ps -ef|grep nginx
    root      15682      1  0 00:47 ?        00:00:00 nginx: master process ./nginx
    nobody    15683  15682  0 00:47 ?        00:00:00 nginx: worker process
    root      15685  13010  0 00:47 pts/3    00:00:00 grep --color=auto nginx
    [root@localhost sbin]# 
    [root@localhost sbin]# history
        1  pwd
        2  ip a
        3  w
        4  ip a
        ...    
      341  ps -ef|grep nginx
      342  ls
      343  cd sbin
      344  ls
      345  ./nginx
      346  history
    [root@localhost sbin]# !341
    ps -ef|grep nginx
    root      15682      1  0 00:47 ?        00:00:00 nginx: master process ./nginx
    nobody    15683  15682  0 00:47 ?        00:00:00 nginx: worker process
    root      15687  13010  0 00:48 pts/3    00:00:00 grep --color=auto nginx
    [root@localhost sbin]# 
    
    9.添加Nginx的环境变量,可以快捷使用Nginx
    [root@localhost sbin]# pwd
    /opt/nginx112/sbin
    [root@localhost sbin]# vim /etc/profile
    PATH="/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/node-v8.6.0-linux-x64/bin:/opt/nginx112/sbin"
    
    10.读取配置文件 source /etc/profile
    [root@localhost sbin]# nginx
    -bash: nginx: command not found
    [root@localhost sbin]# source /etc/profile
    [root@localhost sbin]# which nginx
    /opt/nginx112/sbin/nginx
    
    11.测试
    192.168.0.101
    
    12.修改配置文件
    
    13.平滑重启nginx
    [root@localhost nginx112]# ls
    client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
    [root@localhost nginx112]# ./sbin/nginx -s reload
    
    
    
    Nginx功能目录
    conf  配置文件nginx.conf (Nginx的功能参数都在这个文件定义)
    html  存放前端页面
    logs  存放Nginx的运行日志,错误日志
    sbin   存放Nginx可执行程序的目录
    
    conf/nginx.conf参数说明:
    ####################
    #user  nobody;
    #工作进程数,根据cpu的核数优化。
    worker_processes  4;    
    #错误日志
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    error_log  logs/error.log  info;
    #进程id文件
    #pid        logs/nginx.pid;
    
    #nginx事件驱动
    events {
        worker_connections  60000;
    }
    
    #nginx web配置核心区域
    http {
        include       mime.types;
        default_type  application/octet-stream;
        
        #日志格式
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$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;
        #定义nginx虚拟主机的
        server {
            #nginx监听的端口,默认浏览器是80
            listen       80;
            #填写服务器的域名,如果你有域名,nginx会解析到当前这个虚拟主机
            #当访问pythonav.cn:80
            server_name  pythonav.cn;
    
            #charset koi8-r; 
        
            #access_log  logs/host.access.log  main;
            
            #location 就是nginx的路径资源匹配
            #就是当请求
            #pythonav.cn/
            #pythonav.cn/zoie.jpg
            #python.cn/flash/zoie.mp4
            #这个location / 这个语法是万能匹配,你所有的请求,都会进入这个location
            location / {
                #这个root用于定义网页根目录路径(可以换成其他绝对路径,相对路径是当前nginx)
                root   html;
                #定义网页的首页文件,名字必须叫做index.html
                index  index.html index.htm;
            }
            #通过这个参数,定义错误页面的文件,当状态码是404 400 401时,返回40x.html页面
            error_page  404 401 400 403             /40x.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ .php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ .php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }
    
    
    
    ####################
    1.nginx定义多虚拟主机配置如下:
    http{
        #虚拟主机1,运行吃鸡网站
        #虚拟主机的加载,自上而下的加载,如果是访问的ip地址,永远访问第一个
        server{
            listen 80;
            #当访问的域名是chiji.com就进入这个server标签
            server_name  chiji.com;
            location / {
                root   /opt/chiji;
                index  index.html index.htm;
            }
        }
        #虚拟主机2,运行韩剧网站
        server{
            listen 80;
            #当访问的域名是hanju.com就进入这个server标签
            server_name  hanju.com;
            location / {
                root   /opt/hanju;
                index  index.html index.htm;
            }
        }
    }
    
    2.配置两个虚拟主机的网站资源
        1.配置吃鸡网游的资料
        在/opt/chiji 目录下创建index.html
        2.配置韩剧的资料
        在/opt/hanju 目录下创建index.html
        3.配置两个本地解析的域名
        C:WindowsSystem32driversetchosts文件,写入如下配置:
        192.168.1.101 chiji.com 
        192.168.1.101 hanju.com
        4.在Windows下测试访问是否正常
        chiji.com 
        hanju.com
    
    3.定义nginx错误页面优化404页面定制
        修改nginx.conf 找到如下参数
        #通过这个参数,定义错误页面的文件,当状态码是404 400 401时,返回40x.html页面
        error_page  404 401 400 403             /40x.html;   
    
    4.nginx用户访问日志access.log
    
    ####################
    nginx反向代理
    
    代理:
    微商,代购,房屋中介,黄牛,
    
    用户向一个代理请求资源,而不需要关注这个资源的真实位置
    用户(浏览器) 请求网站资源 --》直接定位到django后台(所有的请求压力,都直接给了后台)
    django默认对并行很差,并且处理网页的静态资源效率很差。
    
    10万个并发请求--》后台应用
    静态请求 --> nginx(天然并发性很高,并且处理静态资源css,js,jpg),静态资源nginx直接从磁盘上返回。
    hanju.com/zoie.png
    nginx无法处理动态请求,直接将这个请求丢给后端应用django
    
    动态请求(对数据库进行交互,必须有编程语言的支撑)
    hanju.com/login/  --> django 后台,django这个编程语言框架,就可以对login请求处理
    
    ####################
    实现nginx反向代理的功能
    1.实验环境准备,2台机器
        用户:浏览器发送请求,得到请求
        192.168.0.101  nginx反向代理服务器(房屋中介代理)
        192.168.0.111  真实资源服务器(有房源的房东)
    
    2.分别在2台机器上,安装nginx    
    3.先配置真实资源服务器 192.168.0.111,打开这个机器的页面是吃鸡的网游页面
    4.配置反向代理服务器192.168.0.101
    http{
        #虚拟主机的加载,自上而下的加载,如果是访问的ip地址,永远访问第一个
        #这个nginx服务器,不再是用作虚拟主机了
        #而是直接转发别人的请求,是一个代理身份
        server{
            listen 80;
            #当访问的域名是chiji.com就进入这个server标签
            server_name  chiji.com;
            location / {
                #root   /opt/chiji;
                #index  index.html index.htm;
                #当请求是chiji.com的时候,这个nginx不做处理,直接转发请求给另一台机器
                proxy_pass  http://192.168.0.111;
            }
        }
    
    
    
    
    ####################其他
    1.nginx脚本命令
    nginx 直接输入是启动
    nginx -s stop 停止
    nginx -s reload 平滑重启,重新读取配置文件
    
    
    .nginx.conf.swp 这个文件是由于你在vim编辑文件的时候,异常退出,或者有其他人也想获取这个文件句柄,
    vim防止文件内容错乱,丢失,自动生成一个swp缓存文件,用于保护文件的。
    [root@localhost conf]# rm -rf .nginx.conf.swp   #删除这个隐藏文件就可以了
    
    
    ###
    top 命令可以查看cpu核数
    hostnamectl set-hostname master
    hostnamectl set-hostname slave
    nginx学习
    nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件。它是一个俄罗斯人lgor sysoev开发的,作者将源代码开源出来供全球使用。
    nginx比它大哥apache性能改进许多,nginx占用的系统资源更少,支持更高的并发连接,有更高的访问效率。
    nginx不但是一个优秀的web服务软件,还可以作为反向代理,负载均衡,以及缓存服务使用。
    安装更为简单,方便,灵活。
    nginx可以说是非常nb了

    面试回答nginx技巧

    支持高并发,能支持几万并发连接
    资源消耗少,在3万并发连接下开启10个nginx线程消耗的内存不到200M
    可以做http反向代理和负载均衡
    支持异步网络i/o事件模型epoll

    Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。

    安装环境准备

    一. gcc 安装
    安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
    
    yum install gcc-c++
    二. PCRE pcre-devel 安装
    PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
    
    yum install -y pcre pcre-devel
    三. zlib 安装
    zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
    
    yum install -y zlib zlib-devel
    四. OpenSSL 安装
    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
    nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
    yum install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y

    安装,启动nginx

    1.下载源码包
    wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
    2.解压缩源码
    tar -zxvf nginx-1.12.0.tar.gz
    3.配置,编译安装 开启nginx状态监测功能
    ./configure --prefix=/opt/nginx1-12/ --with-http_ssl_module --with-http_stub_status_module
    make && make install
    4.启动nginx,进入sbin目录,找到nginx启动命令
    cd sbin
    ./nginx #启动
    ./nginx -s stop #关闭
    ./nginx -s reload #重新加载

    安装完成后检测服务

    netstat -tunlp |grep 80
    curl -I 127.0.0.1
    #如果访问不了,检查selinux,iptables

    部署一个web站点

    nginx默认站点是Nginx目录下的html文件夹,这里可以从nginx.conf中查到

     location /{
                root   html;  #这里是默认的站点html文件夹,也就是 /opt/nginx1-12/html/文件夹下的内容
                index  index.html index.htm; #站点首页文件名是index.html
            }

    如果要部署网站业务数据,只需要把开发好的程序全放到html目录下即可

    [root@oldboy_python /tmp 11:34:52]#ls /opt/nginx1-12/html/
    index.html  jssts.jpeg  lhy.mp4  man.jpg  wget-log

    因此只需要通过域名/资源,即可访问

    http://www.pyyuc.cn/man.jpg

    Nginx的目录结构

    [root@oldboy_python /opt/nginx1-12 11:44:02]#ls
    client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  static  uwsgi_temp
    • conf 存放nginx所有配置文件的目录,主要nginx.conf
    • html 存放nginx默认站点的目录,如index.html、error.html等
    • logs 存放nginx默认日志的目录,如error.log access.log
    • sbin 存放nginx主命令的目录,sbin/nginx

    Nginx主配置文件解析

    Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。

    ######Nginx配置文件nginx.conf中文详解#####
    
    #定义Nginx运行的用户和用户组
    user www www;
    
    #nginx进程数,建议设置为等于CPU总核心数。
    worker_processes 8;
     
    #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
    error_log /usr/local/nginx/logs/error.log info;
    
    #进程pid文件
    pid /usr/local/nginx/logs/nginx.pid;
    
    #指定进程可以打开的最大描述符:数目
    #工作模式与连接数上限
    #这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。
    #现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。
    #这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。
    worker_rlimit_nofile 65535;
    
    
    events
    {
        #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型
        #是Linux 2.6以上版本内核中的高性能网络I/O模型,linux建议epoll,如果跑在FreeBSD上面,就用kqueue模型。
        #补充说明:
        #与apache相类,nginx针对不同的操作系统,有不同的事件模型
        #A)标准事件模型
        #Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
        #B)高效事件模型
        #Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
        #Epoll:使用于Linux内核2.6版本及以后的系统。
        #/dev/poll:使用于Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
        #Eventport:使用于Solaris 10。 为了防止出现内核崩溃的问题, 有必要安装安全补丁。
        use epoll;
    
        #单个进程最大连接数(最大连接数=连接数*进程数)
        #根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。
        worker_connections 65535;
    
        #keepalive超时时间。
        keepalive_timeout 60;
    
        #客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。
        #分页大小可以用命令getconf PAGESIZE 取得。
        #[root@web001 ~]# getconf PAGESIZE
        #4096
        #但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。
        client_header_buffer_size 4k;
    
        #这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
        open_file_cache max=65535 inactive=60s;
    
        #这个是指多长时间检查一次缓存的有效信息。
        #语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.
        open_file_cache_valid 80s;
    
        #open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。
        #语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location  这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如果使用更大的值,文件描述符在cache中总是打开状态.
        open_file_cache_min_uses 1;
        
        #语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.
        open_file_cache_errors on;
    }
     
     
     
    #设定http服务器,利用它的反向代理功能提供负载均衡支持
    http
    {
        #文件扩展名与文件类型映射表
        include mime.types;
    
        #默认文件类型
        default_type application/octet-stream;
    
        #默认编码
        #charset utf-8;
    
        #服务器名字的hash表大小
        #保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。因此,如果Nginx给出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.
        server_names_hash_bucket_size 128;
    
        #客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。
        client_header_buffer_size 32k;
    
        #客户请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,如果header过大,它会使用large_client_header_buffers来读取。
        large_client_header_buffers 4 64k;
    
        #设定通过nginx上传文件的大小
        client_max_body_size 8m;
    
        #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
        #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
        sendfile on;
    
        #开启目录列表访问,合适下载服务器,默认关闭。
        autoindex on;
    
        #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
        tcp_nopush on;
         
        tcp_nodelay on;
    
        #长连接超时时间,单位是秒
        keepalive_timeout 120;
    
        #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 128k;
    
        #gzip模块设置
        gzip on; #开启gzip压缩输出
        gzip_min_length 1k;    #最小压缩文件大小
        gzip_buffers 4 16k;    #压缩缓冲区
        gzip_http_version 1.0;    #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
        gzip_comp_level 2;    #压缩等级
        gzip_types text/plain application/x-javascript text/css application/xml;    #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
        gzip_vary on;
    
        #开启限制IP连接数的时候需要使用
        #limit_zone crawler $binary_remote_addr 10m;
    
    
    
        #负载均衡配置
        upstream jh.w3cschool.cn {
         
            #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
            server 192.168.80.121:80 weight=3;
            server 192.168.80.122:80 weight=2;
            server 192.168.80.123:80 weight=3;
    
            #nginx的upstream目前支持4种方式的分配
            #1、轮询(默认)
            #每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
            #2、weight
            #指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
            #例如:
            #upstream bakend {
            #    server 192.168.0.14 weight=10;
            #    server 192.168.0.15 weight=10;
            #}
            #2、ip_hash
            #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
            #例如:
            #upstream bakend {
            #    ip_hash;
            #    server 192.168.0.14:88;
            #    server 192.168.0.15:80;
            #}
            #3、fair(第三方)
            #按后端服务器的响应时间来分配请求,响应时间短的优先分配。
            #upstream backend {
            #    server server1;
            #    server server2;
            #    fair;
            #}
            #4、url_hash(第三方)
            #按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
            #例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
            #upstream backend {
            #    server squid1:3128;
            #    server squid2:3128;
            #    hash $request_uri;
            #    hash_method crc32;
            #}
    
            #tips:
            #upstream bakend{#定义负载均衡设备的Ip及设备状态}{
            #    ip_hash;
            #    server 127.0.0.1:9090 down;
            #    server 127.0.0.1:8080 weight=2;
            #    server 127.0.0.1:6060;
            #    server 127.0.0.1:7070 backup;
            #}
            #在需要使用负载均衡的server中增加 proxy_pass http://bakend/;
    
            #每个设备的状态设置为:
            #1.down表示单前的server暂时不参与负载
            #2.weight为weight越大,负载的权重就越大。
            #3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
            #4.fail_timeout:max_fails次失败后,暂停的时间。
            #5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
    
            #nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
            #client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug
            #client_body_temp_path设置记录文件的目录 可以设置最多3层目录
            #location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
        }
         
         
         
        #虚拟主机的配置
        server
        {
            #监听端口
            listen 80;
    
            #域名可以有多个,用空格隔开
            server_name www.w3cschool.cn w3cschool.cn;
            index index.html index.htm index.php;
            root /data/www/w3cschool;
    
            #对******进行负载均衡
            location ~ .*.(php|php5)?$
            {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
            }
             
            #图片缓存时间设置
            location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
            {
                expires 10d;
            }
             
            #JS和CSS缓存时间设置
            location ~ .*.(js|css)?$
            {
                expires 1h;
            }
             
            #日志格式设定
            #$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
            #$remote_user:用来记录客户端用户名称;
            #$time_local: 用来记录访问时间与时区;
            #$request: 用来记录请求的url与http协议;
            #$status: 用来记录请求状态;成功是200,
            #$body_bytes_sent :记录发送给客户端文件主体内容大小;
            #$http_referer:用来记录从那个页面链接访问过来的;
            #$http_user_agent:记录客户浏览器的相关信息;
            #通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
            log_format access '$remote_addr - $remote_user [$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
            '"$http_user_agent" $http_x_forwarded_for';
             
            #定义本虚拟主机的访问日志
            access_log  /usr/local/nginx/logs/host.access.log  main;
            access_log  /usr/local/nginx/logs/host.access.404.log  log404;
             
            #对 "/" 启用反向代理
            location / {
                proxy_pass http://127.0.0.1:88;
                proxy_redirect off;
                proxy_set_header X-Real-IP $remote_addr;
                 
                #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                 
                #以下是一些反向代理的配置,可选。
                proxy_set_header Host $host;
    
                #允许客户端请求的最大单文件字节数
                client_max_body_size 10m;
    
                #缓冲区代理缓冲用户端请求的最大字节数,
                #如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是IE浏览器,来提交任意小于256k的图片,都很正常。如果注释该指令,使用默认的client_body_buffer_size设置,也就是操作系统页面大小的两倍,8k或者16k,问题就出现了。
                #无论使用firefox4.0还是IE8.0,提交一个比较大,200k左右的图片,都返回500 Internal Server Error错误
                client_body_buffer_size 128k;
    
                #表示使nginx阻止HTTP应答代码为400或者更高的应答。
                proxy_intercept_errors on;
    
                #后端服务器连接的超时时间_发起握手等候响应超时时间
                #nginx跟后端服务器连接超时时间(代理连接超时)
                proxy_connect_timeout 90;
    
                #后端服务器数据回传时间(代理发送超时)
                #后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
                proxy_send_timeout 90;
    
                #连接成功后,后端服务器响应时间(代理接收超时)
                #连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
                proxy_read_timeout 90;
    
                #设置代理服务器(nginx)保存用户头信息的缓冲区大小
                #设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头,默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小,不过可以将其设置为更小
                proxy_buffer_size 4k;
    
                #proxy_buffers缓冲区,网页平均在32k以下的设置
                #设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况也为分页大小,根据操作系统的不同可能是4k或者8k
                proxy_buffers 4 32k;
    
                #高负荷下缓冲大小(proxy_buffers*2)
                proxy_busy_buffers_size 64k;
    
                #设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长
                #设定缓存文件夹大小,大于这个值,将从upstream服务器传
                proxy_temp_file_write_size 64k;
            }
             
             
            #设定查看Nginx状态的地址
            location /NginxStatus {
                stub_status on;
                access_log on;
                auth_basic "NginxStatus";
                auth_basic_user_file confpasswd;
                #htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
            }
             
            #本地动静分离反向代理配置
            #所有jsp的页面均交由tomcat或resin处理
            location ~ .(jsp|jspx|do)?$ {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://127.0.0.1:8080;
            }
             
            #所有静态文件由nginx直接读取不经过tomcat或resin
            location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|
            pdf|xls|mp3|wma)$
            {
                expires 15d; 
            }
             
            location ~ .*.(js|css)?$
            {
                expires 1h;
            }
        }
    }
    ######Nginx配置文件nginx.conf中文详解#####
    nginx.conf详解 
    CoreModule核心模块
    
    user www;                       #Nginx进程所使用的用户
    worker_processes 1;             #Nginx运行的work进程数量(建议与CPU数量一致或auto)
    error_log /log/nginx/error.log  #Nginx错误日志存放路径
    pid /var/run/nginx.pid          #Nginx服务运行后产生的pid进程号
    events事件模块
    
    events {            
        worker_connections  //每个worker进程支持的最大连接数
        use epool;          //事件驱动模型, epoll默认
    }
    http内核模块
    
    //公共的配置定义在http{}
    http {  //http层开始
    ...    
        //使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
        'server' {
            listen       80;        //监听端口, 默认80
            server_name  localhost; //提供服务的域名或主机名
            access_log host.access.log  //访问日志
            //控制网站访问路径
            'location' / {
                root   /usr/share/nginx/html;   //存放网站代码路径
                index  index.html index.htm;    //服务器返回的默认页面文件
            }
            //指定错误代码, 统一定义错误页面, 错误代码重定向到新的Locaiton
            error_page   500 502 503 504  /50x.html;
        }
        ...
        //第二个虚拟主机配置
        'server' {
        ...
        }
        
        include /etc/nginx/conf.d/*.conf;  //包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件
    
    }   //http层结束

    Nginx虚拟主机

    如果每台linux服务器只运行了一个小网站,那么人气低,流量小的草根站长需要承担高额的服务器租赁费,也造成了硬件资源浪费。

    虚拟主机就是将一台服务器分割成多个“虚拟服务器”,每个站点使用各自的硬盘空间,由于省资源,省钱,众多网站都使用虚拟主机来部署网站。

    虚拟主机的概念就是在web服务里的一个独立的网站站点,这个站点对应独立的域名(IP),具有独立的程序和资源目录,可以独立的对外提供服务。
    这个独立的站点配置是在nginx.conf中使用server{}代码块标签来表示一个虚拟主机。
    Nginx支持多个server{}标签,即支持多个虚拟主机站点。

    虚拟主机类型

    基于域名的虚拟主机
    通过不同的域名区分不同的虚拟主机,是企业应用最广的虚拟主机。

    基于端口的虚拟主机
    通过不同的端口来区分不同的虚拟主机,一般用作企业内部网站,不对外直接提供服务的后台,例如www.pythonav.cn:9000

    基于IP的虚拟主机
    通过不同的IP区分不同的虚拟主机,此类比较少见,一般业务需要多IP的常见都会在负载均衡中绑定VIP

    Nginx状态信息(status)配置

    Nginx状态信息(status)配置及信息详解
        nginx与php-fpm一样内建了一个状态页,对于想了解nginx的状态以及监控nginx非常有帮助。为了后续的zabbix监控,我们需要先了解一下nginx的状态页。
    
    Nginx状态信息(status)介绍
        Nginx软件在编译时又一个with-http_stub_status_module模块,这个模块功能是记录Nginx的基本访问状态信息,让使用者了解Nginx的工作状态。
    要想使用状态模块,在编译时必须增加--with-http_stub_status_module参数。

    监测你的nginx是否安装了status模块

    [root@master conf]# /opt/nginx/sbin/nginx -V
    nginx version: nginx/1.12.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
    configure arguments: --prefix=/opt/nginx/ --with-http_stub_status_module

    启动status状态功能,修改配置文件

    #在访问ip/status的时候,进入状态功能        
    location /status {
            #开启nginx状态功能 stub_status on; }

    平滑重启nginx

    ./sbin/nginx -s reload

    访问status页面

    http://192.168.119.10/status

    通过ab压测命令检测

    yum -y install httpd-tools

    -n requests #执行的请求数,即一共发起多少请求。

    -c concurrency #请求并发数。

    -k #启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。

    ab -kc 1000 -n 100000 http://192.168.119.10/

    status页面解析

    基于域名的多虚拟主机实战

    nginx可以自动识别用户请求的域名,根据不同的域名请求服务器传输不同的内容,只需要保证服务器上有一个可用的ip地址,配置好dns解析服务。

    /etc/hosts是linux系统中本地dns解析的配置文件,同样可以达到域名访问效果

    修改nginx.conf

    [root@oldboy_python ~ 14:33:16]#egrep -v '#|^$' /opt/nginx1-12/conf/nginx.conf
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        access_log  logs/access.log  main;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  www.pyyuc.cn;
            location /{
                root   html/pyyuc;
                index  index.html index.htm;
            }
        }
    }

    上述代码配置了一个www.pyyuc.cn域名的站点,虚拟主机的部分就是server{}里的内容

    创建pyyuc.cn的站点目录和文件

    [root@oldboy_python /opt/nginx1-12/html 14:36:08]#mkdir pyyuc

    [root@oldboy_python /opt/nginx1-12/html 14:36:18]#echo "<meta charset=utf8>我是pyyuc站点" > pyyuc/index.html
    [root@oldboy_python /opt/nginx1-12/html 14:37:21]#cat pyyuc/index.html
    <meta charset=utf8>我是pyyuc站点

    上述作用创建了一个html/pyyuc站点目录,对应于虚拟主机配置文件里的root根目录的设置html/pyyuc

    然后生成一个首页文件index.html,内容是“我是pyyuc站点”

    检查nginx语法重新加载nginx

    [root@oldboy_python /opt/nginx1-12/html 14:37:28]#../sbin/nginx -t
    nginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is ok
    nginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful

    #平滑重启nginx

    [root@oldboy_python /opt/nginx1-12/html 14:39:18]#../sbin/nginx -s reload

    检查nginx端口,进程,访问pyyuc虚拟站点

    [root@oldboy_python /opt/nginx1-12/html 14:40:02]#netstat -tunlp|grep nginx
    [root@oldboy_python /opt/nginx1-12/html 14:40:29]#ps -ef|grep nginx

    #我这里是有dns解析,没有的话则需要/etc/hosts解析
    #成功配置了pyyuc虚拟主机站点
    [root@oldboy_python /opt/nginx1-12/html 14:41:37]#curl www.pyyuc.cn
    <meta charset=utf8>我是pyyuc站点

    配置多个域名的虚拟主机

    其实就是新增一个server{}虚拟主机

    egrep -v '#|^$' /opt/nginx1-12/conf/nginx.conf
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        access_log  logs/access.log  main;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  www.pyyuc.cn;
            location /{
                root   html/pyyuc;
                index  index.html index.htm;
            }
    }
        server {
            listen       80;
            server_name  www.pythonav.cn;
            location /{
                root   html/pythonav;
                index  index.html index.htm;
            }
    }
        }

    创建pythonav虚拟主机站点的目录和文件

    [root@oldboy_python /opt/nginx1-12 14:47:21]#mkdir -p /opt/nginx1-12/html/pythonav
    [root@oldboy_python /opt/nginx1-12 14:49:33]#echo "<meta charset=utf8>我是pythonav,未成年禁止入内"> /opt/nginx1-12/html/pythonav/index.html
    [root@oldboy_python /opt/nginx1-12 14:50:44]#./sbin/nginx -t
    nginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is ok
    nginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful
    [root@oldboy_python /opt/nginx1-12 14:51:32]#./sbin/nginx -s reload

    大功告成,基于域名的虚拟主机实战搞定

    [root@oldboy_python /opt/nginx1-12 14:52:12]#curl www.pythonav.cn
    <meta charset=utf8>我是pythonav,未成年禁止入内
    [root@oldboy_python /opt/nginx1-12 14:52:40]#curl www.pyyuc.cn
    <meta charset=utf8>我是pyyuc站点

    nginx访问日志(access_log)

    日志功能对每个用户访问网站的日志信息记录到指定的日志文件里,开发运维人员可以分析用户的浏览器行为,此功能由ngx_http_log_module模块负责,官网地址是:

    http://nginx.org/en/docs/http/ngx_http_log_module.html

    控制日志的参数

    log_format    记录日志的格式,可定义多种格式
    accsss_log    指定日志文件的路径以及格式

      log_format main '$remote_addr - $remote_user [$time_local] "$request" '
      '$status $body_bytes_sent "$http_referer" '
      '"$http_user_agent" "$http_x_forwarded_for"';

    对应参数解析

    $remote_addr    记录客户端ip
    $remote_user    远程用户,没有就是 “-”
    $time_local    对应
    [14/Aug/2018:18:46:52 +0800]
    $request     对应请求信息"GET /favicon.ico HTTP/1.1"
    $status     状态码
    $body_bytes_sent  571字节 请求体的大小
    $http_referer  对应“-”  由于是直接输入浏览器就是 -
    $http_user_agent  客户端身份信息
    $http_x_forwarded_for  记录客户端的来源真实ip 97.64.34.118

    日志效果如下

    66.102.6.6 - - [14/Aug/2018:18:46:52 +0800] "GET /favicon.ico HTTP/1.1" 404 571 "-"

    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36 Google Favicon" "97.64.34.118"

    nginx.conf默认配置

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  logs/access.log  main;

    日志格式配置定义

    log_format是日志关键字参数,不能变
    main是日志格式指定的标签,记录日志时通过main标签选择指定的格式。 

    nginx限制网站来源IP访问

    如果哪天发现你的nginx很慢,或者检查access.log时候,有一个some body疯狂请求你的nginx server,那么可以禁止这个IP访问

    限制ip或ip段访问
    禁止访问/av/底下的资源
    location /av { deny 122.71.240.254; #alias /opt/nginx1-12/html/av; allow 10.1.1.0/16; }

    Nginx错误页面优化

    在网站运行过程中,可能因为页面不存在等原因,导致网站无法正常响应请求,此时web服务会返回系统的错误码,但是默认的错误页面很不友好。

    因此我们可以将404,403等页面的错误信息重定向到网站首页或者其他指定的页面,提升用户访问体验。

    server {
            listen       80;
            server_name  www.pythonav.cn;
            root html/pythonav;
            location /{
                index  index.html index.htm;
            }
          #在pythonav路径下的40x.html错误页面 error_page
    400 403 404 405 /40x.html; }

    40x.html

    <img style='100%;height:100%;' src=https://pic1.zhimg.com/80/v2-77a9281a2bebc7a2ea5e02577af266a8_hd.png>

    此时访问www.pythonav.cn/asdasd错误页面已经优化了

     

    Nginx代理

    正向代理

    正向代理,也就是传说中的代理,他的工作原理就像一个跳板(VPN),简单的说:

    我是一个用户,我访问不了某网站,但是我能访问一个代理服务器,这个代理服务器呢,他能访问那个我不能访问的网站,于是我先连上代理服务器,告诉他我需要那个无法访问网站的内容,代理服务器去取回来,然后返回给我。

    反向代理

    对于客户端而言,代理服务器就像是原始服务器。

    nginx实现负载均衡的组件

    ngx_http_proxy_module    proxy代理模块,用于把请求抛给服务器节点或者upstream服务器池

    实现一个简单的反向代理

    机器准备,两台服务器

    master  192.168.11.63  主负载
    slave 192.168.11.64  web1

    主负载均衡节点的配置文件

    worker_processes  1;
    error_log  logs/error.log;
    pid        logs/nginx.pid;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        access_log  logs/access.log  main;
        sendfile        on;
        keepalive_timeout  65;
            upstream slave_pools{
        server 192.168.11.64:80 weight=1;
    }
        server {
            listen       80;
            server_name  localhost;
            location / {
            proxy_pass  http://slave_pools;
                root   html;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    nginx.conf

    检查语法并启动nginx

    [root@master 192.168.11.63 /opt/nginx1-12]$/opt/nginx1-12/sbin/nginx -t
    nginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is ok
    nginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful
    #启动nginx
    [root@master 192.168.11.63 /opt/nginx1-12]$/opt/nginx1-12/sbin/nginx
    #检查端口
    [root@master 192.168.11.63 /opt/nginx1-12]$netstat -tunlp|grep nginx
    tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8921/nginx: master

    此时访问master的服务器192.168.11.63:80地址,已经会将请求转发给slave的80端口

    除了页面效果的展示以外,还可以通过log(access.log)查看代理效果

    master端日志

    slave端日志

    nginx语法之location详解

    Location语法优先级排列

    匹配符 匹配规则 优先级
    =    精确匹配    1
    ^~    以某个字符串开头    2
    ~    区分大小写的正则匹配    3
    ~*    不区分大小写的正则匹配    4
    !~    区分大小写不匹配的正则    5
    !~*    不区分大小写不匹配的正则    6
    /    通用匹配,任何请求都会匹配到    7

    nginx.conf配置文件实例

    server {
        listen 80;
        server_name pythonav.cn;
    
        #优先级1,精确匹配,根路径
        location =/ {
            return 400;
        }
    
        #优先级2,以某个字符串开头,以av开头的,优先匹配这里,区分大小写
        location ^~ /av {
           root /data/av/;
        }
    
        #优先级3,区分大小写的正则匹配,匹配/media*****路径
        location ~ /media {
              alias /data/static/;
        }
    
        #优先级4 ,不区分大小写的正则匹配,所有的****.jpg|gif|png 都走这里
        location ~* .*.(jpg|gif|png|js|css)$ {
           root  /data/av/;
            }
    
        #优先7,通用匹配
        location / {
            return 403;
        }
    }

    nginx语法之root和alias区别实战

    nginx指定文件路径有root和alias两种方法
    区别在方法和作用域:
    
    方法:
    
    root
    语法  root  路径;
    默认值 root   html;
    配置块  http{}   server {}   location{}
    
    alias
    语法: alias  路径
    配置块  location{}
    
    
    root和alias区别在nginx如何解释location后面的url,这会使得两者分别以不同的方式讲请求映射到服务器文件上
    
    root参数是root路径+location位置
    
    root实例:
    
        location ^~ /av {
            root /data/av;   注意这里可有可无结尾的   /
        }
    请求url是pythonav.cn/av/index.html时
    web服务器会返回服务器上的/data/av/av/index.html
    
    root实例2:
    location ~* .*.(jpg|gif|png|js|css)$ {
           root  /data/av/;
    }
    
    请求url是pythonav.cn/girl.gif时
    web服务器会返回服务器上的/data/static/girl.gif
    
    
    
    
    alias实例:
    alias参数是使用alias路径替换location路径
    alias是一个目录的别名
    注意alias必须有 "/"  结束!
    alias只能位于location块中
    
    
    请求url是pythonav.cn/av/index.html时
    web服务器会返回服务器上的/data/static/index.html
    
    location ^~ /av {
        alias /data/static/;
    }

    Keepalived高可用软件

    什么是keepalived

    Keepalived是一个用C语言编写的路由软件。该项目的主要目标是为Linux系统和基于Linux的基础架构提供简单而强大的负载均衡和高可用性设施。 
    还可以作为其他服务(nginx,mysql)的高可用软件
    keepalived主要通过vrrp协议实现高可用功能。vrrp叫(virtual router redundancy protocol)虚拟路由器冗余协议,
    目的为了解决单点故障问题,他可以保证个别节点宕机时。整个网络可以不间断的运行。

    高可用故障切换原理

    在keepalived工作时,主master节点会不断的向备节点发送心跳消息,告诉备节点自己还活着,
    当master节点故障时,就无法发送心跳消息,备节点就无法检测到来自master的心跳了,于是调用自身的接管程序,接管master节点的ip资源以及服务,
    当master主节点恢复时,备backup节点又会释放接管的ip资源和服务,回复到原本的备节点角色。

    1.硬件环境准备

    实验环境应该最好是4台虚拟机,环境有限因此用2台机器
    master
    slave

    2.centos系统和nginx代理环境

  • 相关阅读:
    hdu1010
    hiho1041
    cg基础
    python下载网页转化成pdf
    QT笔记 -- (6) opengl
    QT笔记 -- (5) 实现QWidget的paintEvent函数,在widget上画背景图形
    程序媛成长之路--厚积薄发
    mysql集群搭建教程-基础篇
    JAVA遇见HTML——tomcat篇
    【DRP】——JSP+servlet
  • 原文地址:https://www.cnblogs.com/bubu99/p/11582172.html
Copyright © 2020-2023  润新知