MAC brew 安装 Nginx 以及配置域名

  1. 查看brew 的版本
$ brew --version
  1. 搜索nginx
$ brew search nginx
  1. 安装 nginx
$ brew install nginx

==> 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

  1. 查看nginx配置文件目录
$ open /usr/local/etc/nginx/
  1. 查看nginx被安装到的目录
$ open /usr/local/Cellar/nginx
  1. 启动 nginx
$ /usr/local/Cellar/nginx/1.17.7/bin/nginx
  1. 关闭 nginx
$ /usr/local/Cellar/nginx/1.17.7/bin/nginx -s stop
  1. 重新启动
$ /usr/local/Cellar/nginx/1.17.7/bin/nginx -s reload
  1. 查看访问日志、错误日志文件
$ cd /usr/local/var/log/nginx/
$ tail -f access.log
$ tail -f error.log
  1. 查看 nginx 相关文件
$ nginx -V
  1. 通过浏览器访问 http://localhost/
    在这里插入图片描述
遇到的问题

fengruizhideMacBook-Pro:work-manager-web fengrz$ brew install nginx
Updating Homebrew…
Error: The following directories are not writable by your user:
/usr/local/share/man/man5
/usr/local/share/man/man7
You should change the ownership of these directories to your user.
sudo chown -R $(whoami) /usr/local/share/man/man5 /usr/local/share/man/man7
And make sure that your user has write permission.
chmod u+w /usr/local/share/man/man5 /usr/local/share/man/man7

  • 查看目录权限
$ ls -l /usr/local/share/man/

在这里插入图片描述

  1. 修改文件目录的所有者和群组
$ sudo chown -R fengrz:admin /usr/local/share/man/
  1. 修改后的结果,如图在这里插入图片描述
配置域名
  1. 配置本地域名路由指向
$ sudo vim /etc/hosts

127.0.0.1 thnm.fengrz.com
127.0.0.1 thnms.fengrz.com

  1. 配置域名
$ cd /usr/local/etc/nginx
$ mkdir vhosts
$ cd vhosts
$ vim thnm.fengrz.com.conf 
  • thnm.fengrz.com.conf 内容如下
  • thnm.fengrz.com server 配置
server {
    listen       80;
    server_name  thnm.fengrz.com;

    location / {
        root   /Users/fengrz/data/project/work-manager-web;
        index  index.html index.htm;
    }
    location /api {
        add_header 'Access-Control-Allow-Origin' '*';
        proxy_pass http://thnms.fengrz.com;
    }

}
  • thnms.fengrz.com server 配置
server {
    listen       80;
    server_name  thnms.fengrz.com;
        location / {
                root html;
                index index.html index.htm;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Real-Port $remote_port;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass   http://127.0.0.1:4521/;

        }
        location /api/ {
                root html;
                index index.html index.htm;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Real-Port $remote_port;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass   http://127.0.0.1:4521/;

        }


        location /bind/ {
                root html;
                index index.html index.htm;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Real-Port $remote_port;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass   http://127.0.0.1:4521/;

        }

}

  1. 编辑主配置文件 nginx.conf

include vhosts/*.conf;

在这里插入图片描述

  1. 检查配置文件是否正确
$ /usr/local/Cellar/nginx/1.17.7/bin/nginx -t
  1. 重启 nginx
$ /usr/local/Cellar/nginx/1.17.7/bin/nginx -s reload
  1. 启动浏览器 访问 http://thnms.fengrz.com

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值