内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

怎么在Nginx中實現(xiàn)反向代理并支持長連接

怎么在Nginx中實現(xiàn)反向代理并支持長連接?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)長期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為安多企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè),安多網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

前言

Nginx upstream與后端的連接默認(rèn)為短連接,通過HTTP/1.0向后端發(fā)起連接,并把請求的"Connection" header設(shè)為"close"。Nginx與前端的連接默認(rèn)為長連接,一個用戶跟Nginx建立連接之后,通過這個長連接發(fā)送多個請求。如果Nginx只是作為reverse proxy的話,可能一個用戶連接就需要多個向后端的短連接。如果后端的服務(wù)器(源站或是緩存服務(wù)器)處理并發(fā)連接能力不強(qiáng)的話,就可能導(dǎo)致瓶頸的出現(xiàn)。

Nginx目前的upstream連接建立和獲取的機(jī)制如下圖。Nginx會在一開始創(chuàng)建connection pool(進(jìn)程間不共享,可以避免鎖),提供給所有向前/后的連接。

怎么在Nginx中實現(xiàn)反向代理并支持長連接

如果要實現(xiàn)upstream長連接,則每個進(jìn)程需要另外一個connection pool,里面都是長連接。一旦與后端服務(wù)器建立連接,則在當(dāng)前請求連接結(jié)束之后不會立即關(guān)閉連接,而是把用完的連接保存在一個keepalive connection pool里面,以后每次需要建立向后連接的時候,只需要從這個連接池里面找,如果找到合適的連接的話,就可以直接來用這個連接,不需要重新創(chuàng)建socket或者發(fā)起connect()。這樣既省下建立連接時三次握手的時間消耗,又可以避免TCP連接的slow start。如果在keepalive連接池找不到合適的連接,那就按照原來的步驟重新建立連接。假設(shè)連接查找時間可以忽略不計,那么這種方法肯定是有益而無害的(當(dāng)然,需要少量額外的內(nèi)存)。

怎么在Nginx中實現(xiàn)反向代理并支持長連接

具體如何來設(shè)計這個keepalive connection pool,不同人有不同的選擇。比如Nginx目前的第三方模塊upstream keepalive(作者M(jìn)axim Dounin)使用了一個queue來做。因為upstream的服務(wù)器很可能是多個,所以可能當(dāng)保持的連接數(shù)多的時候,查找的時間可能會較長??梢越o每個upstream服務(wù)器都分配一個pool(queue),縮短查找時間。但是總體來說內(nèi)存操作很快,影響不會很大。upstream keepalive模塊目前只支持memcached,但是可以重用其代碼來達(dá)到對http upstream的長連接。由于Nginx作者之前沒有考慮upstream的長連接,所以在設(shè)計上要把http upstream keepalive模塊化可能比較難,只能通過手動修改代碼來做到。

一個完整的讓upstream支持長連接的配置示例如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

#user nobody;

worker_processes 1;

 

#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;

 

 client_max_body_size 20M;

 client_header_buffer_size 32k;

 large_client_header_buffers 4 32k;

 

 sendfile on;

 #tcp_nopush on;

 

 #keepalive_timeout 0;

 keepalive_timeout 65;

 

 #gzip on;

 

 proxy_buffer_size 64k;

 proxy_buffers 32 32k;

 proxy_busy_buffers_size 128k;

 

 upstream aauCfg_backend {

 server 127.0.0.1:97;

 keepalive 16;

 }

 

 upstream HFC_backend {

 server 127.0.0.1:8090;

 keepalive 16;

 }

 

 upstream manager_backend {

 server 127.0.0.1:8095;

 keepalive 16;

 }

 

 server {

 listen 80;

 server_name localhost;

 

 #charset koi8-r;

 

 #access_log logs/host.access.log main;

 

 root html/tools;

 index index.html index.htm index.php;

  

 proxy_http_version 1.1;

 proxy_set_header Connection "";

 proxy_set_header Host $host;

 proxy_set_header X-Real_IP $remote_addr;

 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  

 location / {

  if (!-e $request_filename) {

  #rewrite ^/(.*)$ /index.php/$1 last;

  #break;

  rewrite ^/(.*)$ /index.php/$1;

  }

 }

  

 location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {

  expires max;

  log_not_found off;

 }

  

 location ^~ /aauCfg/ {

  #proxy_pass http://$remote_addr:97$request_uri;

  proxy_pass http://aauCfg_backend;

 }

  

 location ^~ /HFC/ {

  #proxy_pass http://$remote_addr:8090$request_uri;

  proxy_pass http://HFC_backend;

 }

  

 location ^~ /manager/ {

  #proxy_pass http://$remote_addr:8095$request_uri;

  proxy_pass http://manager_backend;

 }

  

 #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;

 }

 

 # proxy the PHP scripts to Apache listening on 127.0.0.1:80

 #

 #location ~ \.php$ {

 # proxy_pass http://127.0.0.1;

 #}

 

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

 #

 #location ~ \.php$ {

 # fastcgi_pass 127.0.0.1:9000;

 # fastcgi_index index.php;

 # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

 # include fastcgi_params;

 #}

  

 location ~ .php

 {

  fastcgi_pass 127.0.0.1:9000;

  fastcgi_index index.php;

  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  include fastcgi.conf;

  include fastcgi_params;

 

  #定義變量 $path_info ,用于存放pathinfo信息

  set $path_info "";

  #定義變量 $real_script_name,用于存放真實地址

  set $real_script_name $fastcgi_script_name;

  #如果地址與引號內(nèi)的正則表達(dá)式匹配

  if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {

   #將文件地址賦值給變量 $real_script_name

   set $real_script_name $1;

   #將文件地址后的參數(shù)賦值給變量 $path_info

   set $path_info $2;

  }

  #配置fastcgi的一些參數(shù)

  fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;

  fastcgi_param SCRIPT_NAME $real_script_name;

  fastcgi_param PATH_INFO $path_info;

 }

 

 # deny access to .htaccess files, if Apache's document root

 # concurs with nginx's one

 #

 #location ~ /\.ht {

 # deny all;

 #}

 }

 

 

 # another virtual host using mix of IP-, name-, and port-based configuration

 #

 #server {

 # listen 8000;

 # listen somename:8080;

 # server_name somename alias another.alias;

 

 # location / {

 # root html;

 # index index.html index.htm;

 # }

 #}

 

 

 # HTTPS server

 #

 #server {

 # listen 443 ssl;

 # server_name localhost;

 

 # ssl_certificate cert.pem;

 # ssl_certificate_key cert.key;

 

 # ssl_session_cache shared:SSL:1m;

 # ssl_session_timeout 5m;

 

 # ssl_ciphers HIGH:!aNULL:!MD5;

 # ssl_prefer_server_ciphers on;

 

 # location / {

 # root html;

 # index index.html index.htm;

 # }

 #}

 

}

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

本文標(biāo)題:怎么在Nginx中實現(xiàn)反向代理并支持長連接
本文地址:http://www.rwnh.cn/article8/gjhhop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司ChatGPT、營銷型網(wǎng)站建設(shè)、App設(shè)計自適應(yīng)網(wǎng)站、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)
巴楚县| 尼玛县| 定兴县| 故城县| 肥东县| 苏州市| 凤庆县| 四平市| 黔西| 安丘市| 黄石市| 五峰| 广东省| 金湖县| 嘉峪关市| 红安县| 高雄市| 麦盖提县| 凤城市| 磐石市| 得荣县| 武平县| 东乡县| 噶尔县| 鄯善县| 敖汉旗| 新泰市| 华亭县| 武冈市| 宝应县| 全椒县| 石首市| 德保县| 娱乐| 信宜市| 宁陕县| 汉寿县| 富锦市| 繁峙县| 伊金霍洛旗| 三穗县|