例如,有两个目录,一个目录下是前端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; } }