通过Homebrew 安装nginx
brew install nginx
配置
添加配置文件在 /usr/local/etc/nginx/servers
目录下 ( 一般都是修改 /usr/local/etc/nginx
目录下nginx.conf 文件, 后来发现nginx.conf 下有 include servers/*;
为了方便管理我本地项目,就把本地项目配置都放到 /usr/local/etc/nginx/servers
目录下 )
nginx.conf配置 把 server 和其他基本配置分开。其中server相关配置全部放置在config.d文件夹中,其他配置依旧防止在nginx.conf中
附 文末:本地nginx测试服务器配置
ps -ef|grep nginx
501 15800 1 0 12:17上午 ?? 0:00.00 nginx: master process /usr/local/Cellar/nginx/1.8.0/bin/nginx -c /usr/local/etc/nginx/nginx.conf
501 15801 15800 0 12:17上午 ?? 0:00.00 nginx: worker process
501 15848 15716 0 12:21上午 ttys000 0:00.00 grep nginx
表示已启动成功,如果不是上图结果,在终端中执行
/usr/local/Cellar/nginx/1.8.0/bin/nginx -c /usr/local/etc/nginx/nginx.conf
命令即可启动nginx。??这时候如果成功访问localhost:8080,说明成功安装和启动好了。
?? 启动前确认,8080 端口未占用
在终端中输入 ps -ef|grep nginx
获取到nginx的进程号,注意是找到“nginx:master”的那个进程号,如下面的进程好是 15800
501 15800 1 0 12:17上午 ?? 0:00.00 nginx: master process /usr/local/Cellar/nginx/1.8.0/bin/nginx -c /usr/local/etc/nginx/nginx.conf
501 15801 15800 0 12:17上午 ?? 0:00.00 nginx: worker process
501 15848 15716 0 12:21上午 ttys000 0:00.00 grep nginx
在终端中输入以下几种命令都可以停止
kill -QUIT 15800 (从容的停止,即不会立刻停止)
Kill -TERM 15800 (立刻停止)
Kill -INT 15800 (和上面一样,也是立刻停止)
如果配置文件错误,则将启动失败,所以在启动nginx之前,需要先验证在配置文件的正确性 nginx -t -c /usr/local/etc/nginx/nginx.conf
,如下表示配置文件正确
/usr/local/Cellar/nginx/1.8.0/bin/nginx -t -c /usr/local/etc/nginx/nginx.conf
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
重启有两种方法?:
cd /usr/local/Cellar/nginx/1.8.0/bin/
./nginx -s reload
kill -HUP [进程号]
启动:sudo nginx
停止:sudo nginx -s stop
验证:sudo nginx -t /usr/local/nginx/conf/nginx.conf
nginx: [error] invalid PID number “” in “/usr/local/var/run/nginx/nginx.pid”
$ sudo nginx -c /usr/local/etc/nginx/nginx.conf
$ sudo nginx -s reload
项目根目录为开发项目下构建后dist, api 为 接口api
server {
listen 8090; #监听端口 可以访问127.0.0.1:8090
# server_name test.com; #这里要是使用需要配本地的host
#charset koi8-r;
access_log logs/k8s.log;
location = / {
root /Users/macbookpro/Downloads/workspace/node-web/dist; #你项目的根目录
index index.html index.htm;
}
location /api/ {
proxy_pass https://api.baidu.com; #****这里配置nginx代理,填上服务器地址
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
原文:https://www.cnblogs.com/yc8930143/p/11933015.html