美文网首页
nginx配置文件

nginx配置文件

作者: 天生顽皮 | 来源:发表于2021-05-02 17:21 被阅读0次

1.Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束

//nginx默认配置语法
worker_processes    //工作进程, 配置和CPU个数保持一致
error_log           //错误日志, 后面接入的是路径
pid                 //Nginx服务启动时的pid

//events事件模块
events {    //事件模块            
    worker_connections  1024;   //每个worker进程支持的最大连接数
    use                 //内核模型,select,poll,epoll
}

//非虚拟主机的配置或公共配置定义在http{}段内, server{}段外
http {
...    
    //必须使用虚拟机配置站点, 每个虚拟机使用一个server{}段
    server {
        listen       80;    //监听端口, 默认80
        server_name  localhost; //提供服务的域名或主机名
        
        //控制网站访问路径
        location / {
            root   /usr/share/nginx/html;   //存放网站路径
            index  index.html index.htm;    //默认访问首页文件
        }
        //错误页面,统一定义错误页面
        //定义请求错误, 指定错误代码
        
        则。
        error_page   500 502 503 504  /50x.html;
        //错误代码重定向到新的Locaiton
        location = /50x.html {
            root   html;
        }
    }
    ...
    //第二个虚拟主机配置
    server {
    ...
    }
}

2.nginx 日志配置

查看HTTP的请求和返回
curl -v http://ww.baidu.com
nginx 日志配置规范
//配置语法: 包括: error.log access.log
Syntax: log_format name [escape=default|json] string ...;
Default:    log_format combined "...";
Context:    http

//Nginx默认配置
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                   '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

$remote_addr    //表示客户端地址
$remote_user    //http客户端请求nginx认证用户名
$time_local     //Nginx的时间
$request        //Request请求行, GET等方法、http协议版本
$status         //respoence返回状态码
$body_bytes_sent    //从服务端响应给客户端body信息大小
$http_referer       //http上一级页面, 防盗链、用户行为分析
$http_user_agent    //http头部信息, 客户端访问设备
$http_x_forwarded_for   //http请求携带的http信息

3.nginx 状态监控

--with-http_stub_status_module记录Nginx客户端基本访问状态信息

配置文件
location /mystatus {
    stub_status on;
    access_log off;
}

//Nginx_status概述
Active connections:2    //Nginx当前活跃连接数
server accepts handled requests
16     16     19
server表示Nginx启动到现在共处理了16个连接。
accepts表示Nginx启动到现在共成功创建16次握手。
请求丢失数=(握手数-连接数)可以看出,本次状态显示没有丢失请求。
handled requests,表示总共处理了19次请求。
Reading     Nginx读取到客户端的 Header 信息数。
Writing     Nginx返回给客户端的 Header 信息数。
Waiting    Nginx开启keep-alive长连接情况下, 既没有读也没有写, 建立连接情况
验证   上百度   www.你配置的域名/status

4.nginx 下载站点

Syntax: autoindex on | off;
Default:    
autoindex off;
Context:    http, server, location

//autoindex常用参数
autoindex_exact_size off;
默认为on, 显示出文件的确切大小,单位是bytes。
修改为off,显示出文件的大概大小,单位是kB或者MB或者GB。

autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
修改为on, 显示的文件时间为文件的服务器时间。

charset utf-8,gbk;
默认中文目录乱码,添加上解决乱码。

配置文件
//开启目录浏览
location / {
    root html;
    autoindex on;
    autoindex_localtime on;
    autoindex_exact_size off;
}

5.nginx 访问限制

连接频率限制 limit_conn_module
请求频率限制 limit_req_module
nginx 连接配置$$$$
//全局定义连接限制
Syntax:  limit_conn_zone key zone=name:size;
Default: —
Context: http
//引用连接限制
Syntax: limit_conn zone number;
Default: —
Context: http, server, location


//具体配置如下:
http {
//http段配置连接限制, 同一时刻只允许一个客户端IP连接
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
    ...
    server {
    ...  

        location / {
        //同一时刻只允许一个客户端IP连接
            limit_conn conn_zone 1;
        }


//压力测试
yum install -y httpd-tools
ab -n 50 -c 20  http://127.0.0.1/index.html
nginx 请求配置¥¥¥¥
//全局定义请求限制
Syntax:  limit_conn_zone key zone=name:size rate=rate;
Default: —
Context: http
//引用请求限制
Syntax: limit_conn zone number [burst=number] [nodelay];
Default: —
Context: http, server, location


//具体配置如下:
http {
//http段配置请求限制, rate限制速率,限制一秒钟最多一个IP请求
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
    ...
    server {
    ...  

        location / {
        //1r/s只接收一个请求,其余请求拒绝处理并返回错误码给客户端
            limit_req zone=req_zone;
        //请求超过1r/s,剩下的将被延迟处理,请求数超过burst定义的数量, 多余的请求返回503
            #limit_req zone=req_zone burst=3 nodelay;
        }


//压力测试
yum install -y httpd-tools
ab -n 50 -c 20  http://127.0.0.1/index.html

6.nginx 访问控制

基于IP的访问控制 http_access_module
基于用户登陆认证 http_auth_basic_module
基于IP的访问控制
//允许配置语法
Syntax: allow address | CIDR | unix: | all;
Default:    —
Context:    http, server, location, limit_except
//拒绝配置语法
Syntax: deny address | CIDR | unix: | all;
Default:    —
Context:    http, server, location, limit_except

//配置拒绝某一个IP, 其他全部允许
location ~ ^/1.html {
    root /usr/share/nginx/html;
    index index.html;
    deny 192.168.56.1;
    allow all;
}

//只允许某一个网段访问,其它全部拒绝
location / {
    root   html;
    index  index.php index.html index.htm;
    allow   192.168.56.0/24;
    deny    all;
}
基于用户登陆认证
//配置语法
Syntax: auth_basic string| off;
Default:    auth_basic off;
Context:    http, server, location, limit_except
//用户密码记录配置文件
Syntax: auth_basic_user_file file;
Default:    -
Context:    http, server, location, limit_except


//需要安装依赖组件
[root@xuliangwei ~]# yum install httpd-tools
[root@xuliangwei ~]# htpasswd -c /etc/nginx/auth_conf xuliangwei

//可在http,server,location下添加如下信息
auth_basic "Auth access Blog Input your Passwd!";
auth_basic_user_file /etc/nginx/auth_conf;

相关文章

  • 第二讲 Nginx模块详解

    本章要点 Nginx 配置文件结构 各个模块的详解 2.1 Nginx配置文件结构 Nginx的配置文件nginx...

  • Nginx配置文件详解

    Nginx配置文件nginx.conf详解 nginx.conf nginx技术一--配置文件nginx.conf...

  • 应用程序常用命令

    nginx: nginx启动:nginx -c nginx配置文件地址检查配置文件是否正确:nginx -t重启n...

  • nginx(五)nginx cmd

    nginx cmd 1、启动nginx start nginx 2、修改配置文件并生效 测试nginx配置文件是否...

  • Nginx配置文件nginx.conf详解和nginx的变量规则

    Nginx配置文件nginx.conf详解 Nginx 总的 配置文件 位置 /usr/local/nginx/c...

  • Nginx使用

    Nginx [toc] nginx命令 参数 Nginx启动 通过指定配置文件启动 配置文件语法检查 Nginx配...

  • nginx配置文件

    nginx配置文件nginx配置文件详解一、nginx配置文件 启动子进程程序默认用户 user nobody;...

  • Nginx 打印body体内容

    Nginx 打印body体内容,修改nginx配置文件nginx.conf 重新加载配置文件 nginx body...

  • nginx(二)

    在修改配置文件后,检查 nginx 配置文件语法是否正确:nginx -t 重新启动 nginx: nginx -...

  • Nginx yum安装目录

    日志切割配置文件/etc/logrotate.d/nginx主要配置文件/etc/nginx/etc/nginx/...

网友评论

      本文标题:nginx配置文件

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