Nginx命令及配置
文章目录
命令
常用命令
1
|
nginx -s signal |
signal为下列中的一个
- stop — 关闭服务
- quit — 等待已经接受的请求处理完后关闭服务
- reload — 重新加载配置文件
- reopen — 重新打开日志文件
命令参数
-?|-h — 打印帮助信息及命令参数
-c file — 使用指定的配置文件
-g directives — 设置全局配置指令
1
nginx -g "pid /var/run/nginx.pid; worker_processes `sysctl -n hw.ncpu`;"
-p prefix — 设置nginx安装路径前缀(默认*/usr/local/nginx*)
-q — 在测试配置文件是阻止非错误信息
-s signal — 发送信号(指令)到祝处理进程(master process)
- stop
- quit
- reload
- reopen
-t — 测试配置文件:检查配置文件语法是否正确
-T — 功能同
-t
,并且转存配置文件到标准输出-v — 打印版本信息
-V — 打印版本信息、编译信息、配置参数
location指令URI匹配规则
定义方式
前缀字符串
1 2 3
location /documents/ { [ configuration C ] }
正则表达式
大小写不敏感
~*
1 2 3
location ~* \.(gif|jpg|jpeg)$ { [ configuration E ] }
大小敏感
~
1 2 3
location ~ \.(gif|jpg|jpeg)$ { [ configuration E ] }
命名方式
@
开头的为命名location,用于请求转发,不能嵌套,也不能包含嵌套的location。
#### 查找匹配
- 首先查找检查前缀字符串方式定义的
location
,选择最长匹配的定义并记住(暂存)- 如果最长匹配以
^~
开头修饰,则不执行2 - 如果字符串方式定义的
location
以=
开头修饰,表示为完全匹配。如果找到完全匹配,则终止继续查找并使用该完全匹配
- 如果最长匹配以
- 接着检查(按照在conf文件中定义的顺序)正则表达式方式定义的
location
,- 如果查找的第一个匹配的定义则停止继续查找并使用
- 如果在整个配置中没找到匹配的正则表达式,则使用1中记住的最长前缀匹配
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
location = / { [ configuration A ] } location / { [ configuration B ] } location /documents/ { [ configuration C ] } location ^~ /images/ { [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { [ configuration E ] } |
“
/
” 使用 configuration A, t“/index.html
” 使用 configuration B, “/documents/document.html
” 使用configuration C, “/images/1.gif
”使用 configuration D, “/documents/1.jpg
” 使用 configuration E.
静态内容服务
try选项
try_files
指令用于检查请求的文件或目录是否存在,如果不存在,将请求转向内部资源或一个特殊的状态码。
转向内部某个资源
1 2 3 4 5 6
server { root /www/data; location /images/ { try_files $uri /images/default.gif; } }
将被重定向到 /www/data/images/default.gif
返回状态码
1 2 3
location / { try_files $uri $uri/ $uri.html =404; }
通过一个特定名字的location转向一个代理服务
1 2 3 4 5 6 7
location / { try_files $uri $uri/ @backend; } location @backend { proxy_pass http://backend.example.com; }
性能优化
开启
sendfile
1 2 3 4 5
location /mp3 { sendfile on; sendfile_max_chunk 1m; #... }
开启
tcp_nopush
1 2 3 4 5
location /mp3 { sendfile on; tcp_nopush on; #... }
开启
tcp_nodelay
1 2 3 4 5
location /mp3 { tcp_nodelay on; keepalive_timeout 65; #... }
优化积压队列(backlog queue)
查看监听队列
1
|
netstat -Lan |
正常情况
1 2 3 |
Current listen queue sizes (qlen/incqlen/maxqlen) Listen Local Address 10/0/128 *.80 |
表示在80端口上有10个未被接受的连接,队列最大连接数为128
异常情况
1 2 3 |
Current listen queue sizes (qlen/incqlen/maxqlen) Listen Local Address 192/0/128 *.80 |
表示在80端口上有192个未被接受的连接,队列最大连接数为128.
- 调整操作系统参数
net.core.somaxconn
默认值:128,调整到4096
调整命令
FreeBSD
1
sudo sysctl kern.ipc.somaxconn=4096
Linux
命令方式
1
sudo sysctl -w net.core.somaxconn=4096
文件方式
/etc/sysctl.conf
1
net.core.somaxconn = 4096
调整Nginx参数
1 2 3 4
server { listen 80 backlog=4096; # ... }
反向代理
发送请求到代理服务器
1 2 3 4 5 6 |
location /some/path/ { proxy_pass http://www.example.com/link/; } location ~ \.php { proxy_pass http://127.0.0.1:8000; } |
当代理服务包含uri时,如上面的
/link/
。请求地址为/some/path/page.html
会被代理到http://www.example.com/link/page.html
发送请求头信息
默认情况,在被代理过的请求中,nginx重新定义了Host和Connection这个两个字段,并删掉了值为空字符串的字段,Host被设置到了$proxy_host
变量上,Connection被设置为close
。
通过proxy_set_header
指令修改其它的头信息字段
1 2 3 4 5 6 7 8 9 |
location /some/path/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://localhost:8000; } location /some/path/ { proxy_set_header Accept-Encoding ""; proxy_pass http://localhost:8000; } |
proxy_set_header
指令可用于location
或更高一级的context中(server
或http
)
配置缓冲区
1 2 3 4 5 6 7 8 9 10 |
location /some/path/ { proxy_buffers 16 4k; proxy_buffer_size 2k; proxy_pass http://localhost:8000; } location /some/path/ { proxy_buffering off; proxy_pass http://localhost:8000; } |
文章作者 binbin wen
上次更新 2019-08-02