Nginx的配置

作者: 我是好宁 | 来源:发表于2017-06-13 15:52 被阅读0次

Nginx的配置

配置语法

默认的配置文件nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    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  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8008;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


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

}

块配置项

块配置项由一个块配置项名称和一对大括号组成。如:

events {
...
}

http {
    upstream backend {
        server 127.0.0.1:8080;
    }
    
    gzip on;
    server {
        ...
        location /webstatic {
            gzip off;
        }
    }
}

上段代码中events,http,upstream,server,location等都是块配置项,块配置项之后是否如“location /webstatic{...}”那样在后面加上参数。取决于这个块配置项的模块,不能一概而论,但块配置项一定会用大括号把一系列所属的配置项全包含进来,表示括号内的配置项同时生效。所有的事件类配置都要在events块中,http、server等配置也遵循这个规定。
块配置项可以嵌套。内层块直接继承外层块。当内外层块发生冲突时,以哪一层的块配置为准,取决于解析这个配置项的模块。上述例子中以内层location gzip off;配置为准

块配置项的语法

上文中示例可以看出,最基本的配置项语法样式如下:
配置项名 配置项值1 配置项值2 ...;

配置项的注释

如果有一个配置项暂时需要注释掉,那么可以加“#”注释掉这一行

配置项的单位

大部分模块遵循一些通用的规定,如指定空间大小时不用每次读定义到字节、指定时间时不用精确到到毫秒(不写单位的时候默认使用最小的单位)
当指定空间大小时可以使用的单位包括:
K或k千字节
M或m兆字节
例如:

gzip_buffers   48k;
client_mac_body_size   64M;

时间单位包括:
ms毫秒 s秒 m分钟 h小时 d天 w周,包含7天 M,月包含30天 y 年,包含365天
例如:

expires    10y;
proxy_read_timeout 600;
client_body_timeout    2m;

在配置项中使用变量

在有写模块中允许使用变量,比如在日志记录部分:

lo_format main '$remote_addr - $remote_user [$time_local] "$request"'
                '$status $bytes_sent "$http_referer"'
                '"$http_user_agent" "http_x_forwarded_for"';

其中,remote_addr等都是变量。使用它的时候需要在前面加上$符号。但也不是所有模块都支持变量的。

Nginx服务的基本配置

用于调试进程和定位问题的配置项

1.是否仪守护进程方式运行Nginx
语法: daemon on|off;
(注意:有些配置项,即使没有显示的进行配置。他们也会有默认的值。比如这个daemon)
默认: daemon on;
2.是否以master/worker 方式工作
语法: master+process on|off;
默认: master+process on;
3.error日志的设置
语法: error_log /path/file level
默认: error_log logs/error.log error
(/path/file 参数可以是一个具体的文件,也可以是/dev/null,这样就不会有任何的日志输出了,这也是关闭日志输出的唯一方法。level是日志的输出级别 取值范围是 debug info notice warn error crit alert emerg 从左到右级别增大,日志不会输出级别比当前设定级别低的日志。)
4.针对指定的客户端进行debug级别的日志输出
语法: debug_connection [IP/CIDR]
(它属于时间类型的配置所以必须放在events{...}中才会生效,它的值可以是IP地址或者CIDR地址)
例如:

events {    
    debug_connection    10.10.10.10;
    debug_connection    10.10.10.10/33;
}

注意: 如果需要输出debug级别的日志,需要在安装nginx 执行 configure 时加入 --with-debug参数 否则不会生效

正常运行的配置项

1.定义环境变量
语法: env VAR|VAR=VALUE
这个配置项可以让用户直接设置操作系统上的环境变量。例如:
env TESTPATH=/tmp/;
2.嵌入其他配置文件
语法: include /path/file
include 配置项可以将其他配置文件嵌入到当前的nginx.conf文件中,可以是绝对路径或相对路径,并且可以用通配符*
例如:

    include my.conf;
    include /conf/nginx/*.conf

3.pid文件的路径
语法: pid path/file
默认: pid /logs/nginx.pid
(默认与configure执行时的参数“--pid-path”所指定的路径是相同的)
4.Nginx worker进程运行的用户及用户组
语法: user username[groupname]
默认: user nobody nobody
(用于设置master进程启动后,fork出的worker进程运行在那个用户和用户组下。)
5.指定 Nginx worker 进程可以打开的最大句柄描述符个数
语法: worker_rlimit_nofile limit;
6.限制信号队列
语法: worker_rlimit_sigpending limit;
(设置每个用户发往Nginx的信号队列的大小。当某个用户的信号队列满了,这个用户在发送的信号量会被丢掉。)

优化性能的配置项

1.Nginx worker进程个数
语法: worker_process number;
默认: worker_process 1;
(在master/worker 运行方式下,定义worker进程的个数,一般情况下,配置与CPU内核数量相等的worker进程,并且使用下面的worker_cpu_affinity配置来绑定CPU内核)
2.绑定Nginx worker进程到指定CPU内核
语法: worker_cpu_affinity cpuask[cpuask...];
例如:

    worker_process 4;
    worker_cpu_affinity 1000 0100 0010 0001;

0001表示启用第一个CPU内核,0010表示启用第二个CPU内核,依此类推

3.Nginx worker进程优先级设置
语法: worker_priority nice
默认: worker_priority 0
(取值范围是-20~+19 -20优先级最高 不建议比内核的进程nice(一般为-5)值更小)

事件类的配置项

1.是否打开accept锁
语法: accept_mutext [on|off];
默认: accept_mutext on;
(accept_mutext 可以让worker进程轮流的,序列化的与新的客户端建立TCP连接)
2.lock 文件路径
语法: lock_file path/file;
默认: lock_file logs/nginx.lock;
(用于accept_mutext锁)
3.使用accept锁后到正真建立连接之间的延迟时间
语法: accept_mutex_delay Nms;
默认: accept_mutex_delay 500ms;
4.批量建立新连接
语法: multi_accept [on|off];
默认: multi_accept off;
(当事件模型通知有新连接时,尽可能的对本次调度中客户端发起的所有TCP请求都建立连接)
5.选择时间模型
语法: use [epoll|poll|select];
默认: use epoll;
(epoll是性能最好的)
6.每个worker的最大链接数
语法: worker_connections number;
(定义每个worker进程可以同时处理的最大连接数)

相关文章

网友评论

    本文标题:Nginx的配置

    本文链接:https://www.haomeiwen.com/subject/njydqxtx.html