MAC安装Nginx 以及使用中遇到的坑

作者 : 开心源码 本文共3040个字,预计阅读时间需要8分钟 发布时间: 2022-05-13 共218人阅读

一.安装

brew install nginx
注释: brew 安装
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  1. 安装完之后,可以在终端看到路径信息
    /usr/local/etc/nginx/nginx.conf (配置文件路径) /usr/local/var/www (服务器默认路径) /usr/local/Cellar/nginx/1.8.0 (安装路径)

  2. 假如是macOS 1.12以上的系统,在安装过程中可能会出现”warning”,说是不支持该版本的操作系统,可以暂时先忽略它。

二. 启动

  1. 在终端输入 ps -ef|grep nginx看能否有启动
  2. 假如没有启动执行以下命令启动/usr/local/Cellar/nginx/1.8.0/bin/nginx -c /usr/local/etc/nginx/nginx.conf 肯定要注意路径能否是自己的安装路径
  3. cd 到/usr/local/Cellar/nginx/1.8.0/bin/ 目录下 执行sudo ./nginx
  4. 这时候假如成功访问localhost:8080,说明成功安装和启动好了。

三. 中止

  1. 终端输入ps -ef|grep nginx获取到nginx的进程号, 注意是找到“nginx:master”的那个进程号
  2. kill -QUIT 15800 (从容的中止,即不会立刻中止)
    Kill -TERM 15800 (立刻中止)
    Kill -INT 15800 (和上面一样,也是立刻中止)

四、重启

  1. 假如配置文件错误,则将启动失败,所以在启动nginx之前,需要先验证在配置文件的正确性,如下表示配置文件正确
    promote:bin yangqianhua$ /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
  2. 重启有两种方法
  • 在终端输入输入如下命令就可重启
    promote:~ yangqianhua$ cd /usr/local/Cellar/nginx/1.8.0/bin/
    promote:bin yangqianhua$ ./nginx -s reload

  • 根据进程号重启,执行命令 kill -HUP 进程号

Nginx 遇到的坑

  • sudo find / -name nginx* 强制全局搜索nginx相关文件

  • cd 到bin目录下执行./ngnix

nginx: [alert] could not open error log file: open() "/usr/local/var/log/nginx/error.log" failed (13: Permission denied) 2018/09/18 16:53:12 [emerg] 9734#0: open() "/usr/local/Cellar/nginx/1.15.3/logs/error.log" failed (2: No such file or directory)

操作 cd /usr/local/Cellar/nginx/1.15.3/ 新建logs目录增加error.log文件, 执行sudo nginx 出现如下错误

nginx: [warn] 1024 worker_connections exceed open file resource limit: 256 localhost:bin hello_xie$ nginx: [warn] 1024 worker_connections exceed open file resource limit: 256 -bash: nginx:: command not found localhost:bin hello_xie$ nginx: [warn] 1024 worker_connections exceed open file resource limit: 256 -bash: nginx:: command not found

  • 执行 ulimit -a
    core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited file size (blocks, -f) unlimited max locked memory (kbytes, -l) unlimited max memory size (kbytes, -m) unlimited open files (-n) 256 pipe size (512 bytes, -p) 1 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 1418 virtual memory (kbytes, -v) unlimited

  • 执行 ulimit -n 1024

  • sudo nginx -t

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

另一种处理方案(stackoverflow)

到 nginx.conf 文件增加 worker_rlimit_nofile 1024;这种入侵性更小, 推荐使用

Mac 403 Forbidden

要在Nginx.conf文件 最开始的地方加权限
user root owner 而后重启或者者重新加载Nginx

  • 这个权限自己搞了好久一直以为是路径配的不对,结果是这个问题
  • mac 中的Nginx安装路径,和win中的不同, win是下载的安装包, 所有的东西都在解压缩文件中, mac 通过brew 安装, 默认启?????动程序在/user/local/下Cellar, etc, var,三个文件夹下

追加1

"/usr/local/var/run/nginx/client_body_temp/0000000004" failed (13: Permission denied), client: 127.0.0.1, server: localhost, request: "POST /vip/doc/docs?userId=admin&userName=%E7%AE%A1%E7%90%86%E5%91%98&sysCode=ivs&businessModel=frameworkAppr HTTP/1.1", host: "localhost:3000", referrer: "http://localhost:3000/"

  • 报错起因是 client_body_temp 文件夹没有权限, 关于client_body_temp目录的作用,简单说就是假如用户端POST一个比较大的文件,长度超过了nginx缓冲区的大小,需要把这个文件的部分或者者一律内容暂存到client_body_temp目录下的临时文件。
  • 处理办法:
    1、拥有client_body_temp的权限,切换root客户,
    #chmod -R 755 /usr/local/var/run/nginx/client_body_temp
    2、控制字符串长度,对图片进行压缩,再转成字符串
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » MAC安装Nginx 以及使用中遇到的坑

发表回复