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

一.安裝

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、控制字符串長度,對圖片進行壓縮,再轉成字符串
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容