全局变量
下面是可以用作if判断的全局变量

$args : #这个变量等于请求行中的参数,同$query_string
$http_host : 请求头head里主机字段
$server_name : 服务器名称 由配置决定的
$host :先url里的host 然后$http_host 最后$server_name
$http_user_agent : 客户端agent信息
$http_cookie : 客户端cookie信息
$request_method : 客户端请求的动作,通常为GET或POST。
$remote_addr : 客户端的IP地址。
$remote_port : 客户端的端口。
$server_port : 请求到达服务器的端口号。
$request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$document_root : 当前请求在root指令中指定的值。
$request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。
$uri : 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
$document_uri : 与$uri相同。
$content_length : 请求头中的Content-length字段。
$content_type : 请求头中的Content-Type字段。
$limit_rate : 这个变量可以限制连接速率。
$remote_user : 已经经过Auth Basic Module验证的用户名。
$scheme : HTTP方法(如http,https)。
$server_protocol : 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。

配置

#运行用户
user  nginx;
#work进程数 由master进程fork的work进程数 
#一般来说按照cpu来设定 比较耗cpu 可以是2倍cpu数
worker_processes  1;
#警告日志保存目录与级别
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid 进程号文件保存位置
#pid        logs/nginx.pid;
events {
#多路复用IO liux就用这个 php里libevent就是这个 
 use   epoll; 
 #是否接受加锁排队机制 避免惊群现象 
 accept_mutex off;
 worker_rlimit_nofile 2000;
 #一个work进程有一个线程池 线程池有多个线程连接 一个线程处理一个请求
  worker_connections  1024;
}
http {
    #mime类型列表文件
    include       mime.types;
    #可接受的mime类型
    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  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        #错误页面
        #error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    # HTTPS server
    #
    #server {
    #https端口
    #    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;
    #    }
    #}

}


连接数计算

cpu个数
cat /proc/cpuinfo | grep “processor” | wc -l

worker_processes 一般依据cpu个数 比较耗cpu cpu个数*2

系统最大打开文件数
cat /proc/sys/fs/file-max

一个work进程极限的连接数
worker_rlimit_nofile = 系统最大打开文件数/worker_processes
如果作为反向代理
worker_rlimit_nofile =系统最大打开文件数/worker_processes/2

worker_connections < worker_rlimit_nofile 即可