Nginx 正向代理 反向代理 負載均衡(二)

Nginx

一 Nginx 安裝

下載Linux版本的Nginx壓縮包,上傳Nginx壓縮包並解壓 tar zxvf nginx-1.6.2.tar.gz  

進入Nginx文件夾

依次操作:

  1. ./configure   
  2. make  
  3. make install

可能會遇到的問題:

  1. c compiler cc is not found   缺少一個c++庫

解決:在root用戶下,進入根目錄

yum -y install gcc gcc-c++ autoconf automake

  1. the http rewrite module requires the PCRE library

解決:在root用戶下,yum -y install pcre pcre-devel

  1. the http gzip module requires the zlib library

解決:Yum -y install zlib zlib-level

二 Nginx的啓動與停止

  1. 啓動

可執行文件地址 -c  配置文件地址

或者進入 /usr/local/nginx/後 ./nginx

  1. 停止

從容停止 ps -ef|grep nginx  kill -QUIT 進程號

快速停止 kill -TERM 進程號 kill -INT 進程號

強制停止 pkill -9 nginx

./nginx -s stop

  1. 重啓

方法一:進入可執行文件文件夾之後

  1. 先驗證配置文件是否正確,進入可執行文件目錄  ./nginx -t
  2. 重啓 ./nginx -s reload

方法二:

先查出進程號 ps -ef|grep nginx , 在執行 kill -HUP 進程號

三 Nginx 配置文件解析

    1. 結構

    一個http模塊中可以有多個server,一個server中可以有多個location

    1. 映射邏輯

通過監聽端口,收集發送過來的請求,Server_name  根據 location 後的匹配路徑(其實就是項目訪問路徑) 來轉到 代理的路徑proxy_pass ,如果proxy_pass 是一個服務集羣,則需要配置upstream

三 Nginx 做代理服務器

在server模塊中

  1. 在listen 和 server_name 中分別配置 要監聽的端口和ip或域名
  2. 在location後面配置要匹配的路徑,即要訪問的項目路徑
  3. 在proxy_pass中配置要跳轉的路徑

四 Nginx 配置負載均衡--反向代理

說完上面映射邏輯和結構後,這邊就比較簡單了。主要的改動就是proxy_pass 後面的由一個upstream模塊來代替。Upstream中配置幾個節點的ip和端口。下面給一個簡單的案例:

server {
        listen       監聽的端口;
        server_name  監聽的ip地址;

        
        location /項目名稱{
            proxy_pass http://負載均衡名稱(自定義);
                                        proxy_set_header Host $host:$server_port;
                                        proxy_redirect off;
                                        proxy_set_header X-Real-IP $remote_addr;
                                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                                        proxy_connect_timeout 60;
                                        proxy_read_timeout 600;
                                        proxy_send_timeout 600;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
       
    }
upstream 負載均衡名稱(自定義) { 
       server ip1:port1;
       server ip2:port2;
       }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章