nginx配置文件中的配置释义

nginx配置文件结构

先上一张全局的nginx配置文件结构图,毕竟,一图胜千言。

0bIMw3

#user  nobody;
worker_processes  2;

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

#pid        logs/nginx.pid;


events {
		# 默认使用epoll
    use epoll
    # 每个worker允许连接的客户端最大连接数
    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核心配置文件中配置含义解析

  1. 设置worker进程的用户,指当然操作系统linux中的用户,会涉及到nginx操作目录或者文件的一些权限,默认为nobody,可根据需要自定义为Linux系统下其他用户。如配置为root用户。

    user  root;
    
  2. worker进程工作数设置,一般来说CPU有几个,就可以设置为几个,或者n-1个。

    worker_processes 1;
    
  3. nginx 日志级别 debug|info|notice|warn|error|crit|alert|emerg,错误级别从左到右越来越大。

    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
  4. 设置nginx进程pid

    #pid        logs/nginx.pid;
    
  5. 设置工作模式

    events {
    		# 默认使用epoll
        use epoll
        # 每个worker允许连接的客户端最大连接数
        worker_connections  1024;
    }
    
  6. http是指令块,针对于http网络传输的一些指令配置

    http{
    
    }
    
  7. include引入外部配置,提高可读性,避免因为单个配置文件过大

     include       mime.types;
    
  8. 设置日志格式,main为定义的格式名称,如此access_log就可以直接使用这个变量了

    参数名 参数含义
    $remote_addr 客户端ip
    $remote_user 远程客户端用户名,一般为:'-'
    $time_local 时间和时区
    $request 请求的url以及method
    $status 响应状态码
    $body_bytes_send 响应客户端内容字节数
    $http_referer 记录用户从哪个链接跳转过来的
    $http_user_agent 用户所使用的代理,一般来说都是浏览器
    $http_x_forwarded_for 通过代理服务器来记录客户端的ip
  9. sendfile使用高效文件传输,提升传输性能。启用后才能使用tcp_nopush,是指当数据表积累一定大小后才发送,提升了传输效率

    sendfile     on;
    tcp_nopush   on;
    
  10. keepalive_timeout设置客户端与服务端请求的超时时间, 保证客户端多次请求的时候不会重复建立新的连接,避免资源损耗。

    keepalive_timeout  0;
    keepalive_timeout  65;
    
  11. gzip启用压缩,html/js/css压缩后传输会更快。

    gzip  on;
    
  12. server可以在http指定块中设置多个虚拟主机

    • listen监听端口

    • server_name location、ip、域名

    • location请求路由映射,匹配拦截

    • root请求位置

    • index 首页设置

     #server {
        #    listen       80;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
原文地址:https://www.cnblogs.com/shine-rainbow/p/12846826.html