MAC brew 安装 Nginx 以及配置域名
- 查看brew 的版本
$ brew --version
- 搜索nginx
$ brew search nginx
- 安装 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
- 查看nginx配置文件目录
$ open /usr/local/etc/nginx/
- 查看nginx被安装到的目录
$ open /usr/local/Cellar/nginx
- 启动 nginx
$ /usr/local/Cellar/nginx/1.17.7/bin/nginx
- 关闭 nginx
$ /usr/local/Cellar/nginx/1.17.7/bin/nginx -s stop
- 重新启动
$ /usr/local/Cellar/nginx/1.17.7/bin/nginx -s reload
- 查看访问日志、错误日志文件
$ cd /usr/local/var/log/nginx/
$ tail -f access.log
$ tail -f error.log
- 查看 nginx 相关文件
$ nginx -V
- 通过浏览器访问 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/
- 修改文件目录的所有者和群组
$ sudo chown -R fengrz:admin /usr/local/share/man/
- 修改后的结果,如图
配置域名
- 配置本地域名路由指向
$ sudo vim /etc/hosts
127.0.0.1 thnm.fengrz.com
127.0.0.1 thnms.fengrz.com
- 配置域名
$ cd /usr/local/etc/nginx
$ mkdir vhosts
$ cd vhosts
$ vim thnm.fengrz.com.conf
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/;
}
}
- 编辑主配置文件 nginx.conf
include vhosts/*.conf;
- 检查配置文件是否正确
$ /usr/local/Cellar/nginx/1.17.7/bin/nginx -t
- 重启 nginx
$ /usr/local/Cellar/nginx/1.17.7/bin/nginx -s reload
- 启动浏览器 访问 http://thnms.fengrz.com
更多推荐
所有评论(0)