Linux&Nginx16_Nginx配置文件说明4

一、Nginx的核心配置文件默认放在Nginx的安装目录/user/local/nginx/conf/nginx.conf

  

二、快速编辑nginx.conf

  使用editplus编辑

     

    

 

  然后OK就行。

     

  在editplus更改完nginx.conf文件后,editplus又能上传到linux上。

三、nginx.conf 规则

 1. 用“#”表示注释

 2. 每行配置的结尾需要加上分号

 3. 如果配置项值中包括语法符号,如空格符,那么需要使用单引号或双引号括住配置项值,否则Nginx会报语法错误

 4. 单位简写。当指定空间大小时,可以使用的单位包括:K或k千字节(KiloByte,KB),M或m兆字节(MegaByte,MB)

   当指定时间时,可以使用的单位包括:ms(毫秒),s(秒),m(分钟),h(小时),d(天),w(周,包含7天),M(月,包含30天),y(年,包含365天)

四、Nginx的核心配置文件主要由三个部分构成

 1. 基本配置、全局配置

#配置worker进程运行用户 nobody也是一个linux用户,一般用于启动程序,没有密码
user  nobody;
#配置worker进程数量,根据硬件调整,通常等于CPU数量或者2倍于CPU数量
worker_processes  1;

#配置全局错误日志及类型,【debug|info|notice|warn|error|crit】,默认是error
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#配置进程pid文件位置
pid        logs/nginx.pid;     

 2. events配置

#配置工作模式和连接数
events {
    #配置单个worker进程的最大并发连接数,nginx支持的总连接数就等于work_process*work_connections
    worker_connections  1024;
}

 3. http配置

  a. 基本配置

#配置http服务器,利用它的反向代理功能提供负载均衡支持
http { # 配置nginx支持哪些多媒体类型,可以在conf/mime.types查看支持哪些多媒体类型。引入mime类型定义文件 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日志及存放路径,并使用上面定义的main日志格式 #access_log logs/access.log main; #开启高效文件传输模式 sendfile on; #与sendfile配合使用,当一个数据包累积到一定大小后发送,提升效率,防止网络阻塞 #tcp_nopush on; #连接超时时间 #keepalive_timeout 0; #长连接超时时间,单位是秒 keepalive_timeout 65; #开启zip压缩输出 #gzip on;

  b. server配置,可以有多个

#配置虚拟主机
server {
        #配置监听端口
        listen       80;
        #配置服务名IP域名,使用localhost访问
        server_name  localhost;

        #配置字符集
        #charset koi8-r;

        #配置本虚拟主机的访问日志
        #access_log  logs/host.access.log  main;
        #默认匹配斜杠/的请求,当访问路径中有斜杠/,会被该location匹配到并进行处理
        location / {
            #配置服务器的默认的网站根目录位置,默认为nginx安装主目录/usr/local/nginx下的html目录
            root   html; 
            #配置首页文件/usr/local/nginx/html/index.html的名称 索引页,欢迎页(上一篇浏览器访问192.128.240.128就是访问的这个页面)
            index  index.html index.htm; 
        }

        #配置404页面
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #错误50x错误提示页面
        error_page   500 502 503 504  /50x.html;
        
        #精确匹配
        location = /50x.html {
            root   html;
        }

        #PHP脚本请求全部转发到Apache处理
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        #PHP脚本请求全部转发到FastCGI处理
        # 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;
        #}


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


    #配置另一个虚拟主机 server配置,可以有多个   
    # 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服务,安全的网络传输协议,加密传输,端口443,运维来配置
    # 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;
    #    }
    #}

}    

 五、gzip压缩

 用于配置http服务器

 

六、location匹配规则

  1. 空格:默认匹配,普通匹配

   

location /ace {
    root /opt/static;
    index index.html;
}

   浏览器访问192.168.240.128/ace,则访问的是root/ace,即/usr/local/nginx/opt/static/ace目录下的index.html文件。

  2. =:精确匹配

   

  3. ~*:匹配正则表达式,不区分大小写

   

  4. ~:匹配正则表达式,区分大小写

   

  5. ^~:以某个字符路径开头

   

七、upstream指令

 配置http服务器里面的指令,upstream配置负载均衡

 1. max_conns参数

  限制每台server的连接数,用于保护避免过载,可起到限流作用

  

 2. slow_start参数

  缓慢启动加入集群,启动开始请求流量由小慢慢到大,商业版,需要付费

  

  

 3. down参数

  将服务器标记为永久不可用

  

 4. backup参数

  表示当前服务器节点是备用机,只有在其他所有的服务器都宕机以后,自己才会加入到集群中,被用户访问到

  

  

 5. max_fails参数

  表示失败几次,默认1次,则标记server已宕机,剔出上游服务

 6. fail_timeout参数

  表示失败的重试时间,默认10s

  

 7. keepalive

  用于提高吞吐量

  

  

八、缓存配置

  1. 浏览器端的缓存

  

  

  

  2. 反向代理端的缓存

  

九、location的匹配优先级

  

十、日志

 1. 日志配置

  a. 配置日志格式内容

  

  

  b. 配置日志

  

  

 2. 日志切割

  a. 创建shell文件

   /usr/local/nginx/sbin目录下创建logs_cut.sh可执行文件

   

  b. 添加可执行的权限

   

  c. 测试日志切割

   

  d. 使用定时任务

   安装定时任务

   

   crontab -e编辑并且添加一行新的任务

   

   重启定时任务

   

   常用定时任务命令

   

 

posted on 2022-11-25 21:43  花溪月影  阅读(149)  评论(0)    收藏  举报