Nginx

来自康健生活
Kangkk​(留言 | 贡献)2022年12月2日 (五) 07:46的版本 (→Nginx)

返回上一级

安装

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 示例 示例

日志

重新打开日志

/home/kangkk
nginx -s reopen

ssl_dhparam

路径重写

去除 /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

error