环境:MacBook Air M1, macOS Monterey 12.6

安装

建议选择homebrew安装,安装完成后会显示基本信息,后续如果要重新查看可通过命令实现。
brew install nginx 安装
brew info nginx 查看信息

1
2
3
4
5
6
7
8
9
10
11
12
13

Docroot is: /opt/homebrew/var/www

The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /opt/homebrew/etc/nginx/servers/.

To restart nginx after an upgrade:
brew services restart nginx
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/nginx/bin/nginx -g daemon off;

使用

brew services start nginx 启动
brew services stop nginx 停止
nginx -t 测试配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ nginx -h
nginx version: nginx/1.23.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]

Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /opt/homebrew/Cellar/nginx/1.23.1/)
-e filename : set error log file (default: /opt/homebrew/var/log/nginx/error.log)
-c filename : set configuration file (default: /opt/homebrew/etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file

报错处理

1
2
3
4
5
nginx: [alert] could not open error log file: open() "/opt/homebrew/var/log/nginx/error.log" failed (13: Permission denied)
nginx: the configuration file /opt/homebrew/etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/opt/homebrew/var/log/nginx/access.log" failed (13: Permission denied)
2022/10/02 15:21:54 [emerg] 50876#0: open() "/opt/homebrew/var/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test failed

我在使用过程中主要有以上几个错误,在网上查了很多资料,最终最简单的操作就是直接删掉error.log,nginx.pid,access.log,如果有修改nginx.conf,按照以下流程即可:

1
2
3
4
brew  services stop nginx
# 手动删除以上报错的文件,nginx.conf不要删!
nginx -t
brew services start nginx

修改配置文件nginx.conf

/opt/homebrew/etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
server {
listen 8080; # 端口号8080,可以为80,只要不和别的冲突
server_name localhost; # 我不知道怎么解释哈哈哈哈
location / { # 网站根目录,我还没研究明白怎么修改
root html;
index index.html index.htm index.php;
}
}