Mac使用Nginx設(shè)置代理并禁用自帶Apache的問(wèn)題記錄
Mac自帶Apache服務(wù),并占用80端口,如果需要使用Nginx,需要禁用Apache并自己安裝Nginx
一、禁用自帶Apache
1.關(guān)閉Apache
sudo apachectl -k stop
如果出現(xiàn)如下報(bào)錯(cuò):
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) 打開(kāi)apache配置目錄:
vim /etc/apache2/httpd.conf
(2) 搜索:#ServerName www.example.com:80
增加一行 ServerName localhost:80
(3) 重新啟動(dòng)Apache
sudo apachectl restart
(4) 關(guān)閉Apache
sudo apachectl -k stop
2.禁止apache自啟動(dòng)
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist
二、使用 Homebrew 安裝 Nginx
在 macOS 上使用 Homebrew 安裝 Nginx 時(shí),Nginx 的默認(rèn)安裝目錄通常是 /usr/local/Cellar/nginx/。具體的安裝路徑可以通過(guò)以下命令查看:
brew --prefix nginx
1. Nginx 配置文件和目錄
安裝完成后,Nginx 的主要配置文件和目錄通常位于以下位置:
- 主配置文件:
/usr/local/etc/nginx/nginx.conf - 站點(diǎn)配置文件:
/usr/local/etc/nginx/servers/ - 日志文件:
/usr/local/var/log/nginx/ - HTML 文件:
/usr/local/var/www/
2. 安裝 Nginx
使用 Homebrew 安裝 Nginx:
brew install nginx
3. 啟動(dòng)和管理 Nginx
安裝完成后,可以使用以下命令啟動(dòng)、停止和重啟 Nginx:
# 啟動(dòng) 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/ 目錄中添加新的站點(diǎn)配置文件。
例如,創(chuàng)建一個(gè)新的站點(diǎn)配置文件 /usr/local/etc/nginx/servers/www.test.com.conf:
server {
listen 80;
server_name www.test.com;
# 將所有 HTTP 請(qǐng)求重定向到 HTTPS, 如果需要強(qiáng)制https開(kāi)啟這項(xiàng)
# 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. 驗(yàn)證配置并重啟 Nginx
(1)驗(yàn)證 Nginx 配置是否正確(如果不確定配置是否正確這個(gè)命令很有效):
nginx -t
(2)如果配置正確,重啟 Nginx 以應(yīng)用更改:
brew services restart nginx
三、生成 ssl/自簽名 證書
在你指定的文件夾中打開(kāi)命令行工具
# x509 根據(jù)現(xiàn)有的證書請(qǐng)求生成自簽名根證書 # -days 設(shè)置證書的有效天數(shù) # rsa:2048 現(xiàn)代的 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) [國(guó)家]: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
到此這篇關(guān)于Mac使用Nginx設(shè)置代理,并禁用自帶Apache的文章就介紹到這了,更多相關(guān)Mac使用Nginx內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mac環(huán)境Nginx配置和訪問(wèn)本地靜態(tài)資源的實(shí)現(xiàn)
- Nginx在MAC上的安裝、啟動(dòng)、重啟和關(guān)閉
- Mac中使用Nginx實(shí)現(xiàn)80端口轉(zhuǎn)發(fā)8080端口
- Mac下Nginx安裝環(huán)境配置詳解
- 詳解Asp.Net Core 發(fā)布和部署( MacOS + Linux + Nginx )
- 在Mac OS上編譯安裝Nginx+PHP+MariaDB開(kāi)發(fā)環(huán)境的教程
- 在Mac OS上搭建Nginx+PHP+MySQL開(kāi)發(fā)環(huán)境的教程
- mac 下 安裝nginx的方法小結(jié)
- mac下安裝nginx和php
相關(guān)文章
Nginx(自定義/預(yù)定義)變量,alias虛擬目錄解讀
這篇文章主要介紹了Nginx(自定義/預(yù)定義)變量,alias虛擬目錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-07-07
通過(guò)Nginx的proxy_set_header設(shè)置請(qǐng)求頭無(wú)效的解決
這篇文章主要介紹了通過(guò)Nginx的proxy_set_header設(shè)置請(qǐng)求頭無(wú)效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Nginx報(bào)404錯(cuò)誤的詳細(xì)解決方法
最近打開(kāi)網(wǎng)站看看結(jié)果提示:nginx 404 not found,所以這篇文章主要給大家介紹了關(guān)于Nginx報(bào)404錯(cuò)誤的簡(jiǎn)單解決方法,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
Nginx服務(wù)器中限制連接數(shù)與限制請(qǐng)求的模塊配置教程
這篇文章主要介紹了Nginx服務(wù)器中限制連接數(shù)與限制請(qǐng)求的模塊配置教程,分別為limit conn模塊與limit req模塊的相關(guān)用法講解,需要的朋友可以參考下2016-01-01
Nginx?請(qǐng)求超時(shí)的實(shí)現(xiàn)
Nginx請(qǐng)求超時(shí)是服務(wù)器無(wú)法在規(guī)定時(shí)間內(nèi)完成對(duì)客戶端請(qǐng)求的響應(yīng),本文就來(lái)介紹一下Nginx?請(qǐng)求超時(shí)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02
Nginx反向代理proxy_cache_path directive is not allowed錯(cuò)誤解決方法
這篇文章主要介紹了Nginx反向代理proxy_cache_path directive is not allowed錯(cuò)誤解決方法,需要的朋友可以參考下2015-04-04

