安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
brew install nginx

## 安装后提示
Docroot is: /usr/local/var/www

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

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
brew services start nginx
Or, if you don't want/need a background service you can just run:
nginx

编辑配置

1
vi /usr/local/etc/nginx/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
## 简单示例
user nobody;
worker_processes 1;

error_log /Users/fang/Service/nginx-running/error.log;
pid /Users/fang/Service/nginx-running/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /Users/fang/Service/nginx-running/access.log main;

sendfile on;
keepalive_timeout 65;

include /Users/fang/Service/nginx-servers/*;
}

编辑默认 server 配置

1
vi /Users/fang/Service/nginx-servers/default
1
2
3
4
5
6
7
8
9
10
11
## 简单示例
server {
listen 80;
root /Users/fang/Service/nginx-html;
index index.html index.htm;
server_name _;

location / {
try_files $uri $uri/ =404;
}
}

启动

由于有监听 80 端口的 Server,必须使用 sudo 去启动(小于 1024 的端口都需要使用 root 权限)

1
2
3
4
5
6
7
8
## 检查配置合法性
sudo nginx -t

## 启动 nginx
sudo nginx

## 开机启动
sudo brew services start nginx