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

ops/2024/9/24 20:54:38/

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.ppmy.cn/ops/115458.html

相关文章

C++STL--------string

文章目录 一、STL介绍二、string1、constructor构造函数2、operator[]方括号运算符重载3、iterator迭代器4、reverse_iterator反向迭代器5、size和length6、capacity7、clear8、shrink_to_fit9、at10、push_back11、append 二、auto类型(C11)1、使用2、真正的价值 三、范围for(…

中间件:maxwell、canal

文章目录 1、底层原理:基于mysql的bin log日志实现的:把自己伪装成slave2、bin log 日志有三种模式:2.1、statement模式:2.2、row模式:2.3、mixed模式: 3、maxwell只支持 row 模式:4、maxwell介…

Vue3:实现div拖拽

作者:CSDN @ _乐多_ 本文记录了一个使用Vue3语法的可以对div进行拖拽,并将新的位置信息保存在本地,刷新浏览器之后位置还存在,的代码,代码即插即用。 文章目录 一、Vue3组件一、Vue3组件 <template><divclass="image-container"@mousedown="sta…

达梦数据库SCHEMA使用初探

1. schema功能描述 用户的模式(Schema&#xff09;指的是用户账号拥有的对象集&#xff0c;在概念上可将其看作是包含表、视图、索引和权限定义的对象。在 DM 中&#xff0c;一个用户可以创建多个模式&#xff0c;一个模式中的对象&#xff08;表、视图等&#xff09;可以被多…

MATLAB无线网络设计工具:从理论到实践

MATLAB作为高级数学软件&#xff0c;广泛应用于无线网络设计领域。通过内置的无线通信工具箱&#xff0c;MATLAB提供了强大的无线网络设计工具&#xff0c;支持从物理层到网络层的全面设计和仿真。本文将详细介绍MATLAB中无线网络设计工具的功能、使用方法以及实际应用案例。 …

基于 PyQt5 和 OpenCV 进行图像处理操作的GUI工具初版

为了实现一个基于 PyQt5 和 OpenCV 的图形用户界面&#xff08;GUI&#xff09;&#xff0c;要求如下&#xff1a; 左边显示加载的图片。 中间提供各种对图片进行处理的操作方法&#xff08;如灰度化、模糊处理等&#xff09;。 右边显示处理后的效果图。 接下来我将详细讲解如…

【JPCS出版】第二届应用统计、建模与先进算法国际学术会议(ASMA2024,9月27日-29)

第二届应用统计、建模与先进算法国际学术会议 2024 2nd International Conference on Applied Statistics, Modeling and Advanced Algorithms&#xff08;ASMA2024&#xff09; 会议官方 会议官网&#xff1a;www.icasma.org 2024 2nd International Conference on Applied …

ftdi_sio驱动学习笔记 2 - probe

目录 1. ftdi_quirk 2. id的特殊性 3. 将特殊操作赋值给串口的私有数据 .probe ftdi_probe, 这个接口函数是FTDI设备插入时调用的函数。程序ftdi_probe里面主要是根据不同的VID和PID设备做了不同的probe函数。这一步最大的作用是probe特殊的一些应用&#xff0c;对于普通…