安裝:
brew install 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
按照提示啟動
brew services start nginx
檢視狀态:
nginx -t
如果報錯
nginx: [emerg] open() "/usr/local/var/run/nginx.pid" failed (13: Permission denied)
權限問題
sudo chown -R $(whoami) /usr/local/var/run # 具體路徑參考報錯提示中的路徑
重新開機nginx伺服器
brew services stop nginx # 停止
brew services start nginx # 啟動
提示中預設顯示8080端口
http://127.0.0.1:8080/網頁中出現下面的文字,就啟動成功了
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
靜态伺服器
在路徑
/usr/local/var/www
下放入檔案
mytest.html
内容如下:
<h1>hello nginx! 2018-07-10</h1>
通過連結通路測試:
http://127.0.0.1:8080/mytest.html通用配置
user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
client_max_body_size 20M;
include conf.d/*.conf;
}
注意到
include conf.d/*.conf;
這樣就可以将conf.d這個檔案夾中的所有配置檔案加載進來,實作單獨配置,互不影響。