在 MAC 下配置 Nginx

1. 更新Homebrew

# mac使用brew update无反应,更新慢解决办法
# 替换brew.git
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 替换homebrew-core.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# update
brew update

2. 安装 nginx

$ brew install nginx

==> Installing dependencies for nginx: openssl@1.1 and pcre

==> Installing nginx dependency: openssl@1.1

==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/openssl@1.1

blablablabla....

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

3. 启动Nginx

登录时作为服务启动: brew services start nginx
直接启动: nginx
默认地址:http://localhost:8080

4. 部署新网站

修改文件 /usr/local/etc/nginx/nginx.conf,可用 sudo pico /usr/local/etc/nginx/nginx.conf,或者直接用记事本编辑。

    # 配置网站
    server {
        listen 8081;
        server_name   localhost;
        location / {
            root     /XXX/DemoSite;
            index    index.html;
        }
    }

重新启动nginx

sudo nginx -s reload

 打开浏览器查看:

http://localhost:8081/
原文地址:https://www.cnblogs.com/surfsky/p/12705386.html