nginx配置:反向代理和負載均衡

 

參考博客:https://blog.csdn.net/xuanjiewu/article/details/79458266

一:反向代理與正向代理

      正向代理:直接獲取目標服務器數據(瀏覽器輸入a.com直接獲取甲網頁)

      反向代理:通過代理服務器獲取目標服務器數據(通過b.com也能獲取一個網頁,因爲b.com去訪問a.com,

                                     所以我們輸入b.com表面上訪問的B,實際獲取一個的數據)

server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass              http://luckwml.top:80;     
            proxy_redirect          off;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    1.配置:

       這是我的CONF / nginx.conf配置文件中的服務器(可以配置多個)

   2.啓動

       窗口啓動的nginx的,可以雙擊nginx.exe

   3.訪問

       在瀏覽器地址欄輸入:  http:// localhost:8080 /

       將會看到  http://luckwml.top:80  所訪問的頁面

二:負載均衡

       爲了避免大量用戶同時訪問一臺機器,採用負載均衡,比如將請求分發在兩臺機器上。

     

    upstream  aa{
      server 48.98.52.51:80;
      server 48.98.52.52:80;
    }
    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass              http://aa;
            proxy_redirect          off;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

   1.配置

       如圖所示,和反向代理比較可以很容易發現,多了upstream 配置,server就是需要分發的服務器,

       這裏假設兩臺服務器48.98.52.51,48.98.52.52

   2.啓動(參考反向代理)

   3.訪問

       在瀏覽器地址欄輸入:  http:// localhost:8080 /

       將會看到48.98.52.51:80或48.98.52.52:80 訪問的頁面,一直訪問將會輪詢這兩臺服務器。

---------------------------------------------------------------------------------------------------------------------------------------

這是我對nginx的的的簡單瞭解,如有理解不正確的地方,非常感謝能夠指點一下,謝謝!

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