Nginx运维之三 配置说明

配置结构

查看系统默认配置文件

vim  /usr/local/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       80;
        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;
    #    }
    #}

}

可以看出nginx给出的默认配置 大致结构如下

.....                                  #全局参数配置块
worker_processes  1;                      
.....

events {                               # nginx工作模式events块配置
	....                            
}
http {                                 # http 配置块
	....
	default_type  application/octet-stream;
	....
	
	upstream apiexample {            # 负载均衡配置块
		server 103.1.248.230:8080;
		server 104.25.253.107:8080;
	}

	server {                          # Server配置块
		....
		listen       80;
		....
		location / {                  # location 配置块
			....
			root   html;
			....
		}
        
		location ^~ /example/ {      # location 配置块二(支持多location配置)
			....
			proxy_set_header   Host    $host;
			proxy_pass   http://apiexample;
			client_max_body_size    10m;
			....
		}
	}
	
	server {
		....
		listen       443 ssl;        # Server配置块二(支持多server,案例给出的是http 和 https的分别配置)
		....
		location / {
			....
			root   html;
			....
		}
	}
    
}

简而言之,我们可以理解为Nginx配置文件主要由6个部分组成:

  • main:用于进行nginx全局信息的配置
  • events:用于nginx工作模式的配置
  • http:用于进行http协议信息的一些配置
  • upstream:用于进行负载均衡的配置
  • server:用于进行服务器访问信息的配置
  • location:用于进行访问路由的配置

main模块

 #指定nginx运行的用户及用户组,默认为nobody
#user  nobody;   

#开启的进程数,保持与逻辑CPU核数一致 cat /proc/cpuinfo| grep "processor"| wc -l
worker_processes  16;   

#定位全局错误日志文件路径和级别,日志级别依次为debug,info,notice,warn,error,crit模式,debug输出最多,crir输出最少,根据实际环境而定
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#指定进程id的存储文件位置
#pid        logs/nginx.pid;

#指定一个nginx进程打开的最多文件描述符数目,受系统进程的最大打开文件数量限制  ulimit -a 查看
#worker_rlimit_nofile 65535

events 模块

推荐配置

events
{
        use epoll;
        worker_connections 65536;
        multi_accept on;
}
指令 语法 默认值 说明
accept_mutex accept_mutex [ on | off ] off Nginx使用连接互斥锁进行顺序的accept()系统调用
multi_accept multi_accept [ on | off ] off multi_accept在Nginx接到一个新连接通知后调用accept()来接受尽量多的连接
use use [ kqueue | rtsig | epoll | /dev/poll | select | poll | eventport ] 使用的事件模型
worker_connections worker_connections number 1024 Nginx每个进程的最大连接数

事件模型

与apache相类,nginx针对不同的操作系统,有不同的事件模型:

  • 标准事件模型
    select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll

  • 高效事件模型
    kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
    epoll:使用于Linux内核2.6版本及以后的系统。
    /dev/poll:使用于Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
    eventport:使用于Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装安全补丁.

对于Linux系统,epoll工作模式是首选。

worker_connections

用于定义Nginx每个进程的最大连接数,即接收前端的最大请求数,默认是1024。最大客户端连接数由worker_processes和worker_connections决定,即

扫描二维码关注公众号,回复: 3948611 查看本文章
Max_clients=worker_processes*worker_connections

在作为反向代理时,Max_clients变为:

Max_clients = worker_processes * worker_connections/4

进程的最大连接数受Linux系统进程的最大打开文件数限制,在执行操作系统命令“ulimit -n 65536”后worker_connections的设置才能生效。

猜你喜欢

转载自blog.csdn.net/weixin_43430036/article/details/83575154
今日推荐