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;
    }
}
相關文章
相關標籤/搜索