nginx在mac中的配置

本文介绍了如何在Mac上使用Homebrew安装Nginx,并提供了详细的安装步骤及配置指南。

本文介绍 nginx 在mac上的安装。 
我是通过brewhome 来安装的。 
brew install nginx 一路顺畅。

下面是安装信息。

复制代码 代码如下:

hematoMacBook-Pro:~ hechangmin$ brew search nginx 
nginx 
hematoMacBook-Pro:~ hechangmin$ brew install nginx 
==> Installing nginx dependency: pcre 
==> Downloading ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.13. 
######################################################################## 100.0% 
######################################################################## 100.0% 
==> ./configure --prefix=/usr/local/Cellar/pcre/8.13 --enable-utf8 --enable-unic 
==> make test 
==> make install 
/usr/local/Cellar/pcre/8.13: 111 files, 2.9M, built in 54 seconds 
==> Installing nginx 
==> Downloading http://nginx.org/download/nginx-1.0.8.tar.gz 
######################################################################## 100.0% 
==> Patching 
patching file auto/lib/pcre/conf 
patching file conf/nginx.conf 
==> ./configure --prefix=/usr/local/Cellar/nginx/1.0.8 --with-http_ssl_module --with-pcre --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/ngi 
==> make install 
==> Caveats 
In the interest of allowing you to run `nginx` without `sudo`, the default 
port is set to localhost:8080.

If you want to host pages on your local machine to the public, you should 
change that to localhost:80, and run `sudo nginx`. You'll need to turn off 
any other web servers running port 80, of course.

You can start nginx automatically on login running as your user with: 
mkdir -p ~/Library/LaunchAgents 
  cp /usr/local/Cellar/nginx/1.0.8/org.nginx.nginx.plist ~/Library/LaunchAgents/ 
  launchctl load -w ~/Library/LaunchAgents/org.nginx.nginx.plist

Though note that if running as your user, the launch agent will fail if you 
try to use a port below 1024 (such as http's default of 80.) 
Warning: /usr/local/sbin is not in your PATH 
You can amend this by altering your ~/.bashrc file 
==> Summary 
/usr/local/Cellar/nginx/1.0.8: 6 files, 616K, built in 19 seconds 
hematoMacBook-Pro:~ hechangmin$

启动nginx ,sudo nginx ;访问localhost:8080 发现已出现nginx的欢迎页面了。

备注: ln -s  /usr/local/sbin/nginx /usr/bin/nginx 做了个软连接。 
常用的指令有:

复制代码 代码如下:

nginx -V 查看版本,以及配置文件地址 
nginx -v 查看版本 
nginx -c filename 指定配置文件 
nginx -h 帮助 
nginx -s [reloadreopenstopquit]

brewhome 常用的指令:

复制代码 代码如下:

brew search  mysql  : 搜索具体的程序包 
brew install mysql : 安装具体的程序包 
brew info mysql : 查看具体程序的信息 
brew uninstall mysql : 卸载具体的应用(这里只是用mysql  作个例子)

配置文件地址:/usr/local/etc/nginx/nginx.conf 
编辑内容,可以制定web 目录,以及PHP 、 python  等。 

sudo nginx -s reload

======================


### Nginx配置文件(`nginx.conf`)在Mac OS上的配置方法 #### 文件结构概述 Nginx的主要配置文件通常位于 `/usr/local/etc/nginx/nginx.conf` 或者通过 Homebrew 安装的情况下可能是 `/opt/homebrew/etc/nginx/nginx.conf`。此文件包含了多个部分,其中最重要的几个是 `events`, `http` 和可能存在的其他上下文定义。 #### 基本配置示例 下面是一个简单的`nginx.conf`配置例子: ```nginx # 用户和工作进程数设置 user nobody; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } 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 /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; # 包含额外的配置文件 include /usr/local/etc/nginx/conf.d/*.conf; upstream backend_servers { ip_hash; server 127.0.0.1:8091; server 127.0.0.1:8092; server 127.0.0.1:8093; } server { listen 80; server_name localhost; location / { proxy_pass http://backend_servers; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } ``` 上述配置展示了如何指定日志格式[^4]、启用代理头传递功能[^2]以及实现基于 IP 的会话粘滞性能[^3]。 #### 关键配置项解释 - **事件模块(`events`)** - 控制处理连接的工作模型参数。 - **HTTP核心指令集(`http`)** - 设置MIME类型映射表的位置。 - 自定义访问日志记录方式。 - 启用高效传输大文件的支持选项。 - 设定保持活动状态的时间长度。 - 使用外部配置片段来扩展主配置文件的功能[^1]。 - **服务器块(`server`)** - 指明监听端口和服务名称。 - 对请求路径进行匹配并转发给后端服务集群。 - 处理错误页面显示逻辑。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值