例如,有兩個目錄,一個目錄下是前端html文件,服務監聽的端口是8001;另外一個是後端nodejs文件,服務監聽的是8000端口。
當瀏覽器訪問 localhost:8888, 而後被nginx 監聽後,若是匹配到localhost:8888/...,直接會代理到 8001 端口 html 文件中;若是匹配到localhost:8888/api/...,則會代理到 8000端口的node.js文件中。html
打開:/usr/local/etc/nginx/nginx.conf前端
而後在 nginx 的 http 模塊上添加一個 servernode
server { listen 8888; location / { proxy_pass: http://localhost:8001; } location /api/ { proxy_pass: http://localhost:8000; proxy_set_header Host $host; } }