当前位置: 首页 > news >正文

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

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

一、禁用自带Apache

1.关闭Apache

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配置目录:

vim /etc/apache2/httpd.conf

(2) 搜索:#ServerName www.example.com:80
        增加一行 ServerName localhost:80
(3) 重新启动Apache

sudo apachectl restart

(4) 关闭Apache

sudo apachectl -k stop

2.禁止apache自启动

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

二、使用 Homebrew 安装 Nginx

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

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:

brew install nginx

3. 启动和管理 Nginx

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

# 启动 Nginxbrew services start nginx# 停止 Nginxbrew services stop nginx# 重启 Nginxbrew 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

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 配置是否正确(如果不确定配置是否正确这个命令很有效):

nginx -t

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

brew services restart nginx

三、生成 ssl/自签名 证书

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

# 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

http://www.lryc.cn/news/444789.html

相关文章:

  • AlmaLinux 安裝JDK8
  • Set 和 Map 的模拟实现
  • 深度学习自编码器 - 预测稀疏分解(PSD)篇
  • 如何检测出来这个ip是共享ip不安全
  • TMStarget学习——T1 Segmentation数据处理及解bug
  • 锁策略, cas 和 synchronized 优化过程
  • 【HTML5】html5开篇基础(2)
  • 大数据新视界 --大数据大厂之 Reactjs 在大数据应用开发中的优势与实践
  • 【论文阅读笔记】TOOD: Task-aligned One-stage Object Detection
  • 类中的特殊内容
  • network request to https://registry.npmjs.org/xxx failed, reason: connect ETIM
  • MQ入门(二):java客户端SpringAMQP
  • 软技能与AI技术的融合
  • 在视频上绘制区域:使用Vue和JavaScript实现交互式画布
  • 31. RabbitMQ顺序消费
  • BERT-BiLSTM-CRF模型实战
  • npm 安装 与 切换 淘宝镜像
  • 在Windows系统上安装的 Arrow C++ 库
  • 格雷母线电缆头安装方法视频-武汉正向科技
  • 统信服务器操作系统【Cron定时任务服务】
  • 微前端中的路由加载流程
  • Axure大屏可视化模板:跨领域数据分析平台原型案例
  • 机器学习(1)——线性回归、线性分类与梯度下降
  • 完整的端到端的中文聊天机器人
  • 【有啥问啥】Stackelberg博弈方法:概念、原理及其在AI中的应用
  • 【UI自动化】前言
  • Unity对象池的高级写法 (Plus优化版)
  • vue3<script setup>中computed
  • 【已解决】使用JAVA语言实现递归调用-本关任务:用循环和递归算法求 n(小于 10 的正整数) 的阶乘 n!。
  • BiRefNet 教程:基于 PyTorch 实现的双向精细化网络