资讯专栏INFORMATION COLUMN

nginx的web-server的基本使用(二)

pf_miles / 1769人阅读

摘要:需要注意的是,通过模块中的和主模块的可以计算出,也就同时最大连接数指令对于上述的的最大连接数有限制作用这其中使用比较多的就是指令。消息能包含文本图像音频视频以及其他应用程序专用的数据。替换访问的具体路径仅在中使用,用来替换访问路径。

基本配置

可以说nginx的使用基本体现在了配置文件的指令上,而每个模块中的指令又对应了很多的不同的功能,所以本文旨在了解了之前的nginx的初始nginx的基本思想和功能的前提下,进一步的了解一些比较常用的模块和其中的指令;

主模块

事件模块

http相关模块

mail、stream相关模块

主要参考nginx的官方文档,nginx的指令集合字母排序,nginx的变量字母排序

主模块

主模块的指令主要包括以下这些,基本都是简单指令,不过每个指令的执行环境context是不一样的,有的可以在http、location、server等中都可以使用,主模块指令以下是主模块中的指令列表:

1.2 daemon
1.3 debug_points
1.4 error_log
1.5 include
1.6 master_process
1.7 pid
1.8 ssl_engine
1.9 timer_resolution
1.10 user
1.11 worker_cpu_affinity
1.12 worker_priority
1.13 worker_processes
1.14 worker_rlimit_core
1.15 worker_rlimit_nofile
1.16 worker_rlimit_sigpending
1.17 working_directory

daemon指令,守护进程QA,生产环境中不要使用"daemon"和"master_process"指令,这些选项仅用于开发调试[主要方便开发过程中的进程清除]。

语法: daemon on | off
缺省值: on
使用环境context: main主模块

debug_points指令,主要也是在开发过程来调试nginx

语法: debug_points [stop | abort]
缺省值: none

error_log 指令用于记录nginx的某些级别的异常错误日志

语法: error_log file [ debug | info | notice | warn | error | crit ]
缺省值: ${prefix}/logs/error.log

include 指令,主要是来引入其他配置文件,避免单一配置文件过大和复杂

语法: include file | *
缺省值: none

你可以在任意地方使用include指令实现配置文件的包含,类似于apache中的include方法,可减少主配置文件的篇幅。include 指令还支持像下面配置一样的全局包含的方法,例如包含一个目录下所有以".conf"结尾的文件:

include vhosts/*.conf;

pid 指令,进程id存储文件

语法: pid file
缺省值: compile-time option Example:
pid /var/log/nginx.pid;

user这个指令名代表的是执行worker processes的本机用户,默认是nobody,那么如果需要读写一些roort或者其他用户所有权的文件时,如果当前配置文件填写的user这个指令名对应的用户又不具有r+w+x的权限时,就会出现一些权限问题;

语法: user user [group]
缺省值: nobody nobody
指定Nginx Worker进程运行用户,默认是nobody帐号。

worker_processes这个指令名是指配置worker_processes的数量,nginx启动时会有一个主进程和若干的工作进程,这个指令就是来规定工作进程的数量的,对应的是一个数值,

nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker processes. Worker processes do actual processing of requests.  
使用: worker_processes number | auto;
默认:worker_processes 1;
使用环境context: main 主模块
如果nginx处理的是cpu密集型(比较耗费cpu的)的操作,建议将此值设置为cpu个数或cpu的核数。
一个cpu配置多于一个worker数,对nginx而言没有任何益处。

需要注意的是,通过event模块中的worker_connections和主模块的worker_proceses可以计算出maxclients,也就同时最大连接数:

max_clients = worker_processes * worker_connections

worker_rlimit_nofile指令,对于上述的的最大连接数有限制作用

Syntax:    worker_rlimit_nofile number;
Default: —
Context: main

Changes the limit on the maximum number of open files (RLIMIT_NOFILE) for worker processes. Used to increase the limit without restarting the main process.

这其中使用比较多的就是include指令。

事件模块

事件模块主要是设置Nginx处理连接请求;事件模块指令指令列表如下:

2.1 accept_mutex
2.2 accept_mutex_delay
2.3 debug_connection
2.4 devpoll_changes
2.5 devpoll_events
2.6 kqueue_changes
2.7 kqueue_events
2.8 epoll_events
2.9 multi_accept
2.10 rtsig_signo
2.11 rtsig_overflow_events
2.12 rtsig_overflow_test
2.13 rtsig_overflow_threshold
2.14 use
2.15 worker_connections

accept_mutex指令,nginx 使用连接互斥锁进行顺序的accept()系统调用.

Syntax:    accept_mutex on | off;
Default:
accept_mutex off;
Context: events
If accept_mutex is enabled, worker processes will accept new connections by turn. Otherwise, all worker processes will be notified about new connections, and if volume of new connections is low, some of the worker processes may just waste system resources.

这个指令以前默认是开的,Prior to version 1.11.3, the default value was on.

worker_connections

Syntax:    worker_connections number;
Default:
worker_connections 512;
Context: events

Sets the maximum number of simultaneous connections that can be opened by a worker process. 设置一个工作进程的同一时刻的最大连接数
It should be kept in mind that this number includes all connections (e.g. connections with proxied servers, among others), not only connections with clients. Another consideration is that the actual number of simultaneous connections cannot exceed the current limit on the maximum number of open files, which can be changed by worker_rlimit_nofile.

其他的之类需要参考官方文档来看: 主模块和事件模块的简单指令详细介绍:http://nginx.org/en/docs/ngx_core_module.html

http相关模块

http相关模块可以看作是nginx的核心模块,是其功能的最核心部分,所以http相关的模块有将近几十个,其中使用的最多的是http核心模块;

http模块的主要组成部分是server和一些公用提取出来指令【可以在http、server、server甚至是if内使用的指令,在server块级指令集中设置的话需要书写多次,所以可以作为公共提取到http块指令中】,而server的重要部分是location、listen、server_name等;

http核心模块 公用基础配置

大部分配置也可以写在server、http块级中,但是为了避免重复书写,可以考虑提取到外层;部分指令如下:

types 响应内容的文件类型,Maps file name extensions to MIME types of responses;MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准。MIME 消息能包含文本、图像、音频、视频以及其他应用程序专用的数据。

Syntax:    types { ... }
Default: types {
text/html html;
image/gif gif;
image/jpeg jpg;
}
Context: http, server, location
一个完整的映射表在conf/mime.types文件中,所以默认的写法是include mime.types;;默认类型还可以使用:To make a particular location emit the “application/octet-stream” MIME type for all requests, the following configuration can be used:
location /download/ {
       types        { }
    default_type application/octet-stream;
}

keepalive_timeout 客户端keeplive的链接无任何操作的保持最久时间

Syntax:    keepalive_timeout timeout [header_timeout];
Default: keepalive_timeout 75s;
Context: http, server, location
The first parameter sets a timeout during which a keep-alive client connection will stay open on the server side. The zero value disables keep-alive client connections. The optional second parameter sets a value in the “Keep-Alive: timeout=time” response header field. Two parameters may differ.

sendfile 指令是指是否开启linux2+的一个sendfile的功能,Enables or disables the use of sendfile()

error_page 为一些特定的错误返回定义好的一些URI路径, 详细介绍error_page

Syntax:    error_page code ... [=[response]] uri;
Default: —
Context: http, server, location, if in location

以下example:

error_page 404             /404.html;
error_page 500 502 503 504 /50x.html;
error_page 404 =200 /empty.gif;
error_page 404 = /404.php;
location / {
    error_page 404 = @fallback;
}
location @fallback {
proxy_pass http://backend;
}
error_page 403      http://example.com/forbidden.html;
error_page 404 =301 http://example.com/notfound.html;

server配置

listen 详细listen,用于监听服务器的某个端口的所有请求;

Syntax:  listen address[:port] [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];  
listen port [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
listen unix:path [default_server] [ssl] [http2 | spdy] [proxy_protocol] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
Default: listen :80 | :8000;
Context: server

一些例子:

listen 127.0.0.1:8000;
listen 127.0.0.1;
listen 8000;
listen *:8000;
listen localhost:8000;
//IPv6 addresses (0.7.36) are specified in square brackets:
listen [::]:8000;
listen [::1];

如果没有这个listen指令,那么需要根据启动用户是否是超级管理员用户,是的话将会启动监听80端口,否则使用8000端口;目前已经支持hhtp2 https的方式。

server_name server_name详情为每个服务设置上名称,在http指令中可以设置多个server,多个server可以监听同一个端口,那么端口接收到的请求就需要根据名字匹配来进行相应的服务中进行处理,如果都没有匹配上的话,就是第一个derver指令来处理。

Syntax:    server_name name ...;
Default: server_name "";
Context: server

可以使用全匹配、通配符、正则匹配,例子如下:

server {
    server_name example.com www.example.com;
}
//The first name becomes the primary server name.

//Server names can include an asterisk (“*”) replacing the first or last part of a name:

server {
    server_name example.com *.example.com www.example.*;
}
//Such names are called wildcard names.

//The first two of the names mentioned above can be combined in one:

server {
    server_name .example.com;
}
It is also possible to use regular expressions in server names, preceding the name with a tilde (“~”):

server {
    server_name www.example.com ~^wwwd+.example.com$;
}
Regular expressions can contain captures (0.7.40) that can later be used in other directives:

server {
    server_name ~^(www.)?(.+)$;

    location / {
        root /sites/$2;
    }
}

server {
    server_name _;

    location / {
        root /sites/default;
    }
}
Named captures in regular expressions create variables (0.8.25) that can later be used in other directives:

server {
    server_name ~^(www.)?(?.+)$;

    location / {
        root /sites/$domain;
    }
}

server {
    server_name _;

    location / {
        root /sites/default;
    }
}

server_name_in_redirect 在由nginx发布的绝对重定向中,启用或禁用使用由server_name指令指定的主服务器名称

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

在开启的时候,如果需要重定向,就可以使用已经开启过的server_name来书写,快速的定位;

server_name  cwj.cc;
server_name_in_redirect on;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location /test {
    proxy_pass   http://cwj.cc:8080; //此服务名开启了重定向,可以直接使用
}

port_in_redirect 和上面的指令差不多,只不过换成了端口号,默认开启

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

location 将解析后的URI请求进行匹配,匹配到后根据配置完成相应的处理和转发,该指令还可以嵌套使用;

Syntax:    location [ = | ~ | ~* | ^~ ] uri { ... }  
location @name { ... }
Default: —
Context: server, location

可以使用= 、^~、 ~|~*、@ 四种方法来配置;匹配顺序如下序列号:

= 使用等号匹配符表明两点:
例子如下:

location = /test{
    return "hello_world no1";
}
// 需要完全匹配,只有 127.0.0.1:port/test 可以正确匹配 ;连127.0.0.1:port/test/也无法匹配上

无法使用正则

必须完全匹配,优选级最高

空字符完全匹配,空字符匹配既可以完全匹配也可以匹配URI的前半部分【prefix string】;区别是:只有完全匹配的时候,匹配优先级才会高于正则匹配,而prefix string的
例子如下:

location /test/test.jpg{
    return "/test/test.jpg";
}
// 需要完全匹配优先级才高,只有 127.0.0.1:port//test/test.jpg 可以算完全匹配 ;
//连127.0.0.1:port/test.jgp.test 也可以匹配上,只是不算完全匹配

无法使用正则

必须完全匹配才高优先级的空字符匹配,也可以把该项当做前缀来匹配,具体看第5点;

完全匹配成功的话,优选级第二高,否则在正则之后

^~ 正则前缀匹配,表示匹配以正则匹配的内容开头的URI,
例子如下:

location ^~ /test{
    return "hello_world no3";
}
// 匹配 127.0.0.1:port/test + 其他后缀路径等;

可以使用正则

可以完全匹配或者部分匹配

~|~*
例子如下:

location ~ ^/users/(.+.(?:gif|jpe?g|png))$ {
    alias /data/w3/images/$1;
}
// 需要与正则表达式匹配,127.0.0.1:port/users/test.jpg 可以正确匹配 ;还可以在后续来使用这个匹配上的可捕获的子表达式

可以使用正则

匹配正则表达式,捕获匹配的字表达式可以在后续使用【主要是alias中】

空字符非精确
例子如下:

location = /test{
    return "hello_world no5";
}
// 可以完全和部分匹配,只有 127.0.0.1:port/test 才算是全匹配 ;127.0.0.1:port/test/也可以匹配上,就是非完全匹配,优先级最低

无法使用正则

非完全匹配的优先级最低

还有很多其他的,此处不一一例举。

location

alias 替换URI访问的具体路径

Syntax:    alias path;
Default: —
Context: location

仅在location中使用,用来替换访问路径。下面的例子就是一个路径的映射关系

location /i/ {
    alias /data/w3/images/;
}
//on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.

root 设置请求的根目录

Syntax:    root path;
Default: root html;
Context: http, server, location, if in location

使用后所有的请求的资源都将映射到设置的根目录下面:

location /i/ {
    root /data/w3;
}
//The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request.

etag 为请求设置etag响应头,和缓存先关的设置

Syntax:    etag on | off;
Default: etag on;
Context: http, server, location
This directive appeared in version 1.3.3.

Enables or disables automatic generation of the “ETag” response header field for static resources.

if_modified_since 替换URI访问的具体路径

Syntax:    if_modified_since off | exact | before;
Default: if_modified_since exact;
Context: http, server, location
This directive appeared in version 0.7.24.

off:
the “If-Modified-Since” request header field is ignored (0.7.34);

exact:
exact match;

before:
modification time of a response is less than or equal to the time in the “If-Modified-Since” request header field.

还有很多其他的,此处不一一例举。

为了避免篇幅过长,部分内容移到下一篇文章中

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39842.html

相关文章

  • nginxweb-server基本使用

    摘要:需要注意的是,通过模块中的和主模块的可以计算出,也就同时最大连接数指令对于上述的的最大连接数有限制作用这其中使用比较多的就是指令。消息能包含文本图像音频视频以及其他应用程序专用的数据。替换访问的具体路径仅在中使用,用来替换访问路径。 基本配置 可以说nginx的使用基本体现在了配置文件的指令上,而每个模块中的指令又对应了很多的不同的功能,所以本文旨在了解了之前的nginx的初始ngin...

    XanaHopper 评论0 收藏0
  • session一致性架构设计实践

    摘要:最常见的,会把用户的登录信息用户信息存储在中,以保持登录状态。什么是一致性问题只要用户不重启浏览器,每次短连接请求,理论上服务端都能定位到,保持会话。在高可用时,如何保证路由的一致性,是今天将要讨论的问题。 一、缘起 什么是session?服务器为每个用户创建一个会话,存储用户的相关信息,以便多次请求能够定位到同一个上下文。 Web开发中,web-server可以自动为同一个浏览器的访...

    freewolf 评论0 收藏0
  • session一致性架构设计实践

    摘要:最常见的,会把用户的登录信息用户信息存储在中,以保持登录状态。什么是一致性问题只要用户不重启浏览器,每次短连接请求,理论上服务端都能定位到,保持会话。在高可用时,如何保证路由的一致性,是今天将要讨论的问题。 一、缘起 什么是session?服务器为每个用户创建一个会话,存储用户的相关信息,以便多次请求能够定位到同一个上下文。 Web开发中,web-server可以自动为同一个浏览器的访...

    honhon 评论0 收藏0
  • nginxweb-server 多文件入口访问

    摘要:访问需求示例需要访问如下服务端目录结构问题我们习惯配置的服务为单入口,即多入口配置利用变量动态配置,实现多入口访问 访问需求示例 需要访问如下 url:localhost/info.phplocalhost/detail.php 服务端 server-root 目录结构: ➜ ~ tree public public ├── detail.php └── info.php 问题 我们...

    vslam 评论0 收藏0
  • nginxweb-server 多文件入口访问

    摘要:访问需求示例需要访问如下服务端目录结构问题我们习惯配置的服务为单入口,即多入口配置利用变量动态配置,实现多入口访问 访问需求示例 需要访问如下 url:localhost/info.phplocalhost/detail.php 服务端 server-root 目录结构: ➜ ~ tree public public ├── detail.php └── info.php 问题 我们...

    muddyway 评论0 收藏0

发表评论

0条评论

pf_miles

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<