linux服务基础之nginx配置详解

nginx简单介绍:https://www.cnblogs.com/ckh2014/p/10848670.html

nginx编译安装:https://www.cnblogs.com/ckh2014/p/10848623.html

nginx相关配置:

主配置段的指令:

  正常运行的必备配置

1. user USERNAME [GROUPNAME]  
      指定运行worker进程的用户和组;
      比如:user nginx nginx;

2. pid /path/to/pid_file;
  指定nginx守护进程的pid文件:
    pid /var/run/nginx/nginx.pid;

3.worker_rlimit_nofile #;
  指定所有worker进程所能够打开的最大文件句柄数;

  性能优化相关的配置

1. worker_processes #
     worker进程的个数:通常应该略小于CPU物理核心数;

2. worker_cpu_affinity cpumask...;
    优点:提升缓存的命中率
    context switch:会产生CPU的不必要的消耗
    cpumask:
      0000 0001
      0000 0010
      0000 0100

   worker_cpu_affinity 00000001 00000010 00000100;

3. timer_resolution
  计时器解析度:降低此值,可减少gettimeofday()系统调用的次数;

4. worker_priority number;
  指明worker进程的nice值
    -20,19
    100,139

  事件相关的配置:  

1. accept_mutex {off|on};
    master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流的、序列化的去响应新请求;
2. lock_file;
  accept_mutex用到的锁文件路径;

3.use [epoll|rtsig|select|poll];
  指明使用的事件模型:建议让Nginx自行选择;

4. worker_connections #;
  设定单个worker进程所能够处理的最大并发连接数量

  worker_connections * work_processes

用于调试、定位问题:

1. daemon {on|off};
  是否以守护进程方式运行nginx, 调试时应该设置为off

2. master_process {on|off};
  是否以master/worker模型来运行nginx;调试时可以设置为off

3. error_log file | stderr | syslog:server=address[,parameter=value] | memory:size [debug | info | notice | warn | error | crit | alert | emerg];
    error_log 位置 级别;

    若要使用debug级别,需要在编译nginx时使用--with-debug选项;

总结: 常需要进行调整的参数

worker_processes, worker_connections, worker_cpu_affinity, work_priority

 新改动配置生效的方式:

nginx -s reload
  stop, quit, reopen

Nginx作为web服务器时使用的配置:

http {}: 由ngx_http_core_module模块所引入;

配置框架:

http {
   upstream {
     ...
   }

   server {
     location URL {
       root "/path/to/somedir";
        ...
     } # 类似于httpd中的<Location>,用于定义URL与本地文件系统的映射关系;
     location URL {
        if ...{
          ...
        }
     }    } # 每个server类似于httpd中的一个<VirtualHost>;
   
   server {
     ...
   }
}

注意:与httpd相关的指令仅能放置于http、server/location、upstream、if上下文,但有些指令仅应用于这5种上下文中的某些种;

配置指令:

1. server {}
  定义一个虚拟主机;
  
示例:
  server {
    listen 8080;
    server_name www.alen.com;
    root "/vhosts/web1";
  }

2. listen
   指定监听的地址和端口:
     listen address[:port];
     listen port;

3. server_name NAME [...];
  后可跟多个主机;名称还可以使用正则表达式(~)或通配符;
  (1)先做精确匹配检查:
  (2)左侧通配符匹配检查: *.alen.com
   (3) 右侧通配符匹配检查;如mail.*
   (4)正则表达式匹配检查: 如~^.*\.alen\.com$
   (5) default_server;
  server {
    server_name www.alen.com;
  }
  server {
    server_name *.alen.com;
  }
  server {
    server_name mail.*;
  }

4. root path;
  设置资源路径映射;用于指明请求的URL所对应的资源所在的文件系统上的起始路径;

5. location [ = | ~* | ^~] uri {}
   location @name {}
   功能: 允许根据用户请求的URI来匹配定义的各location;匹配到时,此请求将被相应的location配置块中的配置所处理,例如
做访问控制等功能
  =:精确匹配
  ~:正则表达式模式匹配检查,区分字符大小写;
  ~*:正则表达式模块匹配检查,不区分字符大小写;
  ^~: URI的前半部分匹配,不支持正则表达式

  匹配的优先级:精确匹配(=)、^~、~、~*、不带任何符号的location

  server {
    listen 80;
    server_name www.alen.com;
    location / {
      root "/vhosts/web1";
    }
    
    location /images/ {
      root "/vhosts/images";
    }

    location ~* \.php$ {
      fcgipass
    }
  }

6. alias path;
  用于location配置段,定义路径别名

  location /images/ {
    root "/vhosts/web1";
  }
  http://www.alen.com/images/a.jpg <-- /vhosts/web1/images/a.jpg

  location /images/ {
    alias "/www/pictures";
  }
  http://www.alen.com/images/a.jpg <-- /www/pictures/a.jpg
  
  注意: root表示指明路径为对应的location "/" URL;alias表示路径映射,即location指令后定义的URL是相对于alias所指明的路径而言;

7.index file;
  默认主页面;
    index index.php index.html;

8. error_page code [...] [=code] URI | @name
  根据http响应状态码来指明特用的错误页面;

  error_page 404 /404_customed.html
  [=code]:以指定的响应吗进行响应,而不是默认的原来的响应;默认表示以新资源的响应吗为其响应吗

9. 基于IP的访问控制
  allow IP/Network;
  deny IP/Network;

10. 基于用户的访问控制
  basic, digest;

  auth_basic "";
  auth_basic_user_file ".PATH/TO/PASSWORD_FILE"
     账号密码文件建议使用htpasswd来创建

11. https服务

  生成私钥,获得证书签署请求,并获得证书;

  #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;
    # }
  #}

12. stub_status {on|off};
  仅能用于location上下文;

  location /status {
    stub_status on;
    allow 192.168.1.0/24;
    deny all;
  }
  
  结果示例:
   Active connections:6  # 当前所有处于打开状态的连接数
   server accepts handled requests
    241 241 431  # 已经接受的连接,已经处理过的连接,已经处理过的请求数;在“保持连接”模式下,请求数量可能会多余连接数量
   Reading: 0 Writing: 1 Waiting: 5  
     # Reading: 正处于接收请求状态的连接数;
     # Writing: 请求已经接收完成,正处于处理请求或响应的过程中的连接数
     # Waiting: 保持连接模式,且处于活动状态的连接数

13. rewrite regex replacement flag;
    例如:
      rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
      
      http://www.alen.com/images/a/b/c/1.jpg --> /imgs/a/b/c/1.jpg
        
    flag:
      last: 此rewrite规则重写完成后,不再被后面其他的rewrite规则进行处理;而是由User Agent重新对重写后的URL再一次发起请求,并从头开始执行类似的过程
      break:一旦此rewrite规则重写完成后,由User Agent对新的URL重新发起请求,且不再会被当前loation内的任何rewrite规则检查
      redirect:以302响应码(临时重定向)返回新的URL;
      permanent:以301响应码(永久重定向)返回新的URL

14. if
  语法: if (condition) {...}
  应用环境: server, location
  
  condition:
    (1)变量名;
      变量值为空串,或者以“0”开始,则为false;其他均为true
    (2)以变量为操作数构成的比较表达式
      可使用=,!=类似的比较操作符进行测试
    (3)正则表达式的模式匹配操作
       ~: 区分大小写的模式匹配检查
       ~*: 不区分大小写的模式匹配检查
       !~和!~*:对上面两种测试取反
    (4)测试路径为文件可能性: -f, !-f
     (5) 测试指定路径为目录的可能性: -d, !-d
     (6)测试文件的存在性:-e,!-e
     (7)检查文件是否有执行权限:-x,!-x
    
    例如:
      if ($http_user_agent ~* MSIE) {
        rewrite ^(.*)$ /msie/$1 break;
      }

15. 防盗链
   location ~* \.(jpg|gif|jpeg|png)$ {
     valid_referer none blocked www.alen.com;
     if ($invalid_referer){
       rewrite ^/ http://www.alen.com/403.html;
     }
   }

16. 定制访问日志格式

    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;

  注意: 此处可用变量为nginx各模块内建变量;

网络连接相关的配置:

1. keepalive_timeout #;
    长连接的超时时长,默认为75s;

2.keepalive_requests #;
  在一个长连接上所能够允许请求的最大资源数

3.keepalive_disable [msie6|safari|none];
  为指定类型的User Agent禁用长连接;

4. tcp_nodelay on|off;
  是否对长连接使用TCP_NODELAY选项;

5. client_header_timeout #;
  读取http请求报文首部的超时时长;

6. client_body_timeout #;
  读取http请求报文body部分的超时时长;

7. send_timeout #;
  发送响应报文的超时时长;

 fastcgi的相关配置:

LNMP:PHP启用fpm模型;

猜你喜欢

转载自www.cnblogs.com/ckh2014/p/10849969.html