• nginx配置


      已经接触nginx有相当一段时间,对nginx的各个模块配置都有一定的了解,但从未具体的进行整理,每次用时都需要去查找相关资料,因此开此篇进行具体汇总下nginx的相关使用配置以及遇到的问题

      一、nginx配置文件简介:

      nginx配置文件主要分为5个模块:1、全局块,2、event块,3、http块,4、server快,5、location块。如下所示:

    ;全局块
    events {
        ...
    }
    
    http {
        server {
            location {
                 .... 
            }
             location {
                 ....
            }
        }
    
         server {
            location {
                 ....
            }
             location {
                 ....
            }
        }
    }
        
    

     1、1:全局块相关配置:

             配置运行nginx的用户,只有被设置的用户和用户组成员才可以操作nginx,nginx默认用户和用户组为nobody(全部用户都可以操作)。配置信息如下

             user       nginx

             group     nginx

            配置nginx的worker_process数:

            worker_processes    number|auto;      //number nginx可最多产生的worker_process数   auto,nginx自动检测

            配置nginx的pid存放路径:

            pid  file;         //file pid存放路径    

            配置错误日志的路径:

            error_log    file| stderr[ debug | info | notice | warn | error | crit | alert | emerg ];   //错误日志路径 

            error_log  /tmp/logs/nginx_error.log error

    1、2:events配置:

            设置是否可以同时接收多个网络连接

            multi_accept on|off

            设置事件驱动模型的选择

            use method | [select, poll, epoll, kqueue,eventport等]

       配置最大链接数

            worker_connections number //每个worker process同时开启的最大连接数

    1、3:http配置:

            定义MIME-Type

         include mime-types;

            default_type application/octet-stream;

            配置access_log以及log_format

            access_log path   //path访问日志路径

            log_format name  string  //string具体查看nginx示例

            配置sendfile方式传输文件

            sendfile on | off

            sendfile_max_chunk size;   //设置每次传输的数据的大小,不设置则表示无限制 , 例:sendfile_max_chunk_size 128k

         配置超时时间

            keepalive_timeout timeout

            keepalive_requests number //单连接请求上线,默认100

    1.4 :server配置:

            配置网络监听:

         此处通过事例描述,如下:

            listen  ip:port;    监听具体的ip和具体的端口

            listen   ip;    监听具体的ip

            listen   port;       监听端口上的所有ip,等同于listen *:8000

            虚拟主机配置:

            server_name   name name2 //name可使用正则表达式,匹配优先级:①准确匹配 ②通配符在开始时匹配 ③通配符在结尾时匹配 ④正则表达式匹配成功    注:从nginx-0.0.40开始支持正则表达式字符串捕获功能。例如:server_name ~^www.(.+).com$;  ()中的将被捕获并记录到$1中,在server中可使用$1来表示捕获到的数据

            server_name ip;   //server_name支持基于ip配置虚拟主机

            root  path;   //path,根目录

    1.5 :location配置:

            location [ = | ~ | ~* | ^~ ] url {...}

            url表示待匹配的字符串,可以包含正则表达式,不包含正则表达式的意味标准url

            []中的表示可选项;详解如下:

            = ,用于标准url前,要求字符串与url严格匹配

            ~ ,用于表示url包含正则表达式,并且区分大小写

            ~*,用于表示url包含正则表达式,并且不区分大小写

            ^~,用于标砖url前,要求服务器找到的url和请求字符串的匹配度最高的location后,立即使用此location处理,而不再使用location快中的正则url和请求字符串做匹配

            例如:   

    首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
    例子,有如下匹配规则:
    location = / {
       #规则A
    }
    location = /login {
       #规则B
    }
    location ^~ /static/ {
       #规则C
    }
    location ~ .(gif|jpg|png|js|css)$ {
       #规则D
    }
    location ~* .png$ {
       #规则E
    }
    location !~ .xhtml$ {
       #规则F
    }
    location !~* .xhtml$ {
       #规则G
    }
    location / {
       #规则H
    }
    那么产生的效果如下:
    访问根目录/, 比如http://localhost/ 将匹配规则A
    访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
    访问 http://localhost/static/a.html 将匹配规则C
    访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到规则C
    访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。
    访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
    访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。

            设置网站的默认首页

            index index.$1.html  index.html  index.php;

            设置网站的错误页面

            error_page code [response] url      //code错误码   response 重定义的错误码   url,错误页面的路径和网站地址

  • 相关阅读:
    借用构造函数实现继承
    原型链
    创建对象 之 组合使用构造函数模式和原型模式
    6.原型对象的问题
    Spring MVC
    AOP
    谈谈对Spring IOC的理解
    Mybatis3.x与Spring4.x整合(转)
    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)
    Appweb写法
  • 原文地址:https://www.cnblogs.com/tangchuanyang/p/5916307.html
Copyright © 2020-2023  润新知