天天看點

nginx配置代理多個上遊服務

項目有開發環境和測試環境,兩個環境都用nginx代理靜态網頁,用nginx代理端口轉發請求道真正的後端服務。并且這是兩套完全不同的環境。隻是nginx軟體部署到了一個機器上,是以要用nginx代理開發請求到一個端口,代理測試環境請求到另一個端口。那這就需要nginx監聽兩個端口了。沒配置之前,我做過如下配置測試:

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '


    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream rov{
        server 192.168.199.31:8801;
    }    
    upstream rov2{
        server 192.168.199.31:8802;
    } 

    server {
        listen       8800;
        server_name  localhost; 

        location ~/static/ {
            root   D:/software/nginx-1.14.2;
        }

       location /rest/v1/rov {
            client_max_body_size 200M;
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
              
            if ($request_method = 'OPTIONS' ) {
                 return 204;
            }
            
            
            proxy_pass http://rov;
            proxy_connect_timeout 6000;
            proxy_send_timeout 3000;
            proxy_read_timeout 6000;
        }
        

        
    } 
	
	 

    server {
        listen       8900;
        server_name  localhost2; 

        location ~/static_test_env/ {
            root   D:/software/nginx-1.14.2;
        }

       location /rest/v1/rov {
            client_max_body_size 200M;
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
              
            if ($request_method = 'OPTIONS' ) {
                 return 204;
            }
            
            
            proxy_pass http://rov2;
            proxy_connect_timeout 6000;
            proxy_send_timeout 3000;
            proxy_read_timeout 6000;
        }
        

        
    } 
    
      

}
           

這樣配置其實沒用,請求都被轉發到upstream為rov的那個上遊服務了。我嘗試把rov2這個上遊服務去掉,然後第二個server的代理請求位址直接改為ip端口号就可以正常轉發請求了,配置如下:

#user  nobody;
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" '


    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream rov{
        server 192.168.199.31:8801;
    }    

    server {
        listen       8800;
        server_name  localhost; 

        location ~/static/ {
            root   D:/software/nginx-1.14.2;
        }

       location /rest/v1/rov {
            client_max_body_size 200M;
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
              
            if ($request_method = 'OPTIONS' ) {
                 return 204;
            }
            
            
            proxy_pass http://rov;
            proxy_connect_timeout 6000;
            proxy_send_timeout 3000;
            proxy_read_timeout 6000;
        }
        

        
    } 
	
	 

    server {
        listen       8900;
        server_name  localhost2; 

        location ~/static_test_env/ {
            root   D:/software/nginx-1.14.2;
        }

       location /rest/v1/rov {
            client_max_body_size 200M;
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
              
            if ($request_method = 'OPTIONS' ) {
                 return 204;
            }
            
            
            proxy_pass http://192.168.199.31:8802;
            proxy_connect_timeout 6000;
            proxy_send_timeout 3000;
            proxy_read_timeout 6000;
        }
        

        
    } 
    
      

}


           

這樣配置後,nginx監聽8800和8900端口,8800端口為接收開發環境請求的,8900為接收測試環境請求的,所代理的後端服務也是兩個應用連兩個不同的資料庫,互不幹擾。但是為什麼不能配置兩個上遊upstream,誰知道什麼原因?