查看“︁Nginx”︁的源代码
来自康健生活
←
Nginx
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
[[Linux|返回上一级]] == [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" |- | 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" |- | 配置目录 || <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"> ps -ef | grep nginx</syntaxhighlight>|<syntaxhighlight lang="bash"> </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 进程号进行重启。 {{Shc_out|<syntaxhighlight lang="bash"> kill -HUP 进程号</syntaxhighlight>|<syntaxhighlight lang="bash"> </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> == 日志 == === 重新打开日志 === {{shc|<syntaxhighlight lang="bash"> nginx -s reopen</syntaxhighlight>|$|/home/kangkk}} == 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> location /api/ { rewrite ^/api/(.*) /$1 break; proxy_pass http://127.0.0.1:8080; }</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 == == error == * 网址 301 重定向 URL 末尾自动加斜杠,比如现在有一个接口 <nowiki>https://www.as**o.cn/api/search?q=自然语言处理</nowiki>,会变成两个请求 *: <syntaxhighlight lang="javascript"> https://www.as**o.cn/api/search?q=自然语言处理 301 https://www.as**o.cn/api/search?q=自然语言处理/ 200 </syntaxhighlight> *: 只需要在 nginx 的配置文件中添加 <code>port_in_redirect off;</code> 即可。 *: 参考: *: 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
此页面嵌入的页面:
模板:Hc
(
查看源代码
)
模板:Hc/style.css
(
查看源代码
)
模板:Shc
(
查看源代码
)
模板:Shc/style.css
(
查看源代码
)
模板:Shc out
(
查看源代码
)
模板:Shc out/style.css
(
查看源代码
)
返回
Nginx
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。