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: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;
    }
}
相关文章
相关标签/搜索