Nginx:修订间差异
来自康健生活
无编辑摘要 |
|||
| (未显示同一用户的11个中间版本) | |||
| 第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"> | ||
ps -ef | grep nginx</syntaxhighlight>|<syntaxhighlight lang="bash"> | ps -ef | grep nginx</syntaxhighlight>|<syntaxhighlight lang="bash"> | ||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
杀死进程。 | 杀死进程。 | ||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<syntaxhighlight lang="bash"> | ||
kill -TERM 进程号 | kill -TERM 进程号 | ||
| 第37行: | 第148行: | ||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
使用 nginx 进程号进行重启。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<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"> | |||
</syntaxhighlight>|$|/home/kangkk}} | 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/ { | ||
| 第71行: | 第247行: | ||
proxy_pass http://127.0.0.1:8080; | proxy_pass http://127.0.0.1:8080; | ||
}</pre> | }</pre> | ||
== 转发 == | == 转发 == | ||
| 第113行: | 第272行: | ||
== socket == | == socket == | ||
== other == | |||
{| class="wikitable" | |||
|- | |||
| [[/同源策略|同源策略]] || 示例 || 示例 | |||
|- | |||
| 示例 || 示例 || 示例 | |||
|- | |||
| 示例 || 示例 || 示例 | |||
|} | |||
== error == | == error == | ||