0%

Mac上安装Nginx

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

配置

默认根目录/usr/local/var/www,可以在配置文件/usr/local/etc/nginx/nginx.conf中修改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
server {
listen 8080; # 端口可以自己修改
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root index # 自定义路径
index index.html index.htm;
}
...

图片服务器,需要配置images目录,在conf文件中增加如下,然后重启。

1
2
3
4
5
location /images {
root /xxxx/xxxx; # 图片存放的目录
autoindex on; # 开启目录游览
}

管理

使用brew来管理服务

1
2
3
brew services start nginx # 启动Nginx后台服务
brew services restart nginx # 重启Nginx后台服务
brew services stop nginx # 停止Nginx服务

诊断

Nginx诊断,有遇到过Nginx已经启动,但是无法访问,可以使用nginx -t来诊断具体的错误。