使用Homebrew方式进行安装
步骤:
1、更新 Homebrew
brew update
2、下载并安装 Nginx
brew install nginx
3、查看 nginx 配置信息
brew info nginx
zhanghua@Breeze ~ % brew info nginx
// 版本信息
==> nginx: stable 1.25.1 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
// Nginx安装目录
https://nginx.org/
/usr/local/Cellar/nginx/1.25.1_1 (26 files, 2.4MB) *
Poured from bottle using the formulae.brew.sh API on 2023-08-10 at 10:48:47
// 安装来源
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/nginx.rb
License: BSD-2-Clause
==> Dependencies
Required: openssl@3 ?, pcre2 ?
==> Options
--HEAD
Install HEAD version
// 根目录
==> Caveats
Docroot is: /usr/local/var/www
// 重点!!!nginx 的配置文件及默认启动端口 8080
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
// Nginx将在 Server 目录下加载所有文件,在这个
nginx will load all files in /usr/local/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:
/usr/local/opt/nginx/bin/nginx -g daemon off;
==> Analytics
install: 11,885 (30 days), 57,023 (90 days), 84,746 (365 days)
install-on-request: 11,855 (30 days), 56,930 (90 days), 84,620 (365 days)
build-error: 3 (30 days)
4、nginx常用命令
启动
brew services start nginx
停止
brew services stop nginx
终端进入nginx 目录
cd /opt/homebrew/etc/nginx
编辑 nginx.conf 配置文件
vim nginx.conf
编辑完成后:wq保存退出
重新加载配置文件
nginx -s reload
5、nginx配置文件?nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
#http 配置
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream xx{
// 负载均衡配置
server 127.0.0.1:8080 weight =1;
server 127.0.0.1:8081 weight =1;
}
// nginx 端口 ,只要访问8080端口 就会被nginx 监听 ,如果电脑端口有冲突 可以更换端口号
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
// 反向代理
proxy_pass http://域名
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# 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/*;
}