Skip to content

Commit

Permalink
nginx
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuigo committed May 31, 2020
1 parent 9e26616 commit 509551d
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 200 deletions.
11 changes: 6 additions & 5 deletions post/c/ops-file-watch.md
Expand Up @@ -24,16 +24,17 @@ reload + detect

$ fswatch -r -o conf | xargs -n1 -I% sh -c 'echo event number%; sh nginx-reload.sh'
$ cat nginx-reload.sh
pid=$(cat ngx.pid);
if kill -HUP $pid; then
if ! { nginx -t -p `pwd`/ -c nginx.conf && nginx -s reload; } then
pid=$(cat ngx.pid);
if kill -HUP $pid; then
if ! { nginx -t -p $PWD -c nginx.conf && nginx -s reload -p $PWD -c nginx.conf; } then
echo "wrong config!"
kill $pid;
fi
else
echo "start nginx"
nginx -p `pwd`/ -c nginx.conf & echo $!>ngx.pid;
fi
nginx -p $PWD -c nginx.conf & echo $! > ngx.pid;
fi


help:

Expand Down
161 changes: 37 additions & 124 deletions post/nginx/nginx-debug-5xx.md
Expand Up @@ -4,7 +4,7 @@ date: 2019-04-11
private:
---
# Nginx
原文https://segmentfault.com/a/1190000016901812
参考原文https://segmentfault.com/a/1190000016901812

worker_processes 1;
events {
Expand Down Expand Up @@ -50,145 +50,75 @@ private:
### 400
NGX_HTTP_BAD_REQUEST

Host头不合法
Host头不合法

curl localhost:8070 -H 'Host:123/com'


<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>

Content-Length头重复
curl localhost:8070 -H 'Content-Length:1' -H 'Content-Length:2'
Content-Length头重复


<html>
curl localhost:8070 -H 'Content-Length:1' -H 'Content-Length:2'
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>

### 401
NGX_HTTP_UNAUTHORIZED

参考如上nginx配置,访问abc.html需要认证

curl localhost:8070/abc.html

curl localhost:8070/abc.html

<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
403
<head><title>401 Authorization Required</title></head>
### 403
NGX_HTTP_FORBIDDEN

chmod 222 index.html
chmod 222 index.html

将index.html设置为不可读

curl localhost:8070

curl localhost:8070
<head><title>403 Forbidden</title></head>

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
404
### 404
NGX_HTTP_NOT_FOUND

curl localhost:8070/cde.html

curl localhost:8070/cde.html
<head><title>404 Not Found</title></head>

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
405
### 405
NGX_HTTP_NOT_ALLOWED

使用非GET/POST/HEAD方法访问一个静态文件
curl -X DELETE localhost:8070/index.html -I


HTTP/1.1 405 Not Allowed
Server: nginx/1.14.0
Date: Tue, 18 Sep 2018 10:02:22 GMT
Content-Type: text/html
Content-Length: 173
Connection: keep-alive
curl -X DELETE localhost:8070/index.html -I
HTTP/1.1 405 Not Allowed

## 5xx系列
### 500
NGX_HTTP_INTERNAL_SERVER_ERROR

修改index.php为

<?php
echo "124"
缺少引号,语法错误
curl localhost:8070/index.php -I
<?php
echo "124"

缺少引号,语法错误

HTTP/1.1 500 Internal Server Error
Server: nginx/1.14.0
Date: Tue, 18 Sep 2018 11:29:19 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Set-Cookie: PHPSESSID=aoesvcuvbh1nh95kdkp152r9e1; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
curl localhost:8070/index.php -I
HTTP/1.1 500 Internal Server Error

### 501
NGX_HTTP_NOT_IMPLEMENTED

nginx的transfer-encoding现在只支持chunked,如果客户端随意设置这个值,会报501
比如:nginx的transfer-encoding现在只支持chunked,如果客户端随意设置这个值,会报501

curl localhost:8070 -H 'Transfer-Encoding:1'
<html>
<head><title>501 Not Implemented</title></head>
<body bgcolor="white">
<center><h1>501 Not Implemented</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
curl localhost:8070 -H 'Transfer-Encoding:1'

### 502
NGX_HTTP_BAD_GATEWAY

修改nginx配置为
fastcgi_pass 127.0.0.1:8000;
指向一个未监听的端口

curl localhost:8070/index.php -I


HTTP/1.1 502 Bad Gateway
Server: nginx/1.14.0
Date: Tue, 18 Sep 2018 11:28:17 GMT
Content-Type: text/html
Content-Length: 537
Connection: keep-alive
ETag: "5ad6113c-219"

fastcgi_pass 127.0.0.1:2300;

### 503
NGX_HTTP_SERVICE_UNAVAILABLE
Expand All @@ -198,15 +128,9 @@ NGX_HTTP_SERVICE_UNAVAILABLE
limit_req zone=one;

连续发送两个请求,第二请求会报503
curl localhost:8070/index.php -I

curl localhost:8070/index.php -I
HTTP/1.1 503 Service Temporarily Unavailable
Server: nginx/1.14.0
Date: Tue, 18 Sep 2018 11:31:43 GMT
Content-Type: text/html
Content-Length: 537
Connection: keep-alive
ETag: "5ad6113c-219"

### 504
NGX_HTTP_GATEWAY_TIME_OUT
Expand All @@ -215,36 +139,25 @@ NGX_HTTP_GATEWAY_TIME_OUT

<?php
echo "124";
sleep(5);
休息5秒钟
sleep(5); 休息5秒钟

修改nginx配置为
三秒钟读超时
fastcgi_read_timeout 3;
curl localhost:8070/index.php -I
HTTP/1.1 504 Gateway Time-out
Server: nginx/1.14.0
Date: Tue, 18 Sep 2018 12:17:57 GMT
Content-Type: text/html
Content-Length: 537
Connection: keep-alive
ETag: "5ad6113c-219"
505

#三秒钟读超时
fastcgi_read_timeout 3;

curl localhost:8070/index.php -I
HTTP/1.1 504 Gateway Time-out

### 505
NGX_HTTP_VERSION_NOT_SUPPORTED

telnet8070端口,输入GET /index.html HTTP/2.1
telnet 8070端口,输入`GET /index.html HTTP/2.1`
不支持http/2.1,会报505

$telnet localhost 8070
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /index.html HTTP/2.1
HTTP/1.1 505 HTTP Version Not Supported
Server: nginx/1.14.0
Date: Tue, 18 Sep 2018 12:26:35 GMT
Content-Type: text/html
Content-Length: 203
Connection: close
HTTP/1.1 505 HTTP Version Not Supported
6 changes: 5 additions & 1 deletion post/nginx/nginx-directive.md
Expand Up @@ -71,7 +71,7 @@ private: true

post-read、server-rewrite、find-config、rewrite、post-rewrite、preaccess、access、post-access、try-files、content 以及 log.

Nginx 的 rewrite 阶段的配置指令一般用来:
rewrite 阶段的配置指令一般用来:
1. 对当前请求进行各种修改(比如对 URI 和 URL 参数进行改写),
2. 或者创建并初始化 Nginx 变量。

Expand All @@ -82,6 +82,10 @@ Nginx 的 rewrite 阶段的配置指令一般用来:
content 阶段
1. 主要是输出内容, 例如 echo 指令的文档: `phase: content` 显示其指令在cotent 中执行

查看指令的执行顺序,可以加debug log, 然后执行script 时观察

tail -f errors.log | grep -E 'http script '

## rewrite 阶段
ngx_set_misc 模块的指令可与rewrite 放一起顺序执行

Expand Down

0 comments on commit 509551d

Please sign in to comment.