Nginx的配置与开发学习(七)

版权声明:转载记得宣传奥~。~ https://blog.csdn.net/c_ym_ww/article/details/88179513

Nginx常见问题

  1. 相同server_name多个虚拟主机优先级访问顺序访问
  2. location匹配优先级
  • = 进行普通字符精确匹配,也就是完全匹配

  • ^~ 表示普通字符匹配,使用前缀匹配

  • ~ \~ * 表示执行一个正则匹配()

  • server {
        listen       80;
        server_name  testserver1 jeson.t.imoocc.io;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/log/host.access.log  main;
        root   /opt/app;
    
        #location = /code1/ {
        #    rewrite ^(.*)$ /code1/index.html break;
        #}
        location ~ /code.* {
            rewrite ^(.*)$ /code3/index.html break;
        }
        #location ^~ /code {
        #    rewrite ^(.*)$ /code2/index.html break;
        #}
        .....
    }
    
  • 匹配不同的路径显示不同的页面

  1. Nginx的try_file的使用:按顺序检查文件是否存在

    • server {
          listen       80;
          server_name  testserver1 jeson.t.imoocc.io;
      
          #charset koi8-r;
          #access_log  /var/log/nginx/log/host.access.log  main;
          #root   /opt/app;
          
          location / {
              root /opt/app/code;
              try_files /cache $uri @java_page; #目录下没有页面就会请求9090端口下的jsp文件
          }
      
      
          location @java_page{
              proxy_pass http://127.0.0.1:9090;
          } 
          ....
      }
      
    • 一般会让用户先去请求缓存文件存不存在,不存在就去请求后端

  2. Nginx的alias和root区别

location /request_path/image/{
    root /local_path/image/;
   #alias /local_path/image/;
}
如果访问的是:www.baidu.com/request_path/image/cat.png
root会优先访问:/local_path/image/request_path/image/cat.png
alias会优先访问:/local_path/image/cat.png
  1. 用什么样的方法传递用户的真实IP地址

在第一级代理中约定:set x_real_ip=$remote_addr

后端服务中设置: $x_real_ip=IP1

扫描二维码关注公众号,回复: 5427023 查看本文章
  1. 其他问题
  • Nginx:413 Request Entity Too Large 如:用户上传文件限制 client_max_body_size
  • 502 bad gateway 如:后端服务无响应
  • 504 Gateway Time-out 后端服务执行超时,默认是60秒的时间

Nginx性能优化

  1. 性能优化考虑点

    1.当期系统结构瓶颈
    

    观察指标,压力测试
    2.了解业务模式
    ​ 接口业务类型,系统层次化结构
    3.性能与安全
    过于安全影响性能
    ​ ```

  2. 压测工具apache bench

1.安装
   yum install httpd-tools
2.使用
   ab -n 2000 -c 2 http://127.0.0.1/
   -n 总的请求数
   -c 并发数
   -k 是否开启长连接
  1. 系统与Nginx性能优化
  2. 网络
  3. 系统
  • 文件句柄:linux\unix 一切皆文件,文件句柄就是一个索引
  • 设置方式:系统全局性修改,用户局部性修改,进程局部性修改

vim /etc/security/limits.conf

# /etc/security/limits.conf
#
#This file sets the resource limits for the users logged in via PAM.
#It does not affect resource limits of the system services.
#
#Also note that configuration files in /etc/security/limits.d directory,
#which are read in alphabetical order, override the settings in this
#file in case the domain is the same or more specific.
#That means for example that setting a limit for wildcard domain here
#can be overriden with a wildcard setting in a config file in the
#subdirectory, but a user specific setting here can be overriden only
#with a user specific setting in the subdirectory.
#
#Each line describes a limit for a user in the form:
#
#<domain>        <type>  <item>  <value>
#
#Where:
#<domain> can be:
#        - a user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open file descriptors
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority
#
#<domain>      <type>  <item>         <value>
#

#*               soft    core            0
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4
root soft nofile 65535  #超过限制软操作:发通知
root hard nofile 65535  #超过限制硬操作:系统自动处理限制
*    soft nofile 25535
*    hard nofile 25535

# End of file
  • CPU亲和

    cat /proc/cpuinfo|grep “processor”|wc -l :总计=物理核数*每核几芯

    cat /proc/cpuinfo|grep “physical id”|sort|uniq|wc -l :cpu物理核数

    cat /proc/cpuinfo|grep “cpu cores”|uniq :每核几芯

    vim /etc/nginx/nginx.conf配置

user  nginx;
worker_processes  16; #与当前CPU核数一致
#worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;平均分配给每一个cpu
#worker_cpu_affinity 1010101010101010 0101010101010101;#区别开:1,3,5 和1,4,6
worker_cpu_affinity auto;# 自动cpu亲和


error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 35535; #进程局部性修改

events {
    use epoll;
    worker_connections  10240;
}



http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    #######
    #Charset
    charset utf-8;

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

    access_log  /var/log/nginx/access.log  main;
    
    #######
    #Core modlue
    sendfile        on;
    #tcp_nopush     on;
    #tcp_nodeny     on;
    keepalive_timeout  65;
    
    ########
    #Gzip module
    gzip  on;
    gzip_disable "MSIE [1-6]\.";  
    gzip_http_version 1.1; 
    
    ########
    #Virtal Server
    include /etc/nginx/conf.d/*.conf;
}

查看worker process的CPU运作
ps -eo pid,args,psr |grep nginx
  1. 服务本身
  2. 程序
  3. 数据库,底层服务

猜你喜欢

转载自blog.csdn.net/c_ym_ww/article/details/88179513
今日推荐