Nginx 常用命令并实现最基本的反向代理

nginx 命令

  • 测试配置文件格式是否正确:$ nginx -t
  • 启动:nginx
  • 重启:nginx -s reload
  • 获取nginx进程号: ps -ef|grep nginx
  • 停止进程(master): Kill -TERM 22649(进程号)
  • 关闭: nginx -s quit (优雅停止)
  • 关闭: nginx -s stop (立即停止)

nginx 反向代理(Mac os下)

例如,有两个目录,一个目录下是前端html文件,服务监听的端口是8001;另一个是后端nodejs文件,服务监听的是8000端口。
当浏览器访问 localhost:8888, 然后被nginx 监听后,如果匹配到localhost:8080/...,直接会代理到 8081 端口 html 文件中;如果匹配到localhost:8080/api/...,则会代理到 8000端口的node.js文件中。

打开:/usr/local/etc/nginx/nginx.conf

然后在 nginx 的 http 模块上添加一个 server

 server {
    listen        8888;

    location / {
        proxy_pass: http://localhost:8001;
    }

    location /api/ {
        proxy_pass: http://localhost:8000;
        proxy_set_header Host $host;
    }
}

猜你喜欢

转载自www.cnblogs.com/cckui/p/10972749.html