nginx 安裝與反向代理測試 under MAC

安裝

在 Mac 下能夠直接使用 homebrew 安裝 nginxhtml

brew search nginx

brew install nginx

啓動 nginx: sudo nginx,訪問 8080 應能看到歡迎界面nginx

nginx -V 查看 nginx 的啓動參數,配置文件的位置默認是  app

--conf-path=/usr/local/etc/nginx/nginx.conf 
 
#從新加載配置|重啓|中止|退出 nginx
nginx -s reload|reopen|stop|quit 
 

反向代理 

 
假設但願別人輸入 localhost:8080 時直接跳轉到 localhost:2014,那麼在 nginx.conf 中配置
 
http {
  server {
    listen 8080;

    location / {
      proxy_pass http://127.0.0.1:2014;
    }
  }
}

 

負載均衡加反向代理

myapp1 是一組網頁的集合,這裏做爲一個變量負載均衡

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 8080;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

  

注意:

1. location 後面的東西是一個 uri 地址,/ 的話表示根目錄的請求直接跳轉到 myapp1 上,固然 uri 還能夠更長一些,好比 /name 之類的,具體怎麼替換還要結合 proxy_pass 來處理,這裏分爲兩種狀況ui

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:代理

location /name/ {
    proxy_pass http://127.0.0.1/remote/;
}

If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:code

location /some/path/ {
    proxy_pass http://127.0.0.1;
}

 

參考orm

1. http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_passserver

2. https://www.nginx.com/blog/load-balancing-with-nginx-plus-part2/?_ga=1.269115441.1947437472.1438069067xml

相關文章
相關標籤/搜索