Nginx:修订间差异
来自康健生活
| (未显示同一用户的28个中间版本) | |||
| 第1行: | 第1行: | ||
[[Linux|返回上一级]] | [[Linux|返回上一级]] | ||
== [http://nginx.org/ Nginx] == | == [http://nginx.org/ Nginx] == | ||
* [[ | * [[Nginx/配置|配置]] | ||
== 安装 == | |||
=== windows === | |||
==== WAMP ==== | |||
==== source ==== | |||
在 [http://nginx.org/en/download.html nginx] 下载 windows 版本的 nginx,将其解压到 windows 系统的 c 目录下。 | |||
命令的操作方式。 | |||
{| class="wikitable" | |||
|- | |||
| start || {{shc|<syntaxhighlight lang="bash"> | |||
cd c:\nginx-1.18.0 | |||
start nginx</syntaxhighlight>|PS C:\nginx-1.18.0>}} | |||
|- | |||
| stop || <code>nginx -s stop</code> 即停止nginx,不保存相关信息 | |||
|- | |||
| quit || <code>nginx -s quit</code> 正常退出nginx,并保存相关信息 | |||
|- | |||
| reload || {{shc|<syntaxhighlight lang="bash"> | |||
./nginx -s reload</syntaxhighlight>|PS C:\nginx-1.18.0>}} | |||
|- | |||
| t || {{shc|<syntaxhighlight lang="bash"> | |||
./nginx -t -c '.\conf\nginx.conf'</syntaxhighlight>|PS C:\nginx-1.18.0>}} | |||
|} | |||
可以使用 tasklist 命令查看任务进程是否存在。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
tasklist /fi "imagename eq nginx.exe"</syntaxhighlight>|PS C:\nginx-1.18.0>}} | |||
=== mac === | |||
==== MAMP ==== | |||
==== brew ==== | |||
通过 brew 命令搜索 nginx 应用。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
brew search nginx</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
==> Formulae | |||
nginx | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
找到应用后进行安装 nginx。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
brew install nginx</syntaxhighlight>|$|/home/kangkk}} | |||
验证 nginx 安装是否成功。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
nginx -v</syntaxhighlight>|<syntaxhighlight lang="bash">nginx version: nginx/1.21.5 | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
命令的操作方式。 | |||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| | | start || {{shc|<syntaxhighlight lang="bash"> | ||
# 方式一 | |||
nginx | |||
# 方式二 | |||
sudo /usr/local/Cellar/nginx/1.21.5/bin/nginx | |||
# 方式三 | |||
brew services start nginx | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
|- | |||
| reload || {{shc|<syntaxhighlight lang="bash"> | |||
sudo /usr/local/Cellar/nginx/1.21.5/bin/nginx -s reload | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
|- | |||
| stop || {{shc|<syntaxhighlight lang="bash"> | |||
# 关闭 | |||
sudo /usr/local/Cellar/nginx/1.21.5/bin/nginx -s stop | |||
# down 掉进程 | |||
ps -ef|grep nginx | |||
# 正常杀死进程 | |||
sudo kill -QUIT 主进程号 | |||
# 快死杀死进程 | |||
sudo kill -TERM 主进程号 | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
|- | |- | ||
| | | info || {{Shc_out|<syntaxhighlight lang="bash"> | ||
brew services info nginx</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
nginx (homebrew.mxcl.nginx) | |||
Running: ✘ | |||
Loaded: ✘ | |||
Schedulable: ✘ | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
|- | |- | ||
| | | t || {{shc|<syntaxhighlight lang="bash"> | ||
sudo /usr/local/Cellar/nginx/1.21.5/bin/nginx -t | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
|} | |} | ||
== 配置 == | |||
==== 配置 ==== | |||
相关目录所在。 | |||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| | | 配置目录 || <code>/usr/local/etc/nginx/nginx.conf</code> | ||
|- | |||
| 文件目录 || <code>/usr/local/var</code> | |||
|- | |||
| 安装目录 || <code>/usr/local/Cellar/nginx/</code> | |||
|- | |- | ||
| | | 执行文件 || <code>/usr/local/etc/nginx/servers</code> | ||
|} | |||
=== linux === | |||
命令的操作方式。 | |||
{| class="wikitable" | |||
|- | |- | ||
| | | t || {{Shc_out|<syntaxhighlight lang="bash"> | ||
nginx -t</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
如果想要验证指定文件。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
nginx -t -c /usr/nginx/conf/nginx.conf</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
|} | |} | ||
== | |||
== 服务 == | |||
=== 进程 === | |||
查看进程。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<syntaxhighlight lang="bash"> | ||
nginx | ps -ef | grep nginx</syntaxhighlight>|<syntaxhighlight lang="bash"> | ||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
杀死进程。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
kill -TERM 进程号 | |||
# or | |||
kill -INT 进程号 | |||
# or | |||
kill -QUIT 进程号 | |||
# or | |||
pkill -9 nginx</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
使用 nginx 进程号进行重启。 | 使用 nginx 进程号进行重启。 | ||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<syntaxhighlight lang="bash"> | ||
kill -HUP 进程号</syntaxhighlight>|<syntaxhighlight lang="bash"> | kill -HUP 进程号</syntaxhighlight>|<syntaxhighlight lang="bash"> | ||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
== 参数 == | |||
{| class="wikitable" | |||
|- | |||
| permanent | |||
|- | |||
| redirect | |||
|} | |||
=== other === | |||
{| class="wikitable" | |||
|- | |||
| [[/favicon.ico|favicon.ico]] || 示例 || 示例 | |||
|} | |||
== 变量 == | |||
<pre> | |||
$args 请求头中的参数 | |||
$content_length 请求头中的 Content-length 字段 | |||
$content_type 请求头中的 Content-Type 字段 | |||
$document_root 当前请求在 root 指令中指定的值 | |||
$host 请求头中的 Host 头部,缺省值为服务器名称 | |||
$http_user_agent 客户端 agent 信息 | |||
$http_cookie 客户端 cookie 信息 | |||
$limit_rate 限制连接速率 | |||
$request_method 请求的方法 | |||
$remote_addr 客户端的 IP 地址。 | |||
$remote_port 客户端的端口。 | |||
$remote_user 已经经过 Auth Basic Module 验证的用户名 | |||
$request_filename 当前请求的文件路径,由 root 或 alias 指令与 URI 请求生成 | |||
$scheme HTTP 方法,eq: http、https | |||
$server_protocol 请求的协议,eq: HTTP/1.0、HTTP/1.1 | |||
$server_addr 服务器地址 | |||
$server_name 服务器名称 | |||
$server_port 服务器的端口 | |||
$request_uri 包含请求参数的 URI,eq: '/path/index.html?user=kangkk' | |||
$uri 不包含请求参数的 URI,eq: '/path/index.html' | |||
$document_uri 与 $uri 相同</pre> | |||
== location == | |||
=== if === | |||
<blockquote><pre> | |||
if (表达式) { | |||
# TODO | |||
}</pre></blockquote> | |||
表达式内部可以使用的命令: | |||
* -f、!-f: 判断是否存在文件 | |||
* -d、!-d: 判断是否存在目录 | |||
* -e、!-e: 判断是否存在文件或目录 | |||
* -x、!-x: 判断文件是否可执行 | |||
=== location === | |||
<blockquote><pre> | |||
location [=|~|~*|^~] /uri/ { | |||
# TODO | |||
}</pre></blockquote> | |||
* <code>=</code> 开头表示精确匹配 | |||
* <code>^~</code> 开头表示uri以某个常规字符串开头 | |||
* <code>~</code> 开头表示区分大小写的正则匹配 | |||
* <code>~*</code> 开头表示不区分大小写的正则匹配 | |||
* <code>!~</code>、<code>!~*</code> 分别为区分大小写不匹配、不区分大小写不匹配的正则 | |||
* <code>/</code> 通用匹配 | |||
== 日志 == | |||
=== 重新打开日志 === | |||
{{shc|<syntaxhighlight lang="bash"> | |||
nginx -s reopen</syntaxhighlight>|$|/home/kangkk}} | |||
== ssl_dhparam == | == ssl_dhparam == | ||
== | == 重定向 == | ||
=== rewrite === | |||
通过使用 <code>if</code> 判断 host 来决策是否进行重定向。 | |||
<syntaxhighlight lang="nginx"> | |||
# 如果 host 不是<code>既定域名</code>则重定向至<code>既定域名</code> | |||
if ($host !~ (kangkk.com)$){ | |||
rewrite ^ https://kangkk.com$request_uri?; | |||
} | |||
# 如果 host 是 ip 格式则重定向至<code>既定域名</code> | |||
if ($host ~ 192.168.1.1){ | |||
rewrite ^ https://kangkk.com$request_uri?; | |||
} | |||
</syntaxhighlight> | |||
=== rewrite 和 proxy_pass === | |||
路径重写,去除 <code>/api/</code> 前缀,并代理到新的服务器地址。 | |||
<pre> | <pre> | ||
location /api/ { | location /api/ { | ||
| 第40行: | 第248行: | ||
}</pre> | }</pre> | ||
== | == 转发 == | ||
=== 端口转发 === | |||
简单的端口转发。 | |||
{{hc|head=nginx.conf|output=<syntaxhighlight lang="nginx"> | |||
upstream quser_pool{ | |||
server 127.0.0.1:3100; | |||
} | |||
server { | |||
listen 80; | |||
server_name kangkk.com; | |||
error_log /var/log/nginx/quser_pool.error.log; | |||
access_log /var/log/nginx/quser_pool.access.log; | |||
# 转发至均衡池 | |||
location / { | |||
proxy_pass http://quser_pool; | |||
proxy_set_header Host $host; | |||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |||
proxy_set_header X-Real-IP $remote_addr; | |||
} | |||
} | |||
</syntaxhighlight>}} | |||
== socket == | |||
== other == | |||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| [[/ | | [[/同源策略|同源策略]] || 示例 || 示例 | ||
|- | |- | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
2025年4月14日 (一) 07:37的最新版本
安装
windows
WAMP
source
在 nginx 下载 windows 版本的 nginx,将其解压到 windows 系统的 c 目录下。
命令的操作方式。
| start | cd c:\nginx-1.18.0
start nginx
|
| stop | nginx -s stop 即停止nginx,不保存相关信息
|
| quit | nginx -s quit 正常退出nginx,并保存相关信息
|
| reload | ./nginx -s reload
|
| t | ./nginx -t -c '.\conf\nginx.conf'
|
可以使用 tasklist 命令查看任务进程是否存在。
tasklist /fi "imagename eq nginx.exe"
mac
MAMP
brew
通过 brew 命令搜索 nginx 应用。
[jian@kang /home/kangkk]$
brew search nginx
==> Formulae
nginx
找到应用后进行安装 nginx。
/home/kangkk
brew install nginx
验证 nginx 安装是否成功。
[jian@kang /home/kangkk]$
nginx -v
nginx version: nginx/1.21.5
命令的操作方式。
| start | /home/kangkk # 方式一
nginx
# 方式二
sudo /usr/local/Cellar/nginx/1.21.5/bin/nginx
# 方式三
brew services start nginx
|
| reload | /home/kangkk sudo /usr/local/Cellar/nginx/1.21.5/bin/nginx -s reload
|
| stop | /home/kangkk # 关闭
sudo /usr/local/Cellar/nginx/1.21.5/bin/nginx -s stop
# down 掉进程
ps -ef|grep nginx
# 正常杀死进程
sudo kill -QUIT 主进程号
# 快死杀死进程
sudo kill -TERM 主进程号
|
| info | [jian@kang /home/kangkk]$ brew services info nginx
nginx (homebrew.mxcl.nginx)
Running: ✘
Loaded: ✘
Schedulable: ✘
|
| t | /home/kangkk sudo /usr/local/Cellar/nginx/1.21.5/bin/nginx -t
|
配置
相关目录所在。
| 配置目录 | /usr/local/etc/nginx/nginx.conf
|
| 文件目录 | /usr/local/var
|
| 安装目录 | /usr/local/Cellar/nginx/
|
| 执行文件 | /usr/local/etc/nginx/servers
|
linux
命令的操作方式。
| t | [jian@kang /home/kangkk]$ nginx -t
如果想要验证指定文件。 [jian@kang /home/kangkk]$ nginx -t -c /usr/nginx/conf/nginx.conf
|
服务
进程
查看进程。
[jian@kang /home/kangkk]$
ps -ef | grep nginx
杀死进程。
[jian@kang /home/kangkk]$
kill -TERM 进程号
# or
kill -INT 进程号
# or
kill -QUIT 进程号
# or
pkill -9 nginx
使用 nginx 进程号进行重启。
[jian@kang /home/kangkk]$
kill -HUP 进程号
参数
| permanent |
| redirect |
other
| favicon.ico | 示例 | 示例 |
变量
$args 请求头中的参数 $content_length 请求头中的 Content-length 字段 $content_type 请求头中的 Content-Type 字段 $document_root 当前请求在 root 指令中指定的值 $host 请求头中的 Host 头部,缺省值为服务器名称 $http_user_agent 客户端 agent 信息 $http_cookie 客户端 cookie 信息 $limit_rate 限制连接速率 $request_method 请求的方法 $remote_addr 客户端的 IP 地址。 $remote_port 客户端的端口。 $remote_user 已经经过 Auth Basic Module 验证的用户名 $request_filename 当前请求的文件路径,由 root 或 alias 指令与 URI 请求生成 $scheme HTTP 方法,eq: http、https $server_protocol 请求的协议,eq: HTTP/1.0、HTTP/1.1 $server_addr 服务器地址 $server_name 服务器名称 $server_port 服务器的端口 $request_uri 包含请求参数的 URI,eq: '/path/index.html?user=kangkk' $uri 不包含请求参数的 URI,eq: '/path/index.html' $document_uri 与 $uri 相同
location
if
if (表达式) {
# TODO
}
表达式内部可以使用的命令:
- -f、!-f: 判断是否存在文件
- -d、!-d: 判断是否存在目录
- -e、!-e: 判断是否存在文件或目录
- -x、!-x: 判断文件是否可执行
location
location [=|~|~*|^~] /uri/ {
# TODO
}
=开头表示精确匹配^~开头表示uri以某个常规字符串开头~开头表示区分大小写的正则匹配~*开头表示不区分大小写的正则匹配!~、!~*分别为区分大小写不匹配、不区分大小写不匹配的正则/通用匹配
日志
重新打开日志
/home/kangkk
nginx -s reopen
ssl_dhparam
重定向
rewrite
通过使用 if 判断 host 来决策是否进行重定向。
# 如果 host 不是<code>既定域名</code>则重定向至<code>既定域名</code>
if ($host !~ (kangkk.com)$){
rewrite ^ https://kangkk.com$request_uri?;
}
# 如果 host 是 ip 格式则重定向至<code>既定域名</code>
if ($host ~ 192.168.1.1){
rewrite ^ https://kangkk.com$request_uri?;
}
rewrite 和 proxy_pass
路径重写,去除 /api/ 前缀,并代理到新的服务器地址。
location /api/ {
rewrite ^/api/(.*) /$1 break;
proxy_pass http://127.0.0.1:8080;
}
转发
端口转发
简单的端口转发。
nginx.conf
upstream quser_pool{
server 127.0.0.1:3100;
}
server {
listen 80;
server_name kangkk.com;
error_log /var/log/nginx/quser_pool.error.log;
access_log /var/log/nginx/quser_pool.access.log;
# 转发至均衡池
location / {
proxy_pass http://quser_pool;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
}
socket
other
| 同源策略 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
error
- 网址 301 重定向 URL 末尾自动加斜杠,比如现在有一个接口 https://www.as**o.cn/api/search?q=自然语言处理,会变成两个请求
https://www.as**o.cn/api/search?q=自然语言处理 301 https://www.as**o.cn/api/search?q=自然语言处理/ 200
- 只需要在 nginx 的配置文件中添加
port_in_redirect off;即可。 - 参考:
- http://nginx.org/en/docs/http/ngx_http_core_module.html#absolute_redirect
- http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name_in_redirect
- http://nginx.org/en/docs/http/ngx_http_core_module.html#port_in_redirect
No categories