1. 程式人生 > >nginx配置域名、反向代理、負載均衡

nginx配置域名、反向代理、負載均衡

監聽不同的埠

nginx預設商品為80埠,如果配置檔案中有一個server服務,如果需要多個把下面server複製多個就可以,監聽不同的埠更改相應配置即可

server {
        listen       80;  # 監聽埠號,預設80
        server_name  localhost;   # 監聽IP

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {     # 返回根目錄
            root   html;    # 返回根目錄下的html目錄
            index  index.html index.htm;  # 顯示相應的html
        }
        
        error_page   500 502 503 504  /50x.html;  # 監聽錯誤資訊
        location = /50x.html {
            root   html;
        }
        
    }

監聽不同的域名

假設現在www.test.com和www.test1.com 都需要訪問同一個html,我們需要把兩個域名對映到同一個html檔案。
需要在本地host檔案最下方新增:

IP地址   www.test1.com
IP地址   www.test.com

www.test.com名域名,當在瀏覽中輸入www.test.com會自動找相應IP地址。

修改nginx配置檔案,把nginx安裝目錄下的html再複製一份,修改一下html顯示內容。

# 監聽www.test.com域名
server {
        listen       80;  # 監聽埠號,預設80
        server_name  www.test.com;   # 改為監聽的域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {     # 返回根目錄
            root   html-test;    # 返回根目錄下的html目錄
            index  index.html index.htm;  # 顯示相應的html
        }
        
        error_page   500 502 503 504  /50x.html;  # 監聽錯誤資訊
        location = /50x.html {
            root   html;
        }
        
    }
    
# 監聽www.test1.com域名
server {
        listen       80;  # 監聽埠號,預設80
        server_name  www.test1.com;   # 改為監聽的域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {     # 返回根目錄
            root   html-test1;    # 返回根目錄下的html目錄
            index  index.html index.htm;  # 顯示相應的html
        }
        
        error_page   500 502 503 504  /50x.html;  # 監聽錯誤資訊
        location = /50x.html {
            root   html;
        }
        
    }

在瀏覽中輸入www.test.com和www.test1.com看到的是兩個不同的html。

反向代理

同樣在host檔案配置域名和IP對映關係,例如:
IP地址 www.sina.com
啟動tomcat埠為預設的8080

upstream sina{ # 地址為tomcat中的地址
		server IP:8080;
	}
	server {
        listen       80;
        server_name  www.sina.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://sina; # 他會找upstream 的sina中的地址
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }

當我們輸入www.sina.com時候它就找“proxy_pass http://sina;”,然後又去upstream的sina中找最終地址,頁面跳轉到tomcat下指定的頁面。

負載均衡

在反向代理已經完成的前提下更改,現在sina有兩臺伺服器,我們輸入域名該怎麼跳轉呢?
只需要在upstream中再加一個server就可以了

upstream sina{ # 地址為tomcat中的地址
		server IP:8080 weight=2; # 權重(預設為1,每個伺服器輪流訪問),權重越大訪問越多
		server IP:8081; # 負載均衡
	}
	server {
        listen       80;
        server_name  www.sina.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://sina; # 他會找upstream 的sina中的地址
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }