• Nginx配置文件nginx.conf(八)


    原文链接:https://www.cnblogs.com/knowledgesea/p/5175711.html

    在nginx.conf的注释符号是#。

    默认的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;
        #    }
        #}
    
    }
    View Code

    nginx文件结构:

    ...              #全局块
    
    events {         #events块
       ...
    }
    
    http      #http块
    {
        ...   #http全局块
        server        #server块
        { 
            ...       #server全局块
            location [PATTERN]   #location块
            {
                ...
            }
            location [PATTERN] 
            {
                ...
            }
        }
        server
        {
          ...
        }
        ...     #http全局块
    }

    1. 全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

    2. events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

    3. http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

    4. server块:配置虚拟主机的相关参数,一个http中可以有多个server。

    5. location块:配置请求的路由,以及各种页面的处理情况。

    示例一:

    ########### 每个指令必须有分号结束。#################
    #user administrator administrators;  #配置用户或者组,默认为nobody nobody。
    #worker_processes 2;  #允许生成的进程数,默认为1
    #pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
    error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
    events {
        accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
        multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
        #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
        worker_connections  1024;    #最大连接数,默认为512
    }
    http {
        include       mime.types;   #文件扩展名与文件类型映射表
        default_type  application/octet-stream; #默认文件类型,默认为text/plain
        #access_log off; #取消服务日志    
        log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
        access_log log/access.log myFormat;  #combined为日志格式的默认值
        sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
        sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
        keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。
    
        upstream mysvr {   
          server 127.0.0.1:7878;
          server 192.168.10.121:3333 backup;  #热备
        }
        error_page 404 https://www.baidu.com; #错误页
        server {
            keepalive_requests 120; #单连接请求上限次数。
            listen       4545;   #监听端口
            server_name  127.0.0.1;   #监听地址       
            location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
               #root path;  #根目录
               #index vv.txt;  #设置默认页
               proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
               deny 127.0.0.1;  #拒绝的ip
               allow 172.18.5.54; #允许的ip           
            } 
        }
    }

    上面是nginx的基本配置,需要注意的有以下几点:

    (1) $remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;

    (2) $remote_user :用来记录客户端用户名称;

    (3) $time_local : 用来记录访问时间与时区;

    (4) $request : 用来记录请求的url与http协议;

    (5) $status : 用来记录请求状态;成功是200;

    (6) $body_bytes_s ent :记录发送给客户端文件主体内容大小;

    (7) $http_referer :用来记录从那个页面链接访问过来的;

    (8) $http_user_agent :记录客户端浏览器的相关信息;

    (9) 惊群现象:一个网路连接到来,多个睡眠的进程被同事叫醒,但只有一个进程能获得链接,这样会影响系统性能;

    (10)每个指令必须有分号结束;

    示例二:

    #运行用户
    user www-data;
     
    #启动进程, 通常设置成和cpu的数量相等; 用cat /proc/cpuinfo查看cpu信息, cpu cores一栏显示内核数
    worker_processes  1;
     
    #全局错误日志及PID文件(/usr/local/nginx/logs/error.log)
    error_log  /var/log/nginx/error.log;
    #进程id存储文件(/usr/local/nginx/logs/nginx.pid)
    pid           /var/run/nginx.pid;
    #工作模式及连接数上限
    events {
        use   epoll;                            #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
        worker_connections  1024;     #单个后台worker process进程的最大并发链接数
        # multi_accept on;                #在nginx获得有新连接的通知之后,接受尽可能多的连接, 如果worker_connections设置太低的话,这样可能会造成拥堵
    }
    #设定http服务器,利用它的反向代理功能提供负载均衡支持
    http {
        #设定mime类型,类型由mime.type文件定义,可以打开mime.type来看(/usr/local/nginx/conf/mime.types)
        #MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型.
        include       /etc/nginx/mime.types;
        #默认文件类型以二进制数据传输
        default_type  application/octet-stream;
        #设定日志文件(/usr/local/nginx/logs/access.log)
        access_log    /var/log/nginx/access.log;
       
        #关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。
        server_tokens off;
     
        #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
        #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
        sendfile        on;
       
        #告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送
        #tcp_nopush     on;
     
        #给客户端分配keep-alive连接超时时间
        #keepalive_timeout  0;
        keepalive_timeout  65;
     
        #告诉nginx不要缓存数据,而是一段一段的发送;当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值
        tcp_nodelay        on;
        
        #开启gzip压缩
        gzip  on;
        #指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。
        gzip_disable "MSIE [1-6].(?!.*SV1)";
     
        #设定请求缓冲
    	client_header_buffer_size    1k;
    	large_client_header_buffers  44k;
     
        open_file_cache max=100000 inactive=20s;     #打开缓存的同时也指定了缓存最大数目,以及缓存的时间; 我们可以设置一个相对高的最大时间,这样我们可以在它们不活动超过20秒后清除掉。
        open_file_cace_valid 30s;                                #在open_file_cache中指定检测正确信息的间隔时间
        open_file_cache_min_uses 2;                           #定义了open_file_cache中指令参数不活动时间期间里最小的文件数
        open_file_cache_errors on;                              #指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。我们也包括了服务器模块,这些是在不同文件中定义的。如果你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。
     
     
        #虚拟主机的配置文件
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
       
        #设定负载均衡的服务器列表
        upstream mysvr {
             #weigth参数表示权值,权值越高被分配到的几率越大
             #本机上的Squid开启3128端口
             server 192.168.8.1:3128 weight=5;
             server 192.168.8.2:80  weight=1;
             server 192.168.8.3:80  weight=6;
        }
     
        server {
            #侦听80端口
            listen       80;
            #定义使用www.xx.com访问
            server_name  www.xx.com;
            #设定本虚拟主机的访问日志
            access_log  logs/www.xx.com.access.log  main;
           
            #默认请求
            location / {
                root   /root;      #定义服务器的默认网站根目录位置
                index index.php index.html index.htm;   #定义首页索引文件的名称
                fastcgi_pass  www.xx.com;
                fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name; 
                include /etc/nginx/fastcgi_params;
            }
           
            # 定义错误提示页面
            error_page   500 502 503 504 /50x.html;  
                 location = /50x.html {
                 root   /root;
            }
     
            #静态文件,nginx自己处理
            location ~ ^/(images|javascript|js|css|flash|media|static)/ {
                 root /var/www/virtual/htdocs;
                 #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
                 expires 30d;
            }
     
            #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
            location ~ .php$ {
                 root /root;
                 fastcgi_pass 127.0.0.1:9000;
                 fastcgi_index index.php;
                 fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
                 include fastcgi_params;
            }
     
            #设定查看Nginx状态的地址
            location /NginxStatus {
                 stub_status            on;
                 access_log              on;
                 auth_basic              "NginxStatus";
                 auth_basic_user_file  conf/htpasswd;
            }
     
            #禁止访问 .htxxx 文件
            location ~ /.ht {
                 deny all;
            }
         
         }
    }
  • 相关阅读:
    ThinkPHP之APP_DEBUG给我带来的问题
    yii框架部署
    论文翻译之--- 软件设计师怎样使用标记来帮助提醒和重新查找
    初始html5,遇到的第一个问题
    几种进入mysql的方法
    百度经验---一些生活常见问题的解决
    myeclipse背景色设置遇到的问题
    linux学习(二)-----Linux 的目录结构、远程登录、vi和vim
    linux学习(一)-----vm、centos安装
    springboot核心技术(四)-----Docker、数据访问、自定义starter
  • 原文地址:https://www.cnblogs.com/myitnews/p/11544695.html
Copyright © 2020-2023  润新知