参看了这里:Mac 安装Nginx详细教程
前提你的 Mac 已经安装了 homebrew 或者 homebrew-CN 自己 bing 搜索教程
主要步骤:
brew install nginx
# 进入自己的用户目录
cd ~
# 创建两个目录
mkdir -p nginx/{html,conf}
# 如果忘记自己 Mac 上面 安装的 NGINX 的信息 用下面的命令查看
brew info nginx
# 修改默认的 nginx.conf
下面的代码 主要是增加一个新的 自动加载默认配置的目录。
include /Users/mac/nginx/conf/*; mac是我的 Mac 电脑用户名,你要根据你的实际情况修改,不要照抄
# cat /usr/local/etc/nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#gzip on;
server {
listen 8080;
server_name localhost;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include servers/*;
include /Users/mac/nginx/conf/*;
}
在 include /Users/mac/nginx/conf/*; 这个目录下面新增 nginx 配置如下
map $http_upgrade $connection_upgrade{
default upgrade;
'' close;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /Users/mac/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 反向代理,处理管理端发送的请求
location /api/ {
proxy_pass http://localhost:8080/admin/;
#proxy_pass http://webservers/admin/;
}
# 反向代理,处理用户端发送的请求
location /user/ {
proxy_pass http://localhost:8080/user/;
# proxy_pass http://webservers/user/;
}
# WebSocket
location /ws/ {
proxy_pass http://localhost:8080/ws/;
# proxy_pass http://webservers/ws/;
proxy_http_version 1.1;
proxy_read_timeout 3600s;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "$connection_upgrade";
}
}
开启和关闭 nginx 服务, 感觉好像如果不关闭以后会开机自动启动,
# 启动 nginx 服务
brew services start nginx
# 关闭 nginx 访问
brew services stop nginx
每一次 修改 nginx 配置都需要测试一下是否有语法问题。
/usr/local/opt/nginx/bin/nginx -t
# 然后加新的配置
sudo /usr/local/opt/nginx/bin/nginx -s reload
运行 brew services start nginx
你可能会遇到如下问题,也可能不会
Mac 使用homebrew 启动某个服务的时候,出现问题:Error: uninitialized constant Homebrew::Service::System的解决方案
cd /usr/local/Homebrew/Library/Taps/homebrew/
rm -rf homebrew-services
# 这一步有可能会卡主 因为会访问 github 如果你这里不会卡就跳过
brew tap homebrew/services
# 解决 github 卡主 前提是你有代理 我的代理的本机端口是 7890 你的如果不一样就改一下
# 开启 git 代理:
git config --global http.proxy 'socks5://127.0.0.1:7890'
git config --global https.proxy 'socks5://127.0.0.1:7890'
# 取消 git 代理
git config --global --unset http.proxy
git config --global --unset https.proxy