MAC使用nginx分發80至8080端口


由於項目必須要啟動80端口,但是mac系統中非root用戶無法直接使用1024以下的端口

2、釋放apache的80端口

由於Mac OS是自帶Apache服務的,它本身占用了80端口,首先你需要將Apache的監聽端口改為其他端口或者將其直接卸載,我選用的是將其端口改為8011

1
sudo vim /etc/apache2/httpd.conf

Listen 8011

改動后,重啟生效

1
sudo /usr/sbin/apachectl restart

到這里,你已經釋放了80端口

3、使用Nginx分發80端口到8080端口

  1. 安裝brew

見官網:https://brew.sh/index_zh-cn.html

  1. 使用Homebrew安裝庫

    1
    2
    brew search nginx
    brew install nginx
  2. 安裝好了后,修改配置

    1
    sudo vim /usr/local/etc/nginx/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
server {
listen 80;
server_name localhost l.sogou.com;


< 大專欄  MAC使用nginx分發80至8080端口/span>
#access_log logs/host.access.log main;

location ~* ^/h5/{
proxy_pass http://127.0.0.1:8091;
}

location ~* ^/weixin/{
proxy_pass http://127.0.0.1:8093;
}

location ~* ^/api/{
proxy_pass http://127.0.0.1:8087;
}

location / {
root html;
index index.html index.htm;
proxy_pass http://127.0.0.1:8080;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server下的結點:
listen:監聽80端口
server_name:轉發到哪個地址
proxy_pass:代理到哪個地址

  1. Nginx開機啟動

你需要了解的就是plist文件。plist就是property list format的意思,是蘋果用來保存應用數據的格式,其實就是個xml。
可以在/usr/local/opt/nginx 下找到nginx對應的plist文件,比如在作者電腦上是 homebrew.mxcl.nginx.plist 。

需要把這個文件復制到 /Library/LaunchDaemons 下,系統啟動時啟動。
也可以復制到 /Library/LaunchAgents下,在用戶登錄時啟動。
接着執行launchctl load -w,如下:

1
2
3
sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons

sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

最后,重啟你的機器,你會發現nginx在80端口啟動了,試着通過http://localhost直接訪問

  1. 修改配置 重啟生效
    1
    2
    3
    4
    sudo vim /usr/local/etc/nginx/nginx.conf

    cd /usr/local/opt/nginx/bin/
    sudo ./nginx -s reload


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM