zoukankan      html  css  js  c++  java
  • nginx核心配置详解

    Nginx核心配置详解

    发布时间:2020-02-27 08:32:02 来源:51CTO 阅读:300 作者:何小帅 栏目:系统运维

    1 全局配置

    user nginx nginx; # 启动nginx工作进程的用户和组,默认为nobody
    worker_processes auto; # 启动nginx工作进程的数量,默认为1
    worker_cpu_affinity 0001 0010 0100 1000; #将Nginx⼯作进程绑定到指定的CPU核⼼,默认Nginx是不进⾏进程绑定的,绑定并不是意味着当前nginx进程独占⼀核⼼CPU,但是可以保证此进程不会运⾏在其他核⼼上,这就极⼤减少了nginx的⼯作进程在不同的cpu核⼼上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能,也可以设置为auto。
    可以执行以下命令观察工作进程是否一致运行在同一核CPU上
    [root@CentOS7-01 ~]#watch -n1 'ps axo pid,cmd,psr,user | grep nginx|grep -v grep'
    #错误⽇志记录配置,语法:error_log file [debug | info | notice | warn | error | crit | alert | emerg]
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    error_log  logs/error.log  error;
    
    pid /run/nginx.pid; # pid文件保存路径
    worker_priority 0; # 工作进程nice值,-20~19
    worker_rlimit_nofile 65536; #这个数字包括Nginx的所有连接(例如与代理服务器的连接等),⽽不仅仅是客户端的连接,另⼀个考虑因素是实际的并发连接数不能超过系统级别的最⼤打开⽂件数的限制
    daemon off; #前台运⾏Nginx服务⽤于测试、docker等环境。
    master_process off|on; #是否开启Nginx的master-woker⼯作模式,仅⽤于开发调试场景。
    events { #事件模型配置参数
    worker_connections 65536; #设置单个⼯作进程的最⼤并发连接数
    use epoll; #使⽤epoll事件驱动,Nginx⽀持众多的事件驱动,⽐如select、poll、epoll,只能设置在events模块中设置。
    accept_mutex on; #优化同⼀时刻只有⼀个请求⽽避免多个睡眠进程被唤醒的设置,on为防⽌被同时唤醒,默认为off,全部唤醒的过程也成为"惊群",因此nginx刚安装完以后要进⾏适当的优化。
    multi_accept on; #Nginx服务器的每个⼯作进程可以同时接受多个新的⽹络连接,但是需要在配置⽂件中配置,此指令默认为关闭,即默认为⼀个⼯作进程只能⼀次接受⼀个新的⽹络连接,打开后即可同时接受多个。

    2 http详细配置

    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; #在开启了sendfile的情况下,合并请求后统一发送给客户端
        #tcp_nodelay off; #在开启了keepalived模式下的连接是否启⽤TCP_NODELAY选项,当为off时,延迟0.2s发送,默认为on,不延迟发送,⽴即发送用户响应报⽂。     
        #keepalive_timeout  0;
        keepalive_timeout  65; #设置会话保持时间
        #gzip  on; #开启文件压缩
    
        server {
            listen       80; #设置监听地址和端口
            server_name  localhost; #设置server name,可以以空格隔开写多个并支持正则表达式
    
            #charset koi8-r; #设置编码格式,默认是俄语格式,可以改为utf-8
    
            #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; #以http的⽅式转发php请求到指定web服务器
            #}
    
            # 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的⽅式转发php请求到php处理
            #    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 { #拒绝web形式访问指定⽂件,如很多的⽹站都是通过.htaccess⽂件来改变⾃⼰的重定向等功能。
            #    deny  all;
            #}
        }
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server { #⾃定义虚拟server
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm; #指定默认⽹⻚⽂件,此指令由ngx_http_index_module模块提供
        #    }
        #}
    
        # HTTPS server
        #
        #server { #https服务器配置
        #    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;
        #    }
        #}
    
    }

    3 核心配置示例

    基于不同的IP、端口和域名实现不同功能的虚拟主机,依赖于核心模块ngx_http_core_module实现。

    3.1 新建一个PC web站点

    [root@CentOS7-01 ~]#mkdir /apps/nginx/conf/vhosts
    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location / {
        root html/pc;
      }
    }
    [root@CentOS7-01 ~]#mkdir /apps/nginx/html/pc
    [root@CentOS7-01 ~]#echo "pc web" > /apps/nginx/html/pc/index.html
    [root@CentOS7-01 ~]#vim /apps/nginx/conf/nginx.conf
    include /apps/nginx/conf/vhosts/*.conf; #添加在http块
    [root@CentOS7-01 ~]#sed -i '1s/$/ pc.hechunping.tech/' /etc/hosts
    
    访问测试
    [root@CentOS7-01 ~]#curl -i pc.hechunping.tech
    HTTP/1.1 200 OK
    Server: nginx/1.16.1
    Date: Thu, 02 Jan 2020 12:07:55 GMT
    Content-Type: text/html
    Content-Length: 7
    Last-Modified: Thu, 02 Jan 2020 11:32:27 GMT
    Connection: keep-alive
    ETag: "5e0dd4cb-7"
    Accept-Ranges: bytes
    
    pc web

    3.2 新建一个Mobile web站点

    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/mobile.conf 
    server {
      listen 80;
      server_name mobile.hechunping.tech;
      location / {
        root html/mobile;
      }
    }
    [root@CentOS7-01 ~]#mkdir /apps/nginx/html/mobile
    [root@CentOS7-01 ~]#echo "mobile web" > /apps/nginx/html/mobile/index.html
    [root@CentOS7-01 ~]#sed -i '1s/$/ mobile.hechunping.tech/' /etc/hosts
    [root@CentOS7-01 ~]#systemctl reload nginx
    
    访问测试
    [root@CentOS7-01 ~]#curl -i mobile.hechunping.tech/index.html
    HTTP/1.1 200 OK
    Server: nginx/1.16.1
    Date: Thu, 02 Jan 2020 12:05:26 GMT
    Content-Type: text/html
    Content-Length: 11
    Last-Modified: Thu, 02 Jan 2020 12:04:43 GMT
    Connection: keep-alive
    ETag: "5e0ddc5b-b"
    Accept-Ranges: bytes
    
    mobile web

    3.3 root与alias

    3.3.1 root:指定web的家⽬录,在定义location的时候,⽂件的绝对路径等于root+location,如:

    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location / {
        root html/pc;
      }
      location /about {
        root html/pc; #在pc目录下,必须要有about这个目录,否则访问的时候会报404错误
      }
    }
    [root@CentOS7-01 ~]#mkdir /apps/nginx/html/pc/about
    [root@CentOS7-01 ~]#echo "about page ..." > /apps/nginx/html/pc/about/index.html
    
    访问测试
    [root@CentOS7-01 ~]#curl -i pc.hechunping.tech/about/index.html
    HTTP/1.1 200 OK
    Server: nginx/1.16.1
    Date: Thu, 02 Jan 2020 12:28:07 GMT
    Content-Type: text/html
    Content-Length: 15
    Last-Modified: Thu, 02 Jan 2020 12:25:03 GMT
    Connection: keep-alive
    ETag: "5e0de11f-f"
    Accept-Ranges: bytes
    
    about page ...

    3.3.2 alias:定义路径别名,会把访问的路径重新定义到其指定的路径,如:

    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location / {
        root html/pc;
      }
      location /alpc { #使⽤alias的时候uri后⾯如果加了"/"则下⾯的路径配置必须加"/",否则访问报错
        alias html/pc; #当访问alpc的时候,会显⽰alias定义的/apps/nginx/html/pc⾥⾯的内容。
      }
    }
    
    访问测试
    [root@CentOS7-01 ~]#systemctl reload nginx
    [root@CentOS7-01 ~]#cat /apps/nginx/html/pc/index.html 
    pc web
    [root@CentOS7-01 ~]#curl -i pc.hechunping.tech/alpc/index.html
    HTTP/1.1 200 OK
    Server: nginx/1.16.1
    Date: Thu, 02 Jan 2020 13:07:30 GMT
    Content-Type: text/html
    Content-Length: 7
    Last-Modified: Thu, 02 Jan 2020 11:32:27 GMT
    Connection: keep-alive
    ETag: "5e0dd4cb-7"
    Accept-Ranges: bytes
    
    pc web

    3.3.3 两者间的区别

    1)alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias指定的目录下查找的,location匹配访问的path目录的名称可以任意指定,该类型类似于Linux文件系统的软连接功能;
    2)alias虚拟配置目录中,location匹配的path目录后⾯如果加了"/",则alias指定的目录后⾯也必须加"/",否则403
    3)root指定的目录是location匹配访问的path目录的父目录,这个path目录一定要是真实存在于root指定目录下的子目录;
    4)root目录配置中,location匹配的path目录后面带不带"/",都不会影响访问。

    3.4 location的详细使用

    语法规则: location [=|~|~*|^~] /uri/ { … }
    =   #⽤于标准uri前,需要请求字串与uri精确匹配,如果匹配成功就停⽌向下匹配并⽴即处理请求。
    ~   #⽤于标准uri前,表⽰包含正则表达式并且区分⼤⼩写,并且匹配
    !~  #⽤于标准uri前,表⽰包含正则表达式并且区分⼤⼩写,并且不匹配
    ~*  #⽤于标准uri前,表⽰包含正则表达式并且不区分⼤写,并且匹配
    !~* #⽤于标准uri前,表⽰包含正则表达式并且不区分⼤⼩写,并且不匹配
    ^~  #⽤于标准uri前,表⽰包含正则表达式并且匹配以什么开头
    $   #⽤于标准uri前,表⽰包含正则表达式并且匹配以什么结尾
       #⽤于标准uri前,表⽰包含正则表达式并且转义字符。可以转. * ?等
    *   #⽤于标准uri前,表⽰包含正则表达式并且代表任意⻓度的任意字符

    3.4.1 精确匹配

    在server部分使用location配置一个web界面,要求:当访问nginx服务器的指定资源时,显示指定html文件的内容
    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location /test.html {
        root /apps/nginx/html/mobile;
      }
    
      location = /test.html {
        root /apps/nginx/html/pc;
      }
    }
    [root@CentOS7-01 ~]#cat /apps/nginx/html/mobile/test.html 
    mobile location
    [root@CentOS7-01 ~]#cat /apps/nginx/html/pc/test.html 
    pc location
    [root@CentOS7-01 ~]#systemctl reload nginx
    [root@CentOS7-01 ~]#curl pc.hechunping.tech/test.html
    pc location
    
    从上面的测试结果可以发现,返回的是精确匹配的内容

    3.4.2 区分大小写匹配

    只匹配以小写html结尾的文件
    [root@CentOS7-01 mobile]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location ~ .html$ {
        root /apps/nginx/html/mobile;
      }
    }
    [root@CentOS7-01 mobile]#ls /apps/nginx/html/mobile/test.*
    /apps/nginx/html/mobile/test.html  /apps/nginx/html/mobile/test.Html
    [root@CentOS7-01 mobile]#curl --head pc.hechunping.tech/test.html
    HTTP/1.1 200 OK
    Server: nginx
    Date: Fri, 03 Jan 2020 13:37:42 GMT
    Content-Type: text/html
    Content-Length: 16
    Last-Modified: Fri, 03 Jan 2020 12:53:04 GMT
    Connection: keep-alive
    ETag: "5e0f3930-10"
    Accept-Ranges: bytes
    
    [root@CentOS7-01 mobile]#curl --head pc.hechunping.tech/test.Html
    HTTP/1.1 404 Not Found
    Server: nginx
    Date: Fri, 03 Jan 2020 13:37:52 GMT
    Content-Type: text/html
    Content-Length: 146
    Connection: keep-alive

    3.4.3 不区分大小写匹配

    此模式无论是大小写html结尾的文件都匹配,大小写可以混合,通常使用此模式匹配用户请求的静态资源并继续做下一步操作
    [root@CentOS7-01 mobile]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location ~* .html$ {
        root /apps/nginx/html/mobile;
      }
    }
    [root@CentOS7-01 mobile]#systemctl reload nginx
    [root@CentOS7-01 mobile]#ls /apps/nginx/html/mobile/test.*
    /apps/nginx/html/mobile/test.html  /apps/nginx/html/mobile/test.Html
    [root@CentOS7-01 mobile]#curl --head pc.hechunping.tech/test.html
    HTTP/1.1 200 OK
    Server: nginx
    Date: Fri, 03 Jan 2020 13:44:08 GMT
    Content-Type: text/html
    Content-Length: 16
    Last-Modified: Fri, 03 Jan 2020 12:53:04 GMT
    Connection: keep-alive
    ETag: "5e0f3930-10"
    Accept-Ranges: bytes
    
    [root@CentOS7-01 mobile]#curl --head pc.hechunping.tech/test.Html
    HTTP/1.1 200 OK
    Server: nginx
    Date: Fri, 03 Jan 2020 13:44:14 GMT
    Content-Type: text/html
    Content-Length: 4
    Last-Modified: Fri, 03 Jan 2020 13:27:17 GMT
    Connection: keep-alive
    ETag: "5e0f4135-4"
    Accept-Ranges: bytes

    3.4.4 以某个URI开头匹配

    只匹配以abc开头的uri下的内容就结束搜索,而不会再匹配uri中包含abc对应目录下的内容
    [root@CentOS7-01 mobile]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location ^~ /abc {
        root /apps/nginx/html/mobile;
      }
      location /babc {
        root /apps/nginx/html/mobile;
      }
    }
    [root@CentOS7-01 mobile]#cat /apps/nginx/html/mobile/abc/index.html 
    abc page
    [root@CentOS7-01 mobile]#cat /apps/nginx/html/mobile/babc/index.html 
    babc page
    [root@CentOS7-01 mobile]#systemctl reload  nginx
    [root@CentOS7-01 mobile]#curl  pc.hechunping.tech/abc/
    abc page

    3.4.5 不区分大小写匹配以某个文件名结尾的文件

    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location ~* .(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
        root /apps/nginx/html/image;
      }
    }
    [root@CentOS7-01 ~]#ls /apps/nginx/html/image/
    1.jpg  2.jpg  3.png  4.jpEg  Ab.jS  timg.jpg
    [root@CentOS7-01 ~]#curl -I pc.hechunping.tech/4.jpEg
    HTTP/1.1 200 OK
    Server: nginx
    Date: Fri, 03 Jan 2020 14:11:07 GMT
    Content-Type: image/jpeg
    Content-Length: 16228
    Last-Modified: Fri, 03 Jan 2020 14:03:22 GMT
    Connection: keep-alive
    ETag: "5e0f49aa-3f64"
    Accept-Ranges: bytes
    
    [root@CentOS7-01 ~]#curl -I pc.hechunping.tech/Ab.jS
    HTTP/1.1 200 OK
    Server: nginx
    Date: Fri, 03 Jan 2020 14:11:18 GMT
    Content-Type: application/javascript
    Content-Length: 16228
    Last-Modified: Fri, 03 Jan 2020 14:10:19 GMT
    Connection: keep-alive
    ETag: "5e0f4b4b-3f64"
    Accept-Ranges: bytes
    
    [root@CentOS7-01 ~]#curl -I pc.hechunping.tech/3.png
    HTTP/1.1 200 OK
    Server: nginx
    Date: Fri, 03 Jan 2020 14:11:34 GMT
    Content-Type: image/png
    Content-Length: 16228
    Last-Modified: Fri, 03 Jan 2020 14:03:11 GMT
    Connection: keep-alive
    ETag: "5e0f499f-3f64"
    Accept-Ranges: bytes

    3.4.6 优先级

    匹配优先级:=,^~,~/~*,/
    location优先级:(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

    3.4.7 生产使用案例

    直接匹配⽹站根会加速Nginx访问处理:
    location = / {
    ......;
    }
    location / {
    ......;
    }
    静态资源配置:
    location ^~ /static/ {
    ......;
    }
    # 或者
    location ~* .(gif|jpg|jpeg|png|css|js|ico)$ {
    ......;
    }
    多应⽤配置
    location ~* /app1 {
        ......;
    }
    location ~* /app2 {
    ......;
    }

    3.5 nginx四层访问控制

    访问控制基于模块ngx_http_access_module实现,可以通过匹配客户端源IP地址进⾏限制,这里只允许192.168.7.72这个ip访问
    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location /pc {
        root /apps/nginx/html;
        allow 192.168.7.72;
        deny all;
      }
    }
    访问测试,192.168.7.72是另一台Linux服务器的地址,这里分别在这台Linux服务器和物理机上进行测试
    Linux服务器
    [root@CentOS7-02 ~]#ifconfig eth0 | awk -F"[ ]+" 'NR==2{print $3}'
    192.168.7.72
    [root@CentOS7-02 ~]#curl -i pc.hechunping.tech/pc/
    HTTP/1.1 200 OK
    Server: nginx
    Date: Sat, 04 Jan 2020 01:45:18 GMT
    Content-Type: text/html
    Content-Length: 7
    Last-Modified: Thu, 02 Jan 2020 11:32:27 GMT
    Connection: keep-alive
    ETag: "5e0dd4cb-7"
    Accept-Ranges: bytes
    
    pc web
    
    物理机
    从访问日志中可以发现403状态码
    [root@CentOS7-01 ~]#tail -n1 -f /apps/nginx/logs/access.log 
    192.168.7.1 - - [04/Jan/2020:09:46:12 +0800] "GET /pc/ HTTP/1.1" 403 548 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"

    3.6 nginx账户认证功能

    1)生成密码文件,可以使用两种方法
    方法1:使用htpasswd命令生成,需要安装httpd-tools包
    [root@CentOS7-01 ~]#yum -y install httpd-tools
    [root@CentOS7-01 ~]#htpasswd -cb /apps/nginx/conf/.passwd user1 123456 #只有创建第一个用户的时候需要加-c参数,后面新建用户的时候不用加该参数,否则会覆盖前面创建的用户名密码。
    Adding password for user user1
    [root@CentOS7-01 ~]#htpasswd -b /apps/nginx/conf/.passwd user2 123456
    Adding password for user user2
    [root@CentOS7-01 ~]#cat /apps/nginx/conf/.passwd 
    user1:$apr1$/4bSUD79$AUHF6.EYkLwW6pvtinl/N1
    user2:$apr1$F1c./Mk9$k3LBFsZ.EFaTSU6PdMk5r1
    方法2:使用openssl命令生成
    [root@CentOS7-01 ~]#printf "user1:$(openssl passwd -crypt 123456)
    " > /apps/nginx/conf/.passwd
    [root@CentOS7-01 ~]#printf "user2:$(openssl passwd -crypt 123456)
    " >> /apps/nginx/conf/.passwd #在创建第二个用户的时候使用追加的方式,否则会覆盖前面创建的用户名密码。
    [root@CentOS7-01 ~]#cat /apps/nginx/conf/.passwd 
    user1:/p2JrkKzl2VvY
    user2:.yQfiUOWOMJJE
    2)配置nginx配置文件
    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location /pc {
        root /apps/nginx/html;
        auth_basic "login password";
        auth_basic_user_file /apps/nginx/conf/.passwd;
      }
    }
    [root@CentOS7-01 ~]#systemctl reload nginx
    3)访问测试
    在物理机进行访问,访问成功的话,在nginx的访问日志中可以获取到远程用户信息,如下
    [root@CentOS7-01 ~]#tail -n1 -f /apps/nginx/logs/access.log 
    192.168.7.1 - user1 [04/Jan/2020:10:20:21 +0800] "GET /pc/ HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"

    Nginx核心配置详解

    3.7 自定义错误页面

    当我们访问一个不存在的页面的时候,浏览器页面通常会报一个大大的404 Not Found,这样看起来显然是很不友好的,所以可以通过自定义错误页面的方式,把404错误定义成自己写的内容
    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      error_page 404 /error.html;
      location = /error.html {
        root html;
      }
    }
    [root@CentOS7-01 ~]#echo "您访问的页面不见了~~~" > /apps/nginx/html/error.html
    [root@CentOS7-01 ~]#systemctl reload nginx
    
    访问测试

    Nginx核心配置详解

    3.8 自定义访问日志

    默认情况下,日志存放的路径是nginx安装路径的logs目录下, 但是我们可以将各个业务的日志剥离开,这样方便统计和管理
    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      error_page 404 /error.html;
      access_log /data/nginx/logs/pc.hechunping.tech/access.log;
      error_log /data/nginx/logs/pc.hechunping.tech/error.log;
      location = /error.html {
        root html;
      }
    }
    [root@CentOS7-01 ~]#systemctl reload nginx
    [root@CentOS7-01 ~]#ls /data/nginx/logs/pc.hechunping.tech/
    access.log  error.log

    3.9 检测文件是否存在

    try_files会按顺序检查⽂件是否存在,返回第⼀个找到的⽂件或⽂件夹(结尾加"/"表⽰为⽂件夹),如果所有⽂件或⽂件夹都找不到,会进⾏⼀个内部重定向到最后⼀个参数。只有最后⼀个参数可以引起⼀个内部重定向,之前的参数只设置内部URI的指向。最后⼀个参数是回退URI且必须存在,否则会出现内部500错误。
    
    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location /pc {
        root html;
        try_files $uri $uri/index.html $uri.html /pc/defautl.html;
      }
    }
    [root@CentOS7-01 ~]#cat /apps/nginx/html/pc/defautl.html 
    pc default page ...
    [root@CentOS7-01 ~]#systemctl reload nginx
    
    访问测试
    当访问的资源无法匹配前面的所有uri时,就会显示defautl.html页面的内容,如下图

    Nginx核心配置详解

    也可以将最后一个参数自定义为一个状态码,如下图

    Nginx核心配置详解

    3.10 长连接匹配

    keepalive_timeout number; #设定保持连接超时时⻓,0表⽰禁⽌⻓连接,默认为75s,通常配置在http字段作为站点全局配置 
    keepalive_requests number; #在⼀次⻓连接上所允许请求的资源的最⼤数量,默认为100次
    
    keepalive_timeout 65 65; #开启⻓连接后,返回客户端的会话保持时间为65s,单次⻓连接累计请求达到指定次数请求或65秒就会被断开,后⾯的65表示发送给客户端的响应报⽂头部中显⽰的超时时间设置为65s:如不设置客户端将不显⽰超时时间。
    
    访问测试
    [root@CentOS7-01 ~]#curl -I pc.hechunping.tech/pc
    HTTP/1.1 200 OK
    Server: nginx
    Date: Sat, 04 Jan 2020 07:43:31 GMT
    Content-Type: text/html; charset=utf-8
    Content-Length: 7
    Last-Modified: Thu, 02 Jan 2020 11:32:27 GMT
    Connection: keep-alive
    Keep-Alive: timeout=65
    ETag: "5e0dd4cb-7"
    Accept-Ranges: bytes
    
    如果设置为0表⽰关闭会话保持功能,如下所示
    curl -I pc.hechunping.tech/pc
    HTTP/1.1 200 OK
    Server: nginx
    Date: Sat, 04 Jan 2020 07:45:23 GMT
    Content-Type: text/html; charset=utf-8
    Content-Length: 7
    Last-Modified: Thu, 02 Jan 2020 11:32:27 GMT
    Connection: close
    ETag: "5e0dd4cb-7"
    Accept-Ranges: bytes
    
    keepalive_requests 2; #在一次长连接上所允许请求的资源的最大次数,这里设置为2,当达到两次就会关闭本次长连接。
    
    访问测试
    # 使用telnet命令测试
    [root@CentOS7-01 ~]#telnet pc.hechunping.tech 80
    Trying 127.0.0.1...
    Connected to pc.hechunping.tech.
    Escape character is '^]'.
    GET /pc/index.html HTTP/1.1
    HOST: pc.hechunping.tech
    
    # 响应头信息
    HTTP/1.1 200 OK
    Server: nginx
    Date: Sat, 04 Jan 2020 07:48:50 GMT
    Content-Type: text/html; charset=utf-8
    Content-Length: 7
    Last-Modified: Thu, 02 Jan 2020 11:32:27 GMT
    Connection: keep-alive
    Keep-Alive: timeout=65
    ETag: "5e0dd4cb-7"
    Accept-Ranges: bytes
    
    # 页面内容
    pc web
    GET /pc/index.html HTTP/1.1
    HOST: pc.hechunping.tech
    
    # 响应头信息
    HTTP/1.1 200 OK
    Server: nginx
    Date: Sat, 04 Jan 2020 07:49:01 GMT
    Content-Type: text/html; charset=utf-8
    Content-Length: 7
    Last-Modified: Thu, 02 Jan 2020 11:32:27 GMT
    Connection: close
    ETag: "5e0dd4cb-7"
    Accept-Ranges: bytes
    # 页面内容
    pc web
    Connection closed by foreign host. #请求资源的次数达到2次后,本次长连接关闭

    3.11 配置nginx为下载服务器

    [root@CentOS7-01 download]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location /download { 
        autoindex on;
        autoindex_exact_size on;
        autoindex_localtime on;
        limit_rate 20k;
        root html/pc;
      }
    }
    [root@CentOS7-01 download]#ls /apps/nginx/html/pc/download/ #该目录下不能有index.html文件
    README.md  ubuntu-18.04.3-server-amd64.iso
    [root@CentOS7-01 download]#systemctl reload nginx
    
    访问测试,如下图

    Nginx核心配置详解
    Nginx核心配置详解

    3.12 配置nginx为上传服务器

    client_max_body_size 1m; #设置允许客户端上传单个⽂件的最⼤值,默认值为1m
    client_body_buffer_size size; #⽤于接收每个客户端请求报⽂的body部分的缓冲区⼤⼩;默认16k;超出此⼤⼩时,其将被暂存到由下⾯的client_body_temp_path指令所定义的磁盘上的位置
    client_body_temp_path path [level1 [level2 [level3]]]; #设定存储客户端请求报⽂的body部分的临时存储路径及⼦⽬录结构和数量,⽬录名为16进制的数字,使⽤hash之后的值从后往前截取1位、2位、2位作为⽂件名
    
    1级⽬录占1位16进制,即2^4=16个⽬录 0-f
    2级⽬录占2位16进制,即2^8=256个⽬录 00-ff
    3级⽬录占2位16进制,即2^8=256个⽬录 00-ff
    
    配置⽰例:
    client_max_body_size 10m;
    client_body_buffer_size 16k;
    client_body_temp_path /data/nginx/temp 1 2 2; #reload Nginx会⾃动创建temp⽬录

    3.13 其它配置

    3.13.1 对哪种浏览器禁⽤⻓连接

    keepalive_disable none | browser ...;
    例如:禁用ie6浏览器,在配置文件中添加如下参数,可以在http,server,location块配置
    keepalive_disable msie6;

    3.13.2 限制客户端只能使用指定的请求方法,仅⽤于location块

    limit_except method ... { ... } ;
    
    method:GET,HEAD,POST,PUT,DELETE,MKCOL,COPY,MOVE,OPTIONS,PROPFIND,PROPPATCH,LOCK,UNLOCK,PATCH
    # 限制仅允许192.168.7.0网段的客户端使用指定的GET和HEAD方法
    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location /pc {
        root html;
        limit_except GET {
          allow 192.168.7.0/24;
          deny all;
        }
      }
    }
    [root@CentOS7-01 ~]#systemctl reload nginx
    [root@CentOS7-01 ~]#curl -XPUT /etc/issue pc.hechunping.tech/pc
    curl: (3) <url> malformed
    <html>
    <head><title>403 Forbidden</title></head> #nginx拒绝上传
    <body>
    <center><h2>403 Forbidden</h2></center>
    <hr><center>nginx</center>
    </body>
    </html>
    
    # 注释掉限制指定方法的配置
    [root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
    server {
      listen 80;
      server_name pc.hechunping.tech;
      location /pc {
        root html;
        #limit_except GET {
        #  allow 192.168.7.0/24;
        #  deny all;
        #}
      }
    }
    [root@CentOS7-01 ~]#systemctl reload nginx
    [root@CentOS7-01 ~]#curl -XPUT /etc/issue pc.hechunping.tech/pc
    curl: (3) <url> malformed
    <html>
    <head><title>405 Not Allowed</title></head> #nginx已经允许,但是程序未支持上传功能
    <body>
    <center><h2>405 Not Allowed</h2></center>
    <hr><center>nginx</center>
    </body>
    </html>

    3.13.3 是否启⽤asynchronous file I/O(AIO)功能,需要编译开启

    linux 2.6以上内核提供以下⼏个系统调⽤来⽀持aio:
    1、SYS_io_setup:建⽴aio的context
    2、SYS_io_submit: 提交I/O操作请求
    3、SYS_io_getevents:获取已完成的I/O事件
    4、SYS_io_cancel:取消I/O操作请求
    5、SYS_io_destroy:毁销aio的context

    3.13.4 启用直接I/O

    directio size | off; #操作完全和aio相反,aio是读取⽂件⽽directio是写⽂件到磁盘,启⽤直接I/O,默认为关闭,当⽂件⼤于等于给定⼤⼩时,例如directio 4m,同步(直接)写磁盘,⽽⾮写缓存。

    3.13.5 是否缓存打开过的文件信息

    open_file_cache off; #是否缓存打开过的⽂件信息
    open_file_cache max=N [inactive=time];
    nginx可以缓存以下三种信息:
    1) ⽂件元数据:⽂件的描述符、⽂件⼤⼩和最近⼀次的修改时间
    2) 打开的⽬录结构
    3) 没有找到的或者没有权限访问的⽂件的相关信息
    max=N:可缓存的缓存项上限数量;达到上限后会使⽤LRU(Least recently used,最近最少使⽤)算法实现管理
    inactive=time:缓存项的⾮活动时⻓,在此处指定的时⻓内未被命中的或命中的次数少于
    open_file_cache_min_uses指令所指定的次数的缓存项即为⾮活动项,将被删除

    3.13.6 open_file_cache指令的inactive参数指定的时⻓内,⾄少被命中此处指定的次数⽅可被归类为活动项

    open_file_cache_min_uses number; #默认值为1

    3.13.7 是否缓存查找时发⽣错误的⽂件⼀类的信息

    open_file_cache_errors on | off; #默认值为off

    3.13.8 缓存项有效性的检查验证频率

    open_file_cache_valid time; #默认值为60s
    open_file_cache max=10000 inactive=60s; #最⼤缓存10000个⽂件,⾮活动数据超时时⻓60s
    open_file_cache_valid 60s;  #每间隔60s检查⼀下缓存数据有效性
    open_file_cache_min_uses 5; #60秒内⾄少被命中访问5次才被标记为活动数据
    open_file_cache_errors on;  #缓存错误信息

    3.13.9 隐藏Nginx server版本。

    server_tokens off; #默认为on
    
    访问测试
    默认值时的结果
    [root@CentOS7-01 ~]#curl -I pc.hechunping.tech
    HTTP/1.1 200 OK
    Server: nginx/1.16.1
    ...
    
    设置为off后的结果,添加在http块的全局配置
    [root@CentOS7-01 ~]#curl -I pc.hechunping.tech
    HTTP/1.1 200 OK
    Server: nginx
  • 相关阅读:
    利用mybatis-generator自动生成代码
    gradle安装
    有关﹤![CDATA[ ]]> 说明
    mysql时间字段转换为毫秒格式
    string 与BigDecimal互转
    VLOOKUP多条件查找不使用辅助列
    BIEE-CSS样式大全
    VBA【遍历每个工作表并将工作表表名赋予B2单元格】
    深入理解公式{1,0}的用法
    DB2解锁
  • 原文地址:https://www.cnblogs.com/mscm/p/13257163.html
Copyright © 2011-2022 走看看