Mac installation and configuration of Nginx
-
Update Homebrew
brew update
-
Download and install Nginx
brew install nginx
-
View nginx configuration information
tong@MacBook-Pro ~ % brew info nginx 版本信息 ==> nginx: stable 1.25.1 (bottled), HEAD HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server https://nginx.org/ Nginx安装目录 /opt/homebrew/Cellar/nginx/1.25.1 (26 files, 2.4MB) * Poured from bottle using the formulae.brew.sh API on 2023-06-17 at 15:52:25 安装来源 From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/nginx.rb License: BSD-2-Clause ==> Dependencies Required: [email protected] ✔, pcre2 ✔ ==> Options --HEAD Install HEAD version 根目录 ==> Caveats Docroot is: /opt/homebrew/var/www 重点!!!nginx 的配置文件及默认启动端口 8080 The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that nginx can run without sudo. Nginx将在 Server 目录下加载所有文件,在这个 nginx will load all files in /opt/homebrew/etc/nginx/servers/. To 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: /opt/homebrew/opt/nginx/bin/nginx -g daemon off; ==> Analytics install: 81,387 (30 days), 151,458 (90 days), 151,458 (365 days) install-on-request: 81,387 (30 days), 151,458 (90 days), 151,458 (365 days) build-error: 0 (30 days)
-
Nginx common commands
启动 brew services start nginx 停止 brew services stop nginx 终端进入nginx 目录 cd /opt/homebrew/etc/nginx 编辑 nginx.conf 配置文件 vim nginx.conf 编辑完成后:wq保存退出 重新加载配置文件 nginx -s reload
-
Nginx configuration file nginx.conf
全局配置 events { worker_connections 1024; } http { http 配置 #原本没有,自己手动添加 upstream XX{ //负载均衡配置 } #主要配置 server,配自己需要的信息 server { listen 8070; server_name localhost; location / { root html; index index.html index.htm; proxy_pass http://localhost:8080; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } //原注释,现在释放生效 # HTTPS server server { listen 443 ssl; server_name localhost; #证书 cert.pen和 cert.key换成证书的目录 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; } } include servers/*; }
The configuration file is mainly to configure the content in the server. It is necessary to understand the concept of reverse proxy. It is recommended to back up when editing nginx.conf to prevent modification errors. Note that the added code must be at the end .