安装Homebrew
安装Nginx
启动Nginx
配置JSON文件
配置Nginx
展现成果
1. 安装Homebrew
打开终端,输入:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/[install](https://www.baidu.com/s?wd=install&tn=24004469_oem_dg&rsv_dl=gh_pl_sl_csd)/master/install)"
跟着步骤走。终端都有提示。
2.依赖Homebrew安装Nginx
依旧在终端中
brew install nginx
这个等待时间会比较长。如果你仔细看了终端的进程。你会发现你用homebrew安装是多么聪明了。其实在安装nginx之前还要帮你安装很多别的,如果你独立安装,是很费劲的。。。
3.启动Nginx
依旧在终端里,输入
nginx
回车就行了。
怎样验证Nginx能用了呢。你在浏览器里输入localhost:8080,回车,出现以下画面,说明成功。
浏览器输入 localhost:8080
4.配置JSON文件
那配置文件是在
/usr/local/Cellar/nginx/1.15.9/html/
这个目录下会默认有50x.html和index.html两个文件。你再新加一个json文件,取个名字叫oscar.json,里面随便写些字典数组,保存。
5.配置Nginx
安装完之后,默认路径是在
/usr/local/etc/
这个文件下会有一个nginx文件夹和一个openssl的文件夹。
点开nginx文件夹,里面会有个
nginx.conf.default
的文件,记住,不要看错了,这里面的名字很相像。用文本编辑器打开,里面默认是有内容的,可以看看。将以下内容粘贴进去。
server {
listen 8080;
server_name localhost;
#access_log logs/host.access.log main;
location ~* {
add_header Content-Type "application/json";
root html;
if (!-f $request_filename) {
rewrite ^/(.*) /$1.json last;
}
index index.php index.html index.htm;
}
error_page 405 =200 http://$host$request_uri;
}
6.展现成果
如果你新加的json文件叫oscar.json,目录是在刚刚说的
/usr/local/Cellar/nginx/1.15.9/html/oscar.json
那么你在浏览器里输入
localhost:8080/oscar.json
看看你的成果吧!
如果你的页面有你配置的JSON文件,那么恭喜你。别人如果想访问你的资源,保证你两在同一个局域网内,然后把你的ip地址替换localhost,再看看你朋友的电脑的页面吧。
Mac 系统 开机启动 nginx
通过brew 安装install 后
sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
即可。