• nginx安装与应用


    一.nginx的安装与启动:

    1.安装依赖库。nginx的一些模块需要依赖其他第三方库,通常有pcre库(perl compatible regular expression,perl兼容正则表达式,支持rewrite模块)、zlib库(支持gzip模块)和openssl库(支持ssl模块)

    yum install -y pcre pcre-devel zlib zlib-devel open openssl-devel

    2.安装patch

    如果想主动检测后端服务器状态的话,需要用yaoweibin开发的nginx_upstream_check_module。从 https://github.com/yaoweibin/nginx_upstream_check_module 下载zip包。注意,是下载

    2.生成Makefile文件。将下载好的nginx-xxx.tar.gz上传到centos7中,解压,进到nginx-xxx目录中,运行./configure,结束后会打印 nginx path prefix 路径等信息

    在执行./configure命令时可以添加一些常用的选项,如

    --prefix=<path> 指定nginx安装目录,默认为/usr/local/nginx目录

    --with-http_ssl_module 声明启用HTTP的ssl模块,这样nginx服务器就可以支持HTTPS请求了

    --with-http_stub_status_module 声明启用Server Status页面,默认不启用。启用后,只需在location块中配置一个uri(如下文中的/nginx_status),访问此uri页面就可以看到一些服务器状态信息,如下

    Active connections: 1

    server accepts handled requests

                  10          10          120

    Reading: 0 Writing: 1 Waiting: 0 

    --add-module=<path> 指定第三方模块的路径,多个第三方模块的话就用多个--add-module指定各自的路径。如--add-module=/home/koushengrui/app/lua-nginx-module-0.10.13 --add-module=/home/koushengrui/app/ngx_devel_kit-0.3.1rc1 --add-module=/home/koushengrui/app/nginx_upstream_check_module-master,指定编译时添加nginx-lua-module、ngx_devel_kit和nginx_upstream_check_module(下文中的 check interval=3000 rise=2 fall=5 timeout=1000 type=tcp; 就是依赖此模块而配置的)。

    3.编译与安装。# make && make install。(因为涉及往/usr/local目录中写入,所以必须是root用户或者具有root权限的用户)

    4.启动与关闭。切换到/usr/local/nginx目录,此目录下有conf目录、html目录、logs目录、sbin目录,其中conf目录有默认的配置文件,logs目录有日志文件(包括访问日志文件和错误日志文件),sbin目录有可执行命令

    用默认配置文件启动nginx:nginx

    指定配置文件启动nginx:nginx -c path/nginx.conf,其中path是绝对路径

    nginx启动后,就会在logs目录中(路径可以在nginx.conf文件中配置,默认是logs目录)生成一个nginx.pid的文件,记录master process的pid。

    启动后,就可以用nginx -s signal来控制nginx了,其中signal的值可以是stop、quit、reload:

    stop,fast shutdown  快速关闭

    quit,graceful shutdown  平滑关闭。关闭nginx后,logs目录中的nginx.pid文件会消失,下次启动后再次生成

    reload,reloading the configuration file  根据之前启动时的配置文件平滑重启

    如果想在重启的时候另外指定配置文件呢???

    二.配置文件的location块解释

    location的语法结构为:location [=|~|~*|^~] uri

    其中uri是待匹配的字符串,分为两种,标准uri和正则uri。[ ]中的是可选项。

    nginx首先在server的多个location块中搜索是否有标准uri和请求字符串匹配,如果有多个可以匹配,就记录匹配度最高的一个。然后,再按顺序搜索是否有正则uri和请求字符串匹配,如果有,则结束搜索,用此location块处理此请求。如果没有,则使用刚才记录的匹配度最高的location块处理请求。如果刚才标准uri匹配时所有的location块都不匹配,则此时会访问html目录下的index.html文件。

    location = uri {

        xxx

    }                     =后面跟标准uri,要求请求字符串与uri严格匹配。如果匹配成功,就用当前location块处理请求,而不会继续搜索了。

    location uri {

        xxx            

    }                    普通匹配,标准uri

    location ~ uri {

       xxx

    }                     ~后面跟正则uri,匹配时区分大小写

    location ~* uri {

       xxx

    }                      ~*后面跟正则uri,匹配时不区分大小写

    location ^~ uri {

       xxx

    }                      ^~后面跟标准uri,匹配以uri开头的请求。如果匹配的话,直接用此location块处理请求,不再去搜索正则uri

    示例:

    修改默认的nginx.conf配置文件server块如下:

    server{
            listen        80;
            server_name   aliecs1;
    
            location /index {
                    root   first;
                    index  index.html;
            }
            #严格匹配
            location = /index {
                    root  /second;
                    index index.html;
            }
    
            location ^~ /index1 {
                    root  third;
                    index index.html;
            }
    }

    root可以指定相对路径,也可以是绝对路径。相对路径是相对nginx的根路径(/usr/local/nginx/),写法是前面不加/。绝对路径以/开始。请求地址被某个location匹配并处理后,最终访问路径是root指定的路径+请求字符串去掉ip和端口的剩余部分。

    如果请求地址是192.168.153.128/index.html,则会用location /index {}处理,最终访问/usr/local/nginx/first/index.html。

    如果请求地址是192.168.153.128/index1.html,则会用location ^~ /index1 {}处理,最终访问/usr/local/nginx/third/index1.html。

    如果请求地址是192.168.153.128/index2.html,还是会用location /index {}处理,最终访问/usr/local/nginx/first/index2.html。

    如果请求地址是192.168.153.128/index,则会用location = /index {}处理,最终访问/second/index。

    三.rewrite功能,字面意思是重写,实际上就是重定向redirect

    主要有if指令、break指令、return指令、rewrite指令、set指令:

    if指令:可以在server块、location块中配置,使用if做条件判断时需要注意两点:if与后面括号一定要有空格;判断等于是用=,而不再是==;表达式中的字符串不用加引号。示例:

     server {
            listen       80;
            server_name  192.168.153.136;
            location / {
                    if ($http_user_agent ~ Trident) {
                            root /home/koushengrui/html;
                    }
            }
    }

    以上配置表示如果发起请求的浏览器是IE或Edge浏览器的话,则首页会是/home/koushengrui/html下的index.html文件,否则是/usr/local/nginx/html下的index.html文件。

    注意,没有else指令,没有else指令,在if块中不能使用index指令。

    return指令:用于直接向客户端返回响应状态码及重定向地址。return指令可以在server块、location块、if块中使用,用法为:return code url。配置示例:

    server {
        listen       80;
        server_name  192.168.153.136;
    
        location / {
            return 301 http://192.168.153.136:10000/image/example.png;
            root /home/koushengrui/html;
            index welcome.htm;
        }
    }

    如果请求地址是http://192.168.153.136/*** 的话,则此请求会被重定向至http://192.168.153.136:10000/image/example.png。

    301是永久移动,302是临时移动。

    rewrite指令:通过正则表达式的使用来改变URI。这里需要大量的正则表达式的知识,现在了解的还不透彻。包括域名跳转、防盗链的功能都是用rewrite指令来做的。

    四.反向代理与负载均衡

    什么是反向代理?

    负载均衡

    内置策略有3种,轮询、加权轮询、IP hash:

    1.轮询,顾名思义,就是服务器将每个前端请求按顺序(时间顺序或者排列次序)逐一分配到不同的后端节点上,对于出现问题的后端节点自动去除;

    2.加权轮询,在基本的轮询策略基础上考虑各后端节点接受请求的权重,指定各后端节点被轮询到的几率,主要用于后端节点性能不均的情况下;

    3.IP hash,是将前端的访问IP进行hash操作,然后根据hash结果将请求分配给不同的后端节点。这样会使得每个前端访问IP会固定访问一个后端节点,好处是前端用户的session只在一个后端节点上,不必考虑一个session存在多台节点上而出现的session共享问题。

    扩展策略有2种,url hash、fair:

    4.url hash,将前端请求的url地址进行hash操作,根据hash结果将请求分配到不同的后端节点上;

    5.fair,将前端请求转发到负载最小的后端节点上。Nginx通过后端节点对请求的响应时间来判断负载情况,响应时间最短的节点负载就相对较轻,Nginx就会将前端请求转发到此后端节点上。

    五.gzib模块

    gzib on;#默认情况下是off,要想启动gzip功能,得设置为on

    gzip_min_length 1024;#当文件的长度大于1024字节时才压缩,否则不压缩,避免越压越大

    gzip_buffers 32 4k;#或者gzip_buffers 16 8k;设置gzip压缩文件使用缓存空间的大小,第一个数是nginx服务器向系统申请缓存空间的个数,第二个数是每个缓存空间的大小

    gzip_comp_level 6;#设置gzip的压缩程度,级别从1到9,1表示压缩程度最低,效率最高,9表示压缩程度最高,效率最低

    gzip_types text/plain application/javascript text/css text/xml;#根据相应文件的MIME类型选择性地开启gzip压缩功能,text/plain对应txt文件,application/javascript(或者text/javascript)对应js文件,text/css对应css文件,text/xml对应xml文件。这其中没有写html文件对应的MIME类型,是因为只要开启了gzip功能,html文件、htm文件是默认压缩的,写的话会报错。通常来说,只建议html/htm、css、js、xml文件进行压缩,而图片、音视频文件不建议压缩,因为压缩效果很小。

    六.expires缓存优化

    一般对图片进行expires配置

    location ~* ^.+.(png|jpg|jpeg|gif|ico|bmp|psd)$ {yyyy-MM-dd HH:mm:ss
           expires 1d;#y表示年,M表示月,d表示天,h表示小时,m表示分钟,s表示秒
    }

    七.限速

    location /download {
           if ($http_user_agent ~ Chrome){
                    limit_rate_after 100M;
           }
           limit_rate         200k;
           root               /home/koushengrui;     
    }

    limit_rate_after表示多少大小之后才开始限速,上例从100M开始限速,也就是说一个文件下载时前100M是不限速的,100M之后才限速

    limit_rate表示限速多少,上例中限速200k/s

    八.跨域

    需添加如下配置:(参考链接 https://enable-cors.org/server_nginx.html,实测有效)

    #
    # Wide-open CORS config for nginx
    #
    location / {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
            add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
    }

    zeb系统生产上某一台nginx服务器(有很多台)配置如下:

    user wls81 wls;
    worker_processes  5;
    
    error_log  /wls/apache/applogs/error.log;
    pid        /wls/apache/applogs/nginx.pid;
    
    events {
        use epoll;
        worker_connections  200000;
    }
    
    http {
    #   include       mime.types;
        default_type  application/octet-stream;
        server_tokens off; # 不返回nginx版本号
    
        sendfile        on;
        keepalive_timeout  65;
    
        log_format access '$remote_addr - $remote_user [$time_local] "$request"' '$status $body_bytes_sent "$http_referer"' '"$http_user_agent" $http_x_forwarded_for';
    
        upstream ng_zeb {
            ip_hash;
            server 192.168.56.100:8080;
            server 192.168.56.101:8080;
    server 192.168.56.102:8080; keepalive 32; check interval=3000 rise=2 fall=5 timeout=1000 type=tcp; } server { listen 0.0.0.0:31023; listen 0.0.0.0:31024 ssl; ssl_certificate /wls/apache/ssl/xxx.crt; ssl_certificate_key /wls/apache/ssl/xxx.key; access_log /wls/apache/applogs/host.access.log access; rewrite ^/f5monweb(.*) /monitor_ng/$1;

    location / { proxy_set_header Host $http_host; proxy_set_header Origin $http_origin; proxy_pass http://ng_zeb; index index.html; } location /monitor_ng { root /wls/apache; index f5check.htm f5check.jsp; } location /nginx_status { stub_status on; access_log off; allow 192.168.56.1; deny all; } } } 
  • 相关阅读:
    Day13
    Day12
    Day11
    Day10
    Day9
    Day8
    Day7
    mac上使用gitlab拉项目报错Permissions 0644 for ...
    vue-cli3使用svg
    js合并多个array
  • 原文地址:https://www.cnblogs.com/koushr/p/5873380.html
Copyright © 2020-2023  润新知