nginx的安装和配置

zlib
pcre
openssl

内核参数的优化

/etc/sysctl.conf

fs.file.max=999999      #进程可以同时打开的最大句柄数,直接限制最大并发连接数
net.ipv4.tcp_tw_reuse=1     #为1 表示TIME-WAIT状态的socket重新用于新的TCP连接
net.ipv4.tcp_keepalive_time=600 #keepalive参数启用时,TCP发送keepalive的频度
net.ipv4.tcp_fin_timeout=30 #服务器主动关闭连接时,socket保持在FIN-WAIT-2状态的最大时间
net.ipv4.tcp_max_tw_buckets=5000    #操作系统允许TIME_WAIT套接字数量的最大值
tcp_max_syn_backlog #TCP三次握手阶段接收SYN请求队列的最大长度,默认1024
ip.ipv4.ip_local_port_range=1024 61000  #UDP和TCP连接中本地端口的取值范围
net.ipv4.tcp_rmem=4096 32768 262142 #TCP接收缓存的最小值、默认值、最大值
net.ipv4.tcp_wmem=4096 32768 262142 #TCP发送缓存的最小值、默认值、最大值
net.core.netdev_max_backlog=8096    #网卡接收包的速度大于内核处理的速度时,还有一个队列保存这些数据包。该参数表示该队列的最大值
net.core.rmem_default=262144    #内核套接字接收缓存区默认的大小
net.core.wmem_default=262144    #内核套接字发送缓冲区默认大小
net.core.rmem_max=2097152   #内核套接字接收缓冲区的最大大小
net.core.wmem_max=2097152   #内核套接字发送缓冲区的最大大小
net.ipv4.tcp_syncookies=1   #与性能无关,用于解决TCP的SYN攻击
net.ipv4.tcp_max_syn.backlog=1024

使用 sysctl -p 是配置生效

configure

./configure --help

路径相关的参数

编译相关的参数

依赖软件的相关参数

模块相关的参数

nginx 命令行控制

默认方式启动

/usr/local/nginx/sbin/nginx
会读取默认配置文件:/usr/local/nginx/conf/nginx.conf
或者在./configure 时指定的 –conf-path

另行指定配置文件

/usr/locl/nginx/sbin/nginx -c /tmp/nginx.conf

另行指定安装目录的启动方式

/usr/local/nginx/sbin/nginx -p /usr/local/nginx

另行指定全局配置项的启动方式

-g 参数
/usr/local/nginx/sbin/nginx -g “pid /var/nginx/test.pid”
不能与默认配置路径下的配置相冲突,否则无法启动
在输入其他命令时,同样需要输入-g 的参数

测试配置信息是否有错误

/usr/local/nginx/sbin/nginx -t

在测试配置阶段不输出信息

/usr/local/nginx/sbin/nginx -t -q

显示版本信息

/usr/local/nginx/sbin/nginx -v

显示编译阶段的参数

/usr/local/nginx/sbin/nginx -V

快速地停止服务

/usr/local/nginx/sbin/nginx -s stop

ps -ef | grep nginx
kill -s SIGTERM ???
kill -s SIGINT ???
kill -s SIGQUIT ??? #

“优雅”地停止服务

/usr/local/nginx/sbin/nginx -s quit
处理完成之后再退出
或者
kill -s SIGWINCH ??? #

运行中的nginx重读配置项并生效

/usr/local/nginx/sbin/nginx -s reload
kill -s SIGHUP ???

日志文件回滚

/usr/local/nginx/sbin/nginx -s reopen
kill -s SIGUSR1 ???

平滑升级nginx

  • 通知正在运行的就版本准备升级 kill -s SIGUSR2 ??? #pid会重新命名
  • 启动新版本 (新旧同时运行)
  • 以优雅的方式关闭旧的

    运行中nginx进程间的关系

    都是使用master进程管理多个worker进程,一般情况,与cpu核心数相等。通过共享内存,原子操作等实现负载均衡。

  • master进程不会对用户请求提供服务(一般需要较大的权限)

  • 多worker可以提高服务的健壮性,充分利用SMP

    nginx配置的通用语法

 user nobody;
 worker_processes 8;
 error_log /var/log/nginx/error.log error;
  # pid logs/nginx.pid
  events{
      use epoll;
      worker_connections 50000;
}

这里写图片描述

块配置项

如 events{

}

配置项的语法格式

配置项名 配置项值1 配置项值2 …;

配置项的注释

#

配置项的单位

由模块决定

在配置中使用变量

由模块决定 $

nginx服务的基本配置

  • 用于调试、定位问题的配置项
  • 正常运行的必备配置项
  • 优化性能的配置项
  • 事件类配置项
  • 默认的配置(如 daemon)

用于调试进程和定位问题的配置项

是否以守护进程方式运行nginx

daemon on | off;
默认 daemon on;

是否以master/worker方式工作

master_process on | off;
默认master_process on

error 日志的设置

error_log /path/file level;
error_log logs/error.log error;

debug info notice warn error crit alert emerg
如果日志级别设定为debug,必须在configure 时加入 –with-debug配置项

是否处理几个特殊的调试点

debug_points [stop | abort]

仅对指定的客户端输出debug级别的日志

debug_connection [IP | CIDR]

限制coredump核心转存文件的大小

worker_rlimit_core size;

指定coredump文件生成目录

working_directory path;

正常运行的配置项

定义环境变量

env VAR | VAR=VALUE

嵌入其他配置文件

include /path/file
如:
include mime.types
include vhost/*.conf

pid文件的路径

pid path/file

nginx worker进程运行的用户及用户组

user username [groupname]

指定nginx worker进程可以打开的最大句柄描述符的个数

worker_rlimit_nofile limit;

限制信号队列

worker_rlimit_sigpending limit;

优化性能的配置项

nginx worker 进程个数

worker_processes number;

绑定nginx worker进程到指定的CPU内核

worker_cpu_affinity cpumask [cpumask…] #仅对linux有效
如:

worker_proesses 4;
worker_cpu_affinity 1000 0100 0010 0001; 

SSL硬件加速

ssl_engine device
使用openssl 查看硬件加速设备
openssl engine -t

系统调用gettimeofday 的执行频率

timer_resolution t;
每次内核的事件调用返回时,都会执行一次gettimeofday

nginx worker 进程优先级设置

worker_priority nice;

事件类型配置项

是否打开accept锁

accept_mutex [on | off]
默认:accept_mutex on
有利于负载均衡

lock文件的路径

lock_file path/file
默认:lock_file logs/nginx.lock
accept锁可能需要这个lock文件,系统不支持原子锁时,用文件锁实现accept锁

使用accept锁后到真正建立连接之间的延迟时间

accept_mutex_delay Nms;
默认:accept_mutex_delay 500ms;
获取不到时,再次获取锁的时间间隔

批量建立新连接

multi_accept [on | off]
默认:multi_accept off;

选择事件模型

use [kqueue | rtsig | epoll | /dev/poll | select | poll | eventport];
默认自动使用最适合的事件模型

每个worker的最大连接数

worker_connections number;
定义每个worker进程可以同时处理的最大连接数

用http核心模块配置一个静态Web服务器

ngx_http_core_module
这里写图片描述
有些块的配置可以任意的出现在某一块中

虚拟主机与请求的分发

每个server块就是一个虚拟主机

监听端口

listen address:port [default(deprecated in 0.8.21) | default_server | [backlog=num | rcvbuf=size | sndbuf=size | accept_filter=filter | deferred | bind | ipv6only=[on | off] | ssl ]];
默认:listen 80;
配置块:server

如:
listen 127.0.0.18000listen 127.0.0.1;#默认端口
listen 8000listen *:8000listen localhost:8000listen [::]:8000;
listen [fe80::1];
listen [:::a8c9:1234]:80;

listen 443 default_server ssl;
listen 127.0.0.1 default_server accept_filter=dataready backlog=1024
  • default:所在server块作为整个Web的默认server块,没有设则为第一个server块,当无法匹配配置文件中的所有主机域名时,就会选用默认的虚拟主机
  • default_server:同上
  • backlog=num:表示TCP中backlog队列的大小,默认-1,表示不予设置。
  • rcvbuf=size;设置监听句柄的SO_RCVBUF参数
  • sndbuf=size;设置监听句柄的SO_SNDBUF参数
  • accept_filter:设置accept过滤器,只对FreeBSD操作系统有用
  • deferred:设置了该参数,在完成了三次握手后,内核不会为此连接调度worker,直到用户真的发送请求数据
  • bind:绑定当前端口/地址对,如127.0.0.1:8000。只有同时对一个端口监听多个地址时才会生效。
  • ssl:在当前监听的端口上建立的连接必须基于SSL协议

主机名称

server_name name […];
默认块:server_name “”;
server_name后面可以跟多个主机名称。
处理http请求时,从header头取出Host,与server_name进行匹配。
如果匹配多个时,根据优先级处理:
- 完全匹配
- 通配符在前面的
- 通配符在后面的
- 正则表达式匹配的
- 如果都不匹配,优先找 有default | default_server 的块,然后找匹配listen端口的第一个

server_names_hash_bucket_size

server_names_hash_bucket_size size;
默认:server_names_hash_bucket_size 32 | 64 |128
配置块:http、server、location

server_names_hash_max_size

server_names_hash_max_size size;
默认:server_names_hash_max_size 512;
配置块:http、server、location
越大,散列key的冲突率会降低

重定向主机名称的处理

server_name_in_redirect on | off
默认:server_name_in_redirect on ;
配置块:http、server、location

location

location [= | ~ | ~* | ^~ | @ ]/uri/{…}
配置块:server

= 表示作为字符串,以便完全匹配
~ 字母大小写是敏感的
~* 忽略字母大小写
^~ 前半部分与uri参数匹配
@ 仅用于nginx服务内部请求之间的重定向

多个匹配时,使用第一个
如果要 不匹配 ,可以使用最后一个,location /

文件路径的定义

以root方式设置资源路径

root path;
默认:root html;
配置块:http、server、location、if

以alias方式设置资源路径

alias path;
配置块:location
在使用 alias配置时会把 location 后面匹配的丢掉

访问首页

index file …;
默认:index idex.html;
配置块:http、server、location
使用的是ngx_http_index_module

根据http返回码重定向页面

error_page code [code …] [= | =answer-code] uri | @named_location
配置块:http、server、location、if

error_page 404 =200 /empty.gif #更改错误码
error_page 404 = /empty.gif #使用重定向后的处理结果

如果只想重定向

location /{
    error_page 404 @fallback;
}
location @fallback{
    proxy_pass http://backend;
}

是否允许递归使用error_page

recursive_error_pages [on | off];
默认:recursive_error_pages off;
配置块: http、server、location

try_files

try_files path1 [path2] uri;
配置块:server、location

内存及磁盘资源的分配

http包体只存储到磁盘文件中

client_body_in_file_only on | clean | off;
默认:client_body_in_file_only off;
配置块:http、server、location

http包体尽量写入到一个内存buffer中

client_body_in_single_buffer on | off;
默认:client_body_in_single_buffer off;
配置块:http、server、location

存储http头部的内存buffer大小

client_header_buffer_size size;
默认:client_header_buffer_size 1K;
配置块:http、server

存储超大http头部的内存buffer大小

large_client_header_buffers number size;
默认:large_client_header_buffers 4 8k;

存储http包体的内存buffer大小

client_body_buffer_size size;
默认:client_body_buffer_size 8k/16k;
配置块:http、server、location

http包体的临时存放目录

client_body_temp_path dir-path [level1 [level2 [level3 ]]]
默认:client_body_temp_path client_body_temp
配置块:http、server、location

connection_pool_size

connection_pool_size size;
默认:connection_pool_size 256;
配置块:http、server

request_pool_size

request_pool_size size;
默认:request_pool_size 4k;
配置块: http、server

网络连接的设置

读取http头部的超时时间

client_header_timeout time(秒);
默认:client_header_timeout 60;
配置块:http、server、location

读取http包体的超时时间

client_body_timeout time(秒);
默认:client_body_timeout 60;
配置块:http、server、location

发送响应的超时时间

send_timeout time;
默认:send_timeout 60;
配置块:http、server、location

reset_timeout_connection

reset_timeout_connection on | off;
默认:reset_timeout_connection off;
配置块:http、server、location
服务端直接释放连接

lingering_close

lingering_close off | on |always;
默认:lingering_close on;
配置块:http、server、location

lingering_time

lingering_time time;
默认:lingering_time 30s;
配置块:http、server、location

lingering_timeout

lingering_timeout time;
默认:lingering_timeout 5s;
配置块:http、server、location

对某些浏览器禁用keepalive功能

keepalive_disable [msie6 | safari | none]…
默认:keepalive_disable msie6 safari
配置块:http、server、location

keepalive超时时间

keepalive_timeout time(秒);
默认:keepalive_timeout 75;
配置块:http、server、location

一个keepalive长连接上允许承载的请求最大数

keepalive_requests n;
默认:keepalive_requests 100;
配置块:http、server、location

tcp_nodelay

tcp_nodelay on | off;
默认:tcp_nodelay on;
配置块:http、server、location

tcp_nopush

tcp_nopush on | off;
默认:tcp_nopush off;
配置块:http、server、location

MIME类型的设置

MIME type与文件扩展的映射

type{…};
配置块:http、server、location

默认 MIME type

default_type MIME-type;
默认:default_type text、plain;
配置块:http、server、location

types_hash_bucket_size

types_hash_bucket_size size;
默认:types_hash_bucket_size 32 | 64 | 128
配置块:http、server、location

types_hash_max_size

types_hash_max_size 1024;
配置块:http、server、location

对客户端请求的限制

http方法名限制用户请求

limit_except method …{…}
配置块:location

http请求包体的最大值

client_max_body_size size;
默认:client_max_body_size 1m;
配置块:http、server、location

对请求的限速

limit_rate speed;
默认:limit_rate 0;
配置块:http、server、location、if

limit_rate_after

limit_rate_after time;
默认:limit_rate_after 1m;
配置块:http、server、location、if

文件操作的优化

sendfile系统调用

sendfile on | off;
默认:sendfile off;
配置块:http、server、location

AIO系统调用

aio on | off;
默认:aio off;
配置块:http、server、location

directio

directio size | off;
默认:directio off;
配置块:http、server、location

directio_alignment

directio_alignment size;
默认:directio_alignment 512;
配置块:http、server、location

打开文件缓存

open_file_cache max=N [inactive=time] | off;
默认:open_file_cache off;
配置块:http、server、location

是否缓存打开文件错误的信息

open_file_cache_errors on | off;
默认:open_file_cache_errors off
配置块:http、server、location

不被淘汰的最小访问次数

open_file_cache_min_uses numbers;
默认:open_file_cache_min_uses 1;
配置块:http、server、location

检验缓存中元素有效性的频率

open_file_cache_valid time;
默认:open_file_cache_valid 60s;
配置块:http、server、location

对客户端请求的特殊处理

忽略不合法的http头部

ignore_invalid_headers on | off;
默认:ignore_invalid_headers on;
配置块:http、server

http头部是否允许下划线

underscores_in_headers on | off;
默认:underscores_in_headers off;
配置块:http、server

对If-modified-since头部的处理策略

if_modified_since [off | exact | before];
默认:if_modified_since exact;
配置块:http、server、location

文件未找到是是否记录到error日志

log_not_found on | off;
默认:log_not_found on;
配置块:http、server、location

merge_slashes

merge_slashes on | off;
默认:merge_slashes on;
配置块:http、server、location

DNS解析地址

resolver address …;
配置块:http、server、location

DNS解析的超时时间;

resolver_timeout time;
默认:resolver_timeout 30s;
配置块:http、server、location

返回错误页面时是否在Server 中注明nginx版本

server_tokens on | off;
默认:server_tokens on;
配置块:http、server、location

ngx_http_core_module模块提供的变量

用http proxy module配置一个反向代理服务器

反向代理方式是指用代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络中的上游服务器,并将从上游服务器上得到的结果返回给Internet上请求连接的客户端

负载均衡的基本配置

upstream块

upstream name{…}
配置块:http
定义了一个上游服务器的集群,便于反向代理中的proxy_pass使用

upstream backend{
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com;        
}

server{
    location /{
        proxy_pass http://backend;
    }
}

server

server name [parameters];
配置块:upstream
可以跟下列参数:

  • weight=number 转发的权重
  • max_fails=number
  • fail_timeout=time
  • down 所在的上游服务器永久下线
  • backup 表示备份

ip_hash

ip_hash;
配置块:upstream

记录日志时支持的变量

反向代理的基本配置

proxy_pass

proxy_pass URL;
配置块:location、if

* 默认情况下反向代理是不会转发请求中的Host头部的。如果需要转发,必须加上配置 proxy_set_header Host $host;*

proxy_method

proxy_method method;
配置块:http、server、location

proxy_hide_header

proxy_hide_header the_header;
配置块:http、server、location
上游到代理

proxy_pass_header

proxy_pass_header the_header;
配置块:http、server、location

proxy_pass_request_body

proxy_pass_request_body on | off;
默认:proxy_pass_request_body on;
配置块:http、server、location

proxy_pass_request_headers

proxy_pass_request_headers on | off;
默认:proxy_pass_request_headers on;
配置块:http、server、location

proxy_redirect

proxy_redirect [default | off | redirect repacement];
默认:proxy_redirect default;
配置块:http、server、location

proxy_next_upstream

proxy_next_upstream [error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_404 | off];
默认:proxy_next_upstream error timeout;
配置块:http、server、location

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值