ps:macOS系统,以下内容是mac电脑,且使用brew安装的,其他慎看,因为安装位置等信息可能会略有不同
1.下载Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2. 下载 nginx
brew install nginx
3. 查看是否安装成功和安装信息
-
nginx -v
出现以下安装成功

以下是出现报错的情况,命令会显示具体的错误信息和错误行号。根据错误信息修复配置文件。

-
brew info nginx

4. 启动nginx
nginx
输入后没有输出,打开http://localhost:8080可以看到成功页面

5. 停止nginx
sudo nginx -s stop(不带sudo可能会报权限错误)
6. 查看nginx进程
ps aux | grep nginx

7. nginx.conf文件是否正确
nginx -t
如果有错误,命令会显示具体的错误信息和错误行号。根据错误信息修复配置文件。
以下是正确的文件配置出现的结果

8. 查看某个端口下的进程
sudo lsof -i :8080

9. 查看nginx日志
cat /opt/homebrew/var/log/nginx/error.log

10. 额外补充
- nginx进程每次运行的时候会生成一个nginx.pid文件在此目录下:
/opt/homebrew/var/run

- nginx.conf文件存放位置
/opt/homebrew/etc/nginx/nginx.conf,一般通过sudo vim /opt/homebrew/etc/nginx/nginx.conf进去配置查看

异常情况
打开http://localhost:8080,显示一下页面,证明nginx没有正常运行,有报错,这种情况需要具体分析,下面举一个自己碰到的例子。

解决办法:
nginx -t,先检查自己的nginx.conf配置文件是否配置错误,如果没有错误那么继续往下看cat /opt/homebrew/var/log/nginx/error.log,查看日志,我这边的报错是这样子,可以看到在16:22的时候,我启动了一次nginx,报错为
18262#0: *1 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: “GET / HTTP/1.1”, upstream: “http://[::1]:3000/”, host: “localhost:8080”
【 Nginx 尝试反向代理到上游服务器时连接被拒绝】

- 然后突然意识到自己在nginx.conf中给http://localhost:3000的服务配置了一个代理,但是我的http://localhost:3000服务并没有启动,导致nginx报错(nginx小白太难了…)

- 删除proxy_pass这个代理,或者启动代理的服务localhost:3000,nginx就可以正常工作了!!!
异常2
nginx: [emerg] invalid number of arguments in "proxy_pass" directive in /opt/homebrew/etc/nginx/nginx.conf:44 nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test failed
nginx.conf的44行报错了,我这里是没有写分号

历经重重困难,显示这个页面表示nginx已经正常运行了~













![[C++]智能指针](https://i-blog.csdnimg.cn/direct/6d99237940184a918e11b122a9151c33.png)






