1. 程式人生 > >淺談nginx 反向代理

淺談nginx 反向代理

一、nginx科普:

Nginx (engine x) 是一個高效能的HTTP反向代理伺服器,也是一個IMAP/POP3/SMTP伺服器。Nginx是由伊戈爾·賽索耶夫為俄羅斯訪問量第二的Rambler.ru站點(俄文:Рамблер)開發的,第一個公開版本0.1.0釋出於2004年10月4日。其將原始碼以類BSD許可證的形式釋出,因它的穩定性、豐富的功能集、示例配置檔案和低系統資源的消耗而聞名。2011年6月1日,nginx 1.0.4釋出。Nginx是一款輕量級Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,並在一個BSD-like 協議下發行。其特點是佔有記憶體少,併發能力強,事實上nginx的併發能力確實在同類型的網頁伺服器中表現較好,中國大陸使用nginx網站使用者有:百度、
京東
新浪網易騰訊淘寶等。

上面的資訊來源於百度百科

二、反向代理:

根據反向代理的書寫格式,我把反向代理歸類為 :埠反向代理,路徑反向代理。

環境  window nginx(埠8080)  phpstudy(埠80) 

1)路徑反向代理:

worker_processes 4; 
error_log  logs/error.log debug;

worker_rlimit_nofile 51200;

events {
    worker_connections  51200;
}
http {
    server {
        listen      8080;
		
        location / {
            root html;
        }
		
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root html;
        }
	location /xiaoyi/ {
	 	index index.php index.html login.php;
	 	proxy_pass  http://127.0.0.1:80/; 
         } 
} }

描述:上面nginx配置檔案監控8080埠 ;定義了一個路徑反向代理xiaoyi 當我們方位http://127.0.0.1/8080/xiaoyi/連結時,實際上是訪問的是http://127.0.0.1:80/的連結



2)埠反向代理

直接上配置檔案:

worker_processes 4;  

#切換自動推送(多 worker 直播流)模式。預設為 off
rtmp_auto_push on;

#當 worker 被幹掉時設定自動推送連線超時時間。預設為 100 毫秒
rtmp_auto_push_reconnect 1s;

error_log  logs/error.log debug;

worker_rlimit_nofile 51200;

events {
 
    worker_connections  51200;
}

rtmp {
    server {
        listen 1935;

        application live {
            #live on;
		live on;
		hls on;
		hls_path temp/hls;
          	hls_fragment 20s;
          	hls_playlist_length 3000s;
          	hls_cleanup off;
		idle_streams off;
		publish_notify on;
		drop_idle_publisher 70s;
	  	record_unique off;
		#record keyframes;  
		wait_video off;
		interleave on;
		wait_key off;
		record all;
		record_path temp/hls;  
		record_interval 3600s;  
	
        }
		
        application hls {
            live on;
            hls on;  
            hls_path temp/hls;  
            hls_fragment 1s;  
        }
    }
}

http {
    server {
        listen      8080;
		
        location / {
            root html;
        }
		
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root html;
        }
		
        location /hls {  
            #server hls fragments  
            types{  
                application/vnd.apple.mpegurl m3u8;  
                video/mp2t ts;  
            }  
            alias temp/hls;  
            expires -1;  
        } 
	location /xiaoyi/ {
	 	index index.php index.html login.php;
	 	proxy_pass  http://127.0.0.1/; 
         } 
    }
    include vhost/*.conf;
}

為了方便管理主機,我把主機配置檔案全部放在了單獨的目錄下include方式引入入配置檔案


內容為:

#xiaoyi
server{
listen  8081;
server_name 127.0.0.1 localhost; 
charset utf-8;
location =/ {
rewrite / /index.html permanent;
}
location / {
proxy_pass  http://127.0.0.1:80/;
proxy_redirect  default;
}
location ~* ^.+.(ico|gif|jpg|jpeg|png|html)$ {
root  D:\phpStudy\WWW;
}
location ~* ^.+.(css|js|txt|xml|swf|wav|json)$ {
root  D:\phpStudy\WWW; 
}
}

訪問效果: