0%

Mac OS 上安装 Niginx

反馈请联系hertz@hertzwang.com,谢谢

参考Mac下用brew安装nginx

安装brew(Homebrew)

一般默认装有brew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew常用命令:

  • 搜索软件:brew search nginx
  • 卸载软件:brew uninstall nginx
  • 升级brew:sodu brew update
  • 查看安装信息:sodu brew info nginx
  • 查看已经安装的软件:brew list
  • 注:脚本会在执行前暂停,按下回车并输入管理员密码。更多信息查看官方网站

安装Nginx

$ brew install nginx

nginx常用命令:

  • 启动nginx服务:sudo brew services start nginx
  • 关闭nginx服务:sudo brew services stop nginx
  • 重新加载nginxnginx -s reload
  • 停止nginxnginx -s stop
  • 检查配置文件语法:nginx -t

注:也可使用sudo nginx启动、使用sudo nginx -s stop停止。

Nginx相关信息

  • 部署路径:/usr/local/var/www
  • nginx配置文件路径:/usr/local/etc/nginx/nginx.conf
  • 默认使用8080端口

执行 brew info nginx 后得到以下信息

nginx: stable 1.12.0 (bottled), devel 1.13.2, HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
/usr/local/Cellar/nginx/1.12.0_1 (23 files, 1MB) *
  Poured from bottle on 2017-07-08 at 18:27:37
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/nginx.rb
==> Dependencies
Required: pcre ✔, openssl@1.1 ✔
Optional: passenger ✘
==> Options
--with-debug
    Compile with support for debug log
--with-gunzip
    Compile with support for gunzip module
--with-passenger
    Compile with support for Phusion Passenger module
--with-webdav
    Compile with support for WebDAV module
--devel
    Install development version 1.13.2
--HEAD
    Install HEAD version
==> Caveats
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.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

worker_processes 1;

events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

# 监听80端口
server {
listen 80;
server_name localhost xxx.com;

location / {
root C:\Users\xx\workspace\xx;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}


# xxx 端口
server {
listen xxx;
server_name xxx.xxx.xx;

location / {
# 本地端口
proxy_pass http://127.0.0.1:xxxx;
# 详细设置
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
}

# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}