proxy_pass和fastcgi_pass區別,反向代理,負載均衡、nginx+fastcgi+php的併發阻塞問題

目錄

1、準備兩個tomcat和一個nginx

1.1、tomcat: http://127.0.0.1:8080

1.2、tomcat:http://127.0.0.1:8081

1.3、nginx:http://127.0.0.1

2、反向代理proxy_pass

2.1、反向代理

2.2、反向代理-使用upstream

2.3、反向代理-負載均衡-輪循模式

2.4、反向代理-負載均衡-權重模式

2.4、反向代理-負載均衡-ip_hash方式

3、網關代理fastcgi_pass(php+nginx配合)

3.1、php安裝(本人安裝的是php-7.3.1)

3.1.1、php下載

3.1.2、php配置

3.2、開啓cgi端口

3.3、fastcgi_passnginx配置

3.3.1、新建文件夾:E:\php\www  。然後創建兩個文件

3.3.2、創建文件index.php

3.3.3、創建文件index.html

3.3.4、修改nginx配置

3.4、調試頁面截圖

3.5、解決 nginx+fastcgi+php的併發阻塞問題

3.5.1、啓動多個cgi端口(9000,9001端口)

3.5.2、nginx配置

3.5.3、測試

4、不同項目地址轉發

4.1、在location層轉發

4.2、在location裏面判斷轉發


1、準備兩個tomcat和一個nginx

 

 

1.1、tomcat: http://127.0.0.1:8080

 

 

1.2、tomcat:http://127.0.0.1:8081

 

 

1.3、nginx:http://127.0.0.1

 

 

 

2、反向代理proxy_pass

官方文檔;http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

 

2.1、反向代理

介紹:127.0.0.1的80端口轉發到127.0.0.1:8080. 。在這裏127.0.0.1就類似於服務器的ip地址同理。

 server {
        listen      80;
        server_name localhost www.xxx.com; #域名
        location / {
            proxy_pass http://127.0.0.1:8080;
            client_max_body_size 1024m; #請求體大小設置
        }
      }

效果展示

 

2.2、反向代理-使用upstream

  upstream tomcat{    
        server 127.0.0.1:8081;
    }

    server {
        listen      80;
        server_name localhost www.xxx.com; #域名
        location / {
            proxy_pass http://tomcat;
            client_max_body_size 1024m; #請求體大小設置
        }
      }

 

2.3、反向代理-負載均衡-輪循模式

輪詢方式是默認方式,每個請求按照時間順序逐一分配到不同的後端服務器進行處理,如有服務器宕機會被自動剔除。

#  負載均衡-輪循模式
    upstream tomcat{    
        server 127.0.0.1:8080;
        server 127.0.0.1:8081;
    }

    server {
        listen      80;
        server_name localhost www.xxx.com; #域名
        location / {
            proxy_pass http://tomcat;
            client_max_body_size 1024m; #請求體大小設置
        }
      }

訪問的時候刷新,會發現每次都不一樣。爲輪循

 

2.4、反向代理-負載均衡-權重模式

權重方式是利用weight指定的權重比率,與訪問率成正比。用於後端服務器性能不均的情況。

#  weigth參數表示權值,權值越高則被分配到的概率越大。測試的時候發現weight=3,刷新的時候8080端口就刷出3次

#  max_fails允許請求失敗的次數默認爲l

#  fail_timeoutq請求失敗後暫停服務的時間 

#  backup預留的備份機器

#  down表示當前的server暫時不參與負載均衡。

#  weigth參數表示權值,權值越高則被分配到的概率越大。測試的時候發現weight=3,刷新的時候8080端口就刷出3次
#  max_fails允許請求失敗的次數默認爲l
#  fail_timeoutq請求失敗後暫停服務的時間 
#  backup預留的備份機器
#  down表示當前的server暫時不參與負載均衡。 
upstream tomcat{    
        #weigrt權重值(越大訪問率大),在fail_timeout時間內檢查後端服務器max_fails次,失敗則被剔除;
        server 127.0.0.1:8080 weight=3 fail_timeout=30s max_fails=2;
        server 127.0.0.1:8081 weight=1 fail_timeout=30s max_fails=2;
    }

    server {
        listen      80;
        server_name localhost www.xxx.com; #域名
        location / {
            proxy_pass http://tomcat;
            client_max_body_size 1024m; #請求體大小設置
        }
      }

2.4、反向代理-負載均衡-ip_hash方式

ip_hash方式是按每個請求訪問IP的hash結果分配,可以使每個訪客固定訪問一個後端服務器,可以解決Session共享的問題。

一般情況下不太建議使用這種方式

# ip_hash;
 upstream tomcat{  
        ip_hash;      
        server 127.0.0.1:8080  fail_timeout=5s ;
        server 127.0.0.1:8081   fail_timeout=5s  ;
    }

    server {
        listen      80;
        server_name localhost www.xxx.com; #域名
        location / {
            proxy_pass http://tomcat;
            client_max_body_size 1024m; #請求體大小設置
        }
      }

 

 

 



 


3、網關代理fastcgi_pass(php+nginx配合)

官方文檔:    http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_pass

    fastcgi介紹:CGI全稱是“公共網關接口”(Common Gateway Interface),HTTP服務器與你的或其它機器上的程序進行“交談”的一種工具,其程序一般運行在網絡服務器上。 CGI可以用任何一種語言編寫,只要這種語言具有標準輸入、輸出和環境變量。如php,perl,tcl等。

這種模式一般和php配合一起使用所以我們首先要安裝php

 

3.1、php安裝(本人安裝的是php-7.3.1)

3.1.1、php下載

進入PHP下載地址http://windows.php.net/download下載最新線程安全版PHP zip壓縮包,解壓縮後放在想要安裝的路徑下。注意:下載的PHP VC版本不能比前面安裝的vc redist版高。

3.1.2、php配置

進入PHP安裝目錄,複製一份php.ini-development改名爲php.ini放到安裝路徑下,打開找到"extension_dir",去掉註釋符,將值改爲"PHP安裝路徑\ext"。我當前的是:extension_dir = "E:\php\php-7.3.1\ext"

如圖:

 

3.2、開啓cgi端口

//  命令,在控制檯執行
E:\php\php-7.3.1\php-cgi.exe -b 127.0.0.1:9000  -c E:\php\php-7.3.1\php.ini

如圖:

 

 

3.3、fastcgi_passnginx配置

3.3.1、新建文件夾:E:\php\www  。然後創建兩個文件

3.3.2、創建文件index.php

<?php
echo '<strong>Hello, this is index.php!</strong>';
?>

3.3.3、創建文件index.html

this is  index html

 

3.3.4、修改nginx配置

server {
        listen       7000;
        server_name  localhost;
        location / {
            ## 解決跨域的代碼
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Headers X-Requested-With;
            add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
            #創建的包含 index.html和 index.php的文件夾
            root   E:/php/www;
            index  index.html index.htm index.php;
            try_files $uri $uri/ /index.php?$query_string;
        }
   
        # 判斷後綴爲php的地址
        location ~ \.php$ {
            #創建的包含 index.html和 index.php的文件夾
            root           E:/php/www;
            fastcgi_pass   127.0.0.1:9000; #端口是上面開通的cgi端口
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        error_page   500 502 503 504  /50x.html; 
    }

重啓nginx:nginx.exe -s reload

 

3.4、調試頁面截圖

訪問:http://127.0.0.1:7000/

訪問:http://127.0.0.1:7000/index.php 

 

3.5、解決 nginx+fastcgi+php的併發阻塞問題

3.5.1、啓動多個cgi端口(9000,9001端口)

命令:E:\php\php-7.3.1\php-cgi.exe -b 127.0.0.1:9000  -c E:\php\php-7.3.1\php.ini

3.5.2、nginx配置

然後重啓nginx:E:\php\nginx-1.18.0>  nginx.exe -s reload

 upstream fastcgi_proxy{
        server 127.0.0.1:9000;
        server 127.0.0.1:9001; 
    }

     server {
        listen       7000;
        server_name  localhost;
        location / {
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Headers X-Requested-With;
            add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
            root   E:/php/www;
            index  index.html index.htm index.php;
            try_files $uri $uri/ /index.php?$query_string;
        }
   
        location ~ \.php$ {
            root           E:/php/www;
            fastcgi_pass   fastcgi_proxy;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            client_max_body_size    10m;
        }
        error_page   500 502 503 504  /50x.html; 
    }

 

3.5.3、測試

經過測試發現,9000端口和9001端口只要有一個還在啓動,http://127.0.0.1:7000/index.php 這個地址就可以訪問

 

如由於需要相關軟件和資料,歡迎留言

 

 

4、不同項目地址轉發

4.1、在location層轉發

 server {
        listen 8088;
        server_name localhost;
        location ^~ /crm {
            proxy_pass http://127.0.0.1:8080/crm;
        }
        location ^~ /resourcesSystem {
            proxy_pass http://127.0.0.1:8080/resourcesSystem;
         }
	    location ^~ /sw_agency {
            proxy_pass http://127.0.0.1:8080/sw_agency;
        }
       
    }

4.2、在location裏面判斷轉發

 server {
        listen 8088;
        server_name localhost;         
        location / {
            proxy_http_version 1.1;
	    proxy_set_header Upgrade $http_upgrade;
	    proxy_set_header Connection "Upgrade";
            
            root html;
            index index.html index.htm;

            if ($request_uri ~* "^\/springboot"){
                    # add_header Access-Control-Allow-Origin *;
                    # add_header Access-Control-Allow-Headers X-Requested-With,content-type,cert,t;
                    # add_header Access-Control-Allow-Methods GET,POST,OPTIONS;            
                    proxy_pass http://127.0.0.1:8080;break;
            }            
        }
    }

 

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