Mac使用Nginx设置代理,并禁用自带Apache

Mac自带Apache服务,并占用80端口,如果需要使用Nginx,需要禁用Apache并自己安装Nginx

一、禁用自带Apache

1.关闭Apache

bash 复制代码
sudo apachectl -k stop

如果出现如下报错:
httpd: Could not reliably determine the server's fully qualified domain name, using pgydeMacBook-Pro.local. Set the 'ServerName' directive globally to suppress this message

(1) 打开apache配置目录:

bash 复制代码
vim /etc/apache2/httpd.conf

(2) 搜索:#ServerName www.example.com:80

增加一行 ServerName localhost:80

(3) 重新启动Apache

bash 复制代码
sudo apachectl restart

(4) 关闭Apache

bash 复制代码
sudo apachectl -k stop

2.禁止apache自启动

bash 复制代码
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

二、使用 Homebrew 安装 Nginx

在 macOS 上使用 Homebrew 安装 Nginx 时,Nginx 的默认安装目录通常是 /usr/local/Cellar/nginx/。具体的安装路径可以通过以下命令查看:

bash 复制代码
brew --prefix nginx

1. Nginx 配置文件和目录

安装完成后,Nginx 的主要配置文件和目录通常位于以下位置:

  • 主配置文件/usr/local/etc/nginx/nginx.conf
  • 站点配置文件/usr/local/etc/nginx/servers/
  • 日志文件/usr/local/var/log/nginx/
  • HTML 文件/usr/local/var/www/

2. 安装 Nginx

使用 Homebrew 安装 Nginx:

bash 复制代码
brew install nginx

3. 启动和管理 Nginx

安装完成后,可以使用以下命令启动、停止和重启 Nginx:

bash 复制代码
# 启动 Nginx

brew services start nginx

# 停止 Nginx

brew services stop nginx

# 重启 Nginx

brew services restart nginx

4. 配置 Nginx

您可以编辑 Nginx 的主配置文件 /usr/local/etc/nginx/nginx.conf,或者在 /usr/local/etc/nginx/servers/ 目录中添加新的站点配置文件。

例如,创建一个新的站点配置文件 /usr/local/etc/nginx/servers/www.test.com.conf

bash 复制代码
server {
    listen 80;
    server_name www.test.com;
    # 将所有 HTTP 请求重定向到 HTTPS, 如果需要强制https开启这项
    # return 301 https://$host$request_uri;
    location / {
		proxy_pass http://localhost:9000;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
	}
}

server {
    listen 443 ssl;
    server_name www.test.com;
    ssl_certificate /usr/local/etc/nginx/certs/certificate.pem;
    ssl_certificate_key /usr/local/etc/nginx/certs/certificate.key;
    location / {
        proxy_pass http://localhost:9000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

5. 验证配置并重启 Nginx

(1)验证 Nginx 配置是否正确(如果不确定配置是否正确这个命令很有效):

bash 复制代码
nginx -t

(2)如果配置正确,重启 Nginx 以应用更改:

bash 复制代码
brew services restart nginx

三、生成 ssl/自签名 证书

在你指定的文件夹中打开命令行工具

bash 复制代码
# x509 根据现有的证书请求生成自签名根证书
# -days 设置证书的有效天数
# rsa:2048 现代的 SSL/TLS 配置通常要求至少 2048 位的密钥
openssl req -newkey rsa:2048 -nodes -keyout www.test.com.key -x509 -days 365 -out www.test.com.crt

Country Name (2 letter code) [国家]:CN

State or Province Name (full name) [省份]:Beijing

Locality Name (eg, city) [城市]:Beijing

Organization Name (eg, company) [组织/公司]:test

Organizational Unit Name (eg, section) [部门/单位]:test

Common Name (eg, fully qualified host name) [域名]:www.test.com

Email Address [邮箱]:test@outlook.com

相关推荐
mgx_7185 小时前
mac下载并配置influxDB到本地
macos·influxdb
Aurora-silas11 小时前
LLM微调尝试——MAC版
人工智能·pytorch·深度学习·macos·机器学习·语言模型·自然语言处理
游戏开发爱好者811 小时前
如何系统化掌握 iOS 26 App 耗电管理,多工具协作
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_9159214313 小时前
运营日志驱动,在 iOS 26 上掌握 App 日志管理实践
android·macos·ios·小程序·uni-app·cocoa·iphone
马拉萨的春天14 小时前
iOS的多线程下数据安全和内存泄漏以及工具使用监测内存泄漏
macos·ios·cocoa·多线程
Lion Long17 小时前
PB级数据洪流下的抉择:从大数据架构师视角,深度解析时序数据库选型与性能优化(聚焦Apache IoTDB)
大数据·性能优化·apache·时序数据库·iotdb
程序员勾践19 小时前
安装nginx
linux·nginx·centos
ybb_ymm21 小时前
从需求开始至架构设计的适用于商家及小吃摊的点餐小程序
小程序·apache
2501_916007471 天前
iOS 上架技术支持全流程解析,从签名配置到使用 开心上架 的实战经验分享
android·macos·ios·小程序·uni-app·cocoa·iphone
QMY5205201 天前
深度优先遍历策略
macos·jupyter·postman