天天看點

Nginx多套環境配置及常見路由政策

一、配置nginx

1、進入 nginx\conf 目錄下,用文本編輯器打開 nginx.conf,對配置檔案進行如下修改:

(1) 在 http 中增加 upstream,并配置兩台環境的位址;

#有幾套環境,就配置幾條
 upstream pcservice{
        server 10.101.137.244:8224 ;            #節點1 
        server 10.101.137.245:8081;             #節點2 
}	
           
http {
    #增加 upstream 的配置,其中 myserver 是自己起的名字
    upstream myserver{
	     server www.jintianxuesha.com 127.0.0.1:8088;  #有幾套環境,就配置幾條
	     server  www.yuanyyleezc.cn   127.0.0.1:8089;
    }
}
           

示例:

#user  ;
worker_processes  2;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid         logs/nginx.pid;
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    
    proxy_set_header            X-Real-IP $remote_addr;
    proxy_set_header   x-forwarded-for $proxy_add_x_forwarded_for;
    
    sendfile        on;
    keepalive_timeout  65;
    client_max_body_size 8M;
    client_body_buffer_size 128k;
    client_header_buffer_size 128k;
    #add_header X-Frame-Options DENY;
    server_tokens off;
	
#後端負載均衡配置

#兩套服務,pcservice可以自己命名
upstream pcservice{
        server 10.101.137.244:8224 ;  #節點1  ip和端口
        server 10.101.137.245:8224 ;  #節點2  ip和端口
    }	

#pc-web應用環境配置
        server {
        listen       6005; #通路端口配置
        server_name  localhost;
	    add_header X-Frame-Options SAMEORIGIN;
	    add_header Strict-Transport-Security max-age=63072000;
	    add_header Content-Security-Policy "default-src 'self' at.alicdn.com data:; 
        style-src 'self' 'unsafe-inline'; worker-src blob:; media-src blob:";
	    add_header Referrer-Policy strict-origin-when-cross-origin;
        location / {
		    add_header Strict-Transport-Security max-age=63072000;
		    add_header Content-Security-Policy "default-src 'self' at.alicdn.com data:;         
            style-src 'self' 'unsafe-inline'; worker-src blob:; media-src blob:";
		    add_header Referrer-Policy strict-origin-when-cross-origin;
	        add_header Cache-Control 'no-cache, must-revalidate, proxy-revalidate, max-age=0';
            add_header X-Frame-Options SAMEORIGIN;
	        root   /home/test/pc-web/pc; #前端代碼路徑
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        
        }
		
        #後端服務配置
        location /pc-service {
            proxy_pass  http://pcservice/pc-service; 
        }
        
        #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;
        }
	    location ^~/vms/ {
            proxy_pass  http://10.109.55.33:83/;
        }


    }
}
           

二、Nginx常見的路由政策

1. 輪詢法

最簡單的輪詢法,多餘的配置不需要增加。

upstream myserver{
   server 127.0.0.1:8088;  # 有幾套環境,就配置幾條
   server 127.0.0.1:8089;
}
           

2. 源位址哈希法

使用 ip_hash 關鍵字,每一個請求,都按 hash(IP) 的結果決定通路哪台伺服器;

upstream myserver{
   ip_hash; # hash(IP)
   server 127.0.0.1:8088;  # 有幾套環境,就配置幾條
   server 127.0.0.1:8089;
}
           

如果我們本地測試,多次通路接口,可以發現請求永遠落到同一個服務上,因為本地 IP 一直沒有改變。

3. 權重輪詢法

使用 weight 關鍵字,設定每台伺服器的權重;

upstream myserver{
   server 127.0.0.1:8088 weight=1;  # 20% 請求會發給8088
   server 127.0.0.1:8089 weight=4;
}