• nginx 10-Nginx排查优化


    Nginx常见问题

    1、相同server_name多个虚拟主机优先级访问

    (就近原则)

    2、location匹配优先级

    =		--进行普通字符精确匹配,也就是完全匹配
    ^~		--进行普通字符匹配,使用前缀匹配
    ~ ~*	--表示执行一个正则匹配()
    
    • 配置案例:
    server {
        listen       80;
        server_name  testserver1;
        root   /opt/app;
    
        location = /code1/ {
            rewrite ^(.*)$ /code1/index.html break;
        }
        location ~ /code.* {
            rewrite ^(.*)$ /code3/index.html break;
        }
        location ^~ /code {
            rewrite ^(.*)$ /code2/index.html break;
        }
    }
    

    3、try_files使用

    • 按顺序检查文件是否存在
    • 语法配置:
    location / {
        try_files $uri $uri/ /index.php
    }
    
    • 配置案例:
    location / {
        root /opt/app/code/cache;
        try_files $uri @java_page;
    }
    
    location @java_page {
        proxy_pass http://127.0.0.1:9000;
    }
    

    4、nginx的alias和root区别

    • 配置案例:
    location /request_path/image/ {
        root /local_path/image/;
    }
    
    请求url:http://www.test.com/request_path/image/cat/png
    访问路径:/local_path/image/request_path/image/cat/png
    
    location /request_path/image/ {
        alias /local_path/image/;
    }
    
    请求url:http://www.test.com/request_path/image/cat/png
    访问路径:/local_path/image/cat/png
    

    5、用什么方法传递用户的真实IP

    set x_real_ip=$remote_addr
    

    6、Nginx中常见错误码

    Nginx:413 Request Entity Too Large
    用户上传文件限制 client_max_body_size
    
    502 bad gateway
    后端服务无响应
    
    504 Gateway Time-out
    后端服务执行超时
    

    Nginx性能测试

    1、性能优化考虑点

    • 当前系统结构瓶颈:观察指标、压力测试
    • 了解业务模式:接口业务类型、系统层次化结构
    • 性能与安全

    2、压测工具ab

    • 安装
    yum install httpd-tools
    
    • 使用
    ab -n 2000 -c 2 http://127.0.0.1
    -n	总的请求数
    -c	并发数
    -k	是否开启长连接
    
    • 返回结果
    localhost:~ user$ ab -n 100 -c 10 www.baidu.com/index.php
    This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Licensed to The Apache Software Foundation, http://www.apache.org/
    
    Benchmarking www.baidu.com (be patient).....done
    
    
    Server Software:        BWS/1.1
    Server Hostname:        www.baidu.com
    Server Port:            80
    
    Document Path:          /index.php
    Document Length:        153242 bytes
    
    Concurrency Level:      10
    Time taken for tests:   2.358 seconds
    Complete requests:      100
    Failed requests:        98
       (Connect: 0, Receive: 0, Length: 98, Exceptions: 0)
    Total transferred:      15422153 bytes
    HTML transferred:       15325288 bytes
    Requests per second:    42.41 [#/sec] (mean)
    Time per request:       235.797 [ms] (mean)
    Time per request:       23.580 [ms] (mean, across all concurrent requests)
    Transfer rate:          6387.14 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:       36   39   1.9     39      45
    Processing:   117  186  77.4    135     339
    Waiting:       38   72  38.4     46     142
    Total:        156  225  77.5    174     381
    
    Percentage of the requests served within a certain time (ms)
      50%    174
      66%    251
      75%    308
      80%    323
      90%    352
      95%    368
      98%    371
      99%    381
     100%    381 (longest request)
    localhost:~ user$
    

    系统与nginx性能优化

    • 网络、系统、服务、程序、数据库、底层服务

    1、文件句柄

    • Linux一切皆文件,文件句柄就是一个索引
    • 系统全局性修改、用户全局部性修改、进程局部性修改
    cat /etc/security/limits.conf
    
    root soft nofile 65535
    root hard nofile 65535
    * soft nofile 65535
    * hard nofile 65535
    
    • 针对nginx进程进行限制
    nginx.conf
    ...
    worker_rlimit_nofile 65535;
    http {
    ......
    }
    

    2、CPU的亲和设置

    • 把进程通常不会在处理器之间频繁迁移进程迁移的频率小,减少性能损耗。
    • 查看系统cpu情况
    cat /proc/cpuinfo | grep "processor" | wc -l
    cat /proc/cpuinfo | grep "physical id" | sort | uniq |wc -l
    
    • 查看nginx使用cpu的情况
    ps -eo pid,args,psr | grep [n]ginx
    
    • 优化配置:
    worker_processes  16;
    #worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;
    #worker_cpu_affinity 1010101010101010 0101010101010101;
    worker_cpu_affinity auto;
    .....
    http {
    ......
    }
    

    3、Nginx通用配置优化

    events {
        use epoll;
        worker_connections 10240;
    }
    http {
        ...
        charset utf-8;
        sendfile on;
        #tcp_nopush;
        #tcp_nodeny;
        keepalive_timeout 65;
        
        gzip on;
        gzip_diable "MSIE [1-6].";
        gzip_http_version 1.1;
    }
    
  • 相关阅读:
    同花顺黄金分割线及斐波那契数列分析网格(主图公式)
    走在城市计算的路上
    206. Reverse Linked List
    237. Delete Node in a Linked List
    876. Middle of the Linked List
    83. Remove Duplicates from Sorted List
    21. Merge Two Sorted List
    33. Search in Rotated Sorted Array
    852. Peak Index in a Mountain Array
    744. Find Smallest Letter Greater Than Target
  • 原文地址:https://www.cnblogs.com/liangjingfu/p/10677790.html
Copyright © 2020-2023  润新知