NGINX配置及问题

一、关于Nginx的负载均衡

在服务器集群中,Nginx起到一个代理服务器的角色(即反向代理),为了避免单独一个服务器压力过大,将来自用户的请求转发给不同的服务器。详情请查看我的另一篇博客

 

二、Nginx负载均衡策略

  负载均衡用于从“upstream”模块定义的后端服务器列表中选取一台服务器接受用户的请求。一个最基本的upstream模块是这样的,模块内的server是服务器列表:

    #动态服务器组
    upstream dynamic_zuoyu {
        server localhost:8080;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082;  #tomcat 8.5
        server localhost:8083;  #tomcat 9.0
    }

 

  在upstream模块配置完成后,要让指定的访问反向代理到服务器列表:

        #其他页面反向代理到tomcat容器
        location ~ .*$ {
            index index.jsp index.html;
            proxy_pass http://dynamic_zuoyu;
        }
        

 

  这就是最基本的负载均衡实例,但这不足以满足实际需求;目前Nginx服务器的upstream模块支持6种方式的分配:

负载均衡策略
轮询默认方式
weight权重方式
ip_hash依据ip分配方式
least_conn最少连接方式
fair(第三方)响应时间方式
url_hash(第三方)依据URL分配方式

  在这里,只详细说明Nginx自带的负载均衡策略,第三方不多描述。

1、轮询

  最基本的配置方法,上面的例子就是轮询的方式,它是upstream模块默认的负载均衡默认策略。每个请求会按时间顺序逐一分配到不同的后端服务器。

  有如下参数:

fail_timeout与max_fails结合使用。
max_fails

设置在fail_timeout参数设置的时间内最大失败次数,如果在这个时间内,所有针对该服务器的请求都失败了,那么认为该服务器会被认为是停机了,

fail_time服务器会被认为停机的时间长度,默认为10s。
backup标记该服务器为备用服务器。当主服务器停止时,请求会被发送到它这里。
down标记服务器永久停机了。

  注意:

  • 在轮询中,如果服务器down掉了,会自动剔除该服务器。
  • 缺省配置就是轮询策略。
  • 此策略适合服务器配置相当,无状态且短平快的服务使用。

2、weight

  权重方式,在轮询策略的基础上指定轮询的几率。例子如下:

    #动态服务器组
    upstream dynamic_zuoyu {
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082   backup;  #tomcat 8.5
        server localhost:8083   max_fails=3 fail_timeout=20s;  #tomcat 9.0
    }

  在该例子中,weight参数用于指定轮询几率,weight的默认值为1,;weight的数值与访问比率成正比,比如Tomcat 7.0被访问的几率为其他服务器的两倍。

  注意:

  • 权重越高分配到需要处理的请求越多。
  • 此策略可以与least_conn和ip_hash结合使用。
  • 此策略比较适合服务器的硬件配置差别比较大的情况。

3、ip_hash

  指定负载均衡器按照基于客户端IP的分配方式,这个方法确保了相同的客户端的请求一直发送到相同的服务器,以保证session会话。这样每个访客都固定访问一个后端服务器,可以解决session不能跨服务器的问题。

#动态服务器组
    upstream dynamic_zuoyu {
        ip_hash;    #保证每个访客固定访问一个后端服务器
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082;  #tomcat 8.5
        server localhost:8083   max_fails=3 fail_timeout=20s;  #tomcat 9.0
    }

  注意:

  • 在nginx版本1.3.1之前,不能在ip_hash中使用权重(weight)。
  • ip_hash不能与backup同时使用。
  • 此策略适合有状态服务,比如session。
  • 当有服务器需要剔除,必须手动down掉。

4、least_conn

  把请求转发给连接数较少的后端服务器。轮询算法是把请求平均的转发给各个后端,使它们的负载大致相同;但是,有些请求占用的时间很长,会导致其所在的后端负载较高。这种情况下,least_conn这种方式就可以达到更好的负载均衡效果。

    #动态服务器组
    upstream dynamic_zuoyu {
        least_conn;    #把请求转发给连接数较少的后端服务器
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082 backup;  #tomcat 8.5
        server localhost:8083   max_fails=3 fail_timeout=20s;  #tomcat 9.0
    }

  注意:

  • 此负载均衡策略适合请求处理时间长短不一造成服务器过载的情况。

5、第三方策略

  第三方的负载均衡策略的实现需要安装第三方插件。

①fair

  按照服务器端的响应时间来分配请求,响应时间短的优先分配。

    #动态服务器组
    upstream dynamic_zuoyu {
        server localhost:8080;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082;  #tomcat 8.5
        server localhost:8083;  #tomcat 9.0
        fair;    #实现响应时间短的优先分配
    }

②url_hash

  按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,要配合缓存命中来使用。同一个资源多次请求,可能会到达不同的服务器上,导致不必要的多次下载,缓存命中率不高,以及一些资源时间的浪费。而使用url_hash,可以使得同一个url(也就是同一个资源请求)会到达同一台服务器,一旦缓存住了资源,再此收到请求,就可以从缓存中读取。 

    #动态服务器组
    upstream dynamic_zuoyu {
        hash $request_uri;    #实现每个url定向到同一个后端服务器
        server localhost:8080;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082;  #tomcat 8.5
        server localhost:8083;  #tomcat 9.0
    }

三、总结

  以上便是6种负载均衡策略的实现方式,其中除了轮询和轮询权重外,都是Nginx根据不同的算法实现的。在实际运用中,需要根据不同的场景选择性运用,大都是多种策略结合使用以达到实际需求。

 

 

nginx实现负载均衡和动静分离

 

nginx配置(windows配置),供大家参考,具体内容如下

以下是我的项目用到的一份配置文件

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

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

#user nobody;

worker_processes 4; #进程数,一般cpu是几核就写多少

 

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

 

 sendfile on;

 #tcp_nopush on;

 

 #keepalive_timeout 0;

 keepalive_timeout 65;

 proxy_connect_timeout 15s;

 proxy_send_timeout 15s;

 proxy_read_timeout 15s;

 fastcgi_buffers 8 128k;

 

 gzip on;

 client_max_body_size 30m;

 gzip_min_length 1k;

 gzip_buffers 16 64k;

 gzip_http_version 1.1;

 gzip_comp_level 6;

 gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png image/webp;

 gzip_vary on;

 

 #第一个集群

 upstream xdx.com{

 server 119.10.52.28:8081 weight=100;

 server 119.10.52.28:8082 weight=100;

  

 }

 

 #第二个集群,用于上传图片所用

 upstream xdxfile.com{

 server 119.10.52.28:8081;#关于文件上传的请求均访问这个集群

 }

 

 #第三个集群

 upstream xdx8082.com{

 server 119.10.52.28:8082;#8082

 }

 

 #第四个集群

 upstream xdxali.com{

 server 139.196.235.228:8082;#阿里云

 }

 

 #第五个集群

 upstream xdxaliws.com{

 server 139.196.235.228:8886;#阿里云websocket

 }

#第一个代理服务器,监听的是80端口,监听的域名是www.wonyen.com或者wonyen.com

 server {

 listen 80;#监听的端口

 server_name www.wonyen.com wonyen.com;#监听的域名

 

 #charset koi8-r;

 

 #access_log logs/host.access.log main;

 

 

 #location指的是访问的路径,下面这条配置表示当访问网站的根目录,即访问wonyen.com或者www.wonyen.com的时候,就去根目录为html的下面去寻找index.html或者index.htm。在index.html这个页面里面你可以做一些重定向的工作,跳转到指定页面

 

 #也可以自定义到某个集群

 # location / {

  # root html;

  # index index.html index.htm;

 #}

 #所有静态请求都交由nginx处理,存放目录为webapps下的root,过期时间为30天

  location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v|json)$ {

  root apache-tomcat-8.0.9-windows-x86-yipin-8081/apache-tomcat-8.0.9/webapps/ROOT;

  expires 30d;

 }

 

 #配置以Att结尾的请求的处理集群为http://xdxfile.com

 location ~ ^/\w+Att{

  proxy_pass http://xdxfile.com;

 }

 

 #配置以Fill结尾的请求的处理集群为http://xdxfile.com

 location ~ ^/\w+Fill{

  proxy_pass http://xdxfile.com;

 }

 

 #精准配置,如果请求名为/crowdFundSave,则

 location = /crowdFundSave{

  proxy_pass http://xdxfile.com;

 }

 

 #精确配置,同上

 location = /crowdFundRewardSave{

  proxy_pass http://xdxfile.com;

 }

 

 #精确配置,同上

 location = /garbageCategorySave{

  proxy_pass http://xdxfile.com;

 }

 

 #精确配置,同上

 location = /mailTestAjax{

  proxy_pass http://xdx8082.com;

 }

 

 #精确配置,同上

 location = /mailSendAjax{

  proxy_pass http://xdx8082.com;

 }

 

 #精确配置,同上

 location = /mailOldAjax{

  proxy_pass http://xdx8082.com;

 }

 

 #精确配置,同上

 #location = /wechatAuthority{

  #proxy_pass http://xdxali.com;

 #}

 location ~ ^/ueditor1_4_3{

  proxy_pass http://xdxfile.com;

 }

 #其他所有请求都访问 http://xdx.com的集群

  location ~ .*$ {

  index index;

  proxy_pass http://xdx.com;

  }

 #404页面访问/Error404.jsp这个location

 error_page 404  /Error404.jsp;

 

 #500等页面也访问 /Error404.jsp这个location

 error_page 500 502 503 504 /Error404.jsp;

 

 #配置请求/Error404.jsp就访问http://xdxfile.com集群

 location = /Error404.jsp {

  proxy_pass http://xdxfile.com;

 }

 

 # 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$ {

 # root  html;

 # fastcgi_pass 127.0.0.1:9000;

 # fastcgi_index index.php;

 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

 # include fastcgi_params;

 #}

 

 # 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

 #另外一个代理服务器,监听8886接口,监听的域名为www.wonyen.com或者wonyen.com

 server {

 listen 8886;

 server_name www.wonyen.com wonyen.com;

#配置若请求为wonyen.com:8086(根目录),就让他去访问http://xdxaliws.com这个集群,这边配置的是websocket的服务端

 location / {

  proxy_pass http://xdxaliws.com;

  proxy_http_version 1.1;

  proxy_set_header Upgrade $http_upgrade;

  proxy_set_header Connection "upgrade";

 }

 }

 

 

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

 # }

 #}

 

}

以上就是我的一个配置。基本上需要注意的都在配置文件中注解。我把几个重要的地方单独拿出来讲一下。

1.集群的配置,我在上面的配置里有定义多个集群,集群按字面的意思理解就是由多台服务器构成的一个集合,典型的例子如 

1

2

3

4

5

upstream xdx.com{

 server 119.10.52.28:8081 weight=100;

 server 119.10.52.28:8082 weight=100;

  

 }

这样的一个配置,这个集群包含了两个分支,我们可以在两台服务器上搭建相同的项目(上述的例子是在同样的服务器,不同的端口部署相同的项目,因为笔者的服务器有限),当有请求是需要这个集群来处理的时候,nginx会随机分配,当然也可以配置权重来设置两个server的访问概率。这就是负载均衡的原理。我们在多台服务器上部署相同的项目,利用nginx对请求进行转发,这样可以降低只有一台服务器所造成的的负载过大,而且当其中一台服务器挂掉以后,nginx会分配另外一台服务器来工作,这样就不会造成服务停止了。

2.server配置项代表的是一个代理服务器,上面的文件中我们配置了两个文件,分别监听wonyen.com(www.wonyen.com)这两个域名的80和8886端口,所有访问wonyen.com:80(即wonyen.com)这个域名下的请求,都按照第一个server所定义的规则去转发,而所有访问wonyen.com:8886下的请求,则会按照第二个server所定义的规则去转发。

3.我们甚至可以通过配置来处理多个域名,看以下的例子。下面的例子我配置了两个域名的规则,一个是iis服务器,一个是tomcat服务器,主要目的是为了解决80端口只能被一个程序使用的问题。如果iis用了80,tomcat就用不了,反之亦然。所以我给iis和tomcat都分配除了80以外的端口,而把80端口留给niginx。由nginx来分配请求给不同的网站。

复制代码

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

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

 

 sendfile on;

 #tcp_nopush on;

 

 #keepalive_timeout 0;

 keepalive_timeout 65;

 

 gzip on;

 client_max_body_size 30m;

 gzip_min_length 1k;

 gzip_buffers 16 64k;

 gzip_http_version 1.1;

 gzip_comp_level 6;

 gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png image/webp;

 gzip_vary on;

 upstream achina.com{

 server 120.76.129.218:81;

  

 }

 upstream qgrani.com{

 server 120.76.129.218:8080;

  

 }

 

 server {

 listen 80;

 server_name www.achinastone.com achinastone.com;

 

 #charset koi8-r;

 

 #access_log logs/host.access.log main;

 

 location / {

  root html;

  index index.html index.htm;

 }

  #其他请求

  location ~ .*$ {

  index index;

  proxy_pass http://achina.com;

  }

 

 #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$ {

 # root  html;

 # fastcgi_pass 127.0.0.1:9000;

 # fastcgi_index index.php;

 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

 # include fastcgi_params;

 #}

 

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

 server_name www.qgranite.com qgranite.com;

 

 location / {

  root html;

  index index.html index.htm;

 }

  #所有静态请求都交由nginx处理,存放目录为webapp

  location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v)$ {

  root apache-tomcat-8.0.9\webapps\ROOT;

  expires 30d;

 }

  #其他请求

  location ~ .*$ {

  index index;

  proxy_pass http://qgrani.com;

  }

 }

 

 

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

 # }

 #}

 

}

4.还有一个就是动静分离,说得通俗一点就是,把请求数据(动)与请求图片(静)分开,在tomcat里,当我们没有做动静分离的时候,tomcat把对图片的请求也会当成一个动态的请求,而处理动态请求是比较费性能的(至于为什么,我也不太清楚)。所以我们可以使用nginx配置来实现动静分离。

我的做法是把其中一个tomcat项目放在nginx的根目录下,这样,我们就可以通过以下方式来配置,实现当我们访问图片,js,css等静态资源的时候,都到一个指定的目录去访问。这样做的好处除了节省性能,还有一个就是我们不需要在所有的负载均衡服务器中都同步保留这些静态资源,只需要在一个地方保留就好了。配置如下

1

2

3

4

5

#所有静态请求都交由nginx处理,存放目录为webapps下的root,过期时间为30天

  location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v|json)$ {

  root apache-tomcat-8.0.9-windows-x86-yipin-8081/apache-tomcat-8.0.9/webapps/ROOT;

  expires 30d;

 }

5.既然读取静态资源是从这个目录读取的,那么我们必须考虑如何存储静态资源,特别是当我们做了负载均衡以后,在我们的 项目中上传图片的请求有可能在任意一个集群的分支中被调用,比如我们的集群中有A,B两台服务器,他们都有可能做上传图片这件事情,如果A调用了上传图片这个请求,则图片则被上传到了A这台服务器上,反之就是B上面。这样势必导致A,B两台服务器上的静态图片是不同步的,当我们要访问这些图片的时候,(假设此时我们还没做动静分离)就有可能出现访问不到的情况。由于上一步我们做了动静分离,现在的问题就演变为,如何把这些A,B服务器上传的图片,都同步到我们做动静分离的那个文件夹下。人工或者程序去同步都很麻烦,我的做法是指定一台服务器(也就是nginx安装的那台服务器)的tomcat项目(也就是部署在nginx根目录下的那个tomcat项目),让它专门来负责上传图片的工作,这样所有的图片都由这个tomcat项目来上传,也就保证了静态库中的图片就是完整的图片。为此我配置了一个集群,如下。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#第二个集群,用于上传图片所用

 upstream xdxfile.com{

 server 119.10.52.28:8081;#关于文件上传的请求均访问这个集群

 }

 

然后在location中我这样配置:

 

 #配置以Att结尾的请求的处理集群为http://xdxfile.com

 location ~ ^/\w+Att{

  proxy_pass http://xdxfile.com;

 }

 

 #配置以Fill结尾的请求的处理集群为http://xdxfile.com

 location ~ ^/\w+Fill{

  proxy_pass http://xdxfile.com;

 }

因为我把所有涉及到附件上传的请求都加上了Att或者Fill的后缀,当nginx捕获这些后缀名的请求的时候,就会把他们都交给 http://xdxfile.com这个集群,也就是119.10.52.28:8081这个项目。

6.做了负载均衡以后,有一个不得不面临的问题就是内存数据的同步,我们在程序中有时候会把一些数据存放在内存中,典型的一类数据就是session。如何让session数据在集群的各个分支中共享session呢,这边要用到一个新的东西,叫做redis。我会在下一篇文章中详细地介绍。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

 

Nginx负载均衡配置简单配置方法

Nginx配置文件

 1)  从Nginx官网下载 http://nginx.org/en/download.html

 2)    安装Nginx,并找到nginx.conf文件(C:\nginx\conf\nginx.conf);

 在http中加入配置:

   加权轮询,按服务器的性能给予权重,本例是1:2分配

?

1

2

3

4

upstream www.woizuqiu.com {

  server 192.168.1.1:8080 weight=1;

 server 192.168.1.1:8090 weight=2;

}

  ip_hash轮询方法,不可给服务器加权重,nginx会让相同的客户端ip请求相同的服务器 

?

1

2

3

4

5

upstream www.woizuqiu.com {

    server 192.168.1.1:8080;

    server 192.168.1.1:8090 max_fails=3 fail_timeout=30s ;

   ip_hash;

   }

  根据服务器的本身的性能差别及职能,可以设置不同的参数控制。

  down 表示负载过重或者不参与负载

  weight 权重过大代表承担的负载就越大

  backup 其它服务器时或down时才会请求backup服务器

  max_fails 失败超过指定次数会暂停或请求转往其它服务器

  fail_timeout 失败超过指定次数后暂停时间

server配置如下: 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

server {

  listen  80;

  server_name www.woizuqiu.com;

  #charset koi8-r;

  #access_log logs/host.access.log main;

  location / {

   add_header backendIP $upstream_addr;#被转发到的上游服务器地址

   add_header backendCode $upstream_status;#状态码

   proxy_pass http://www.woizuqiu.com;

   proxy_set_header Host $host;

   proxy_set_header X-Real-IP $remote_addr;

   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;         

  }

 }

1.查看Nginx版本:

  C:\nginx>nginx -v

2.启动Nginx:

  C:\nginx>start nginx

  启动Nginx需要占用80端口,常见错误:bind() to 0.0.0.0:8080 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions),需要把系统的80端口关掉,

  检查端口:netstat -aon | findstr :80 

3.判断Nginx是否启动:

  tasklist /fi "imagename eq nginx.exe"

4.停止:

  C:\nginx>nginx.exe -s stop

5.重新载入Nginx:

  C:\nginx>nginx.exe -s reload

 

如何通过nginx负载均衡跳转https

 

web端拷贝证书与密钥

   scp -rp -P52113 /application/nginx/conf/key 10.0.0.5:/application/nginx/conf/

在nginx负载均衡服务端配置

vim /application/nginx/conf/nginx.conf

?

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

worker_processes 2;

error_log logs/error.log;

events {

  worker_connections 65535;

}

http {

  include    mime.types;

  default_type application/octet-stream;

  sendfile    on;

  keepalive_timeout 65;

  

  

  upstream server_pools {

    server 10.0.0.200:443 weight=1 max_fails=3 fail_timeout=10;

    #server 10.0.0.8:443 weight=1 max_fails=3 fail_timeout=10;

    #server 10.0.0.9:443 weight=1 max_fails=3 fail_timeout=10;

  }

  

  server {

    listen    80;

    server_name localhost;

    rewrite ^(.*)$ https://$host$1 permanent;

  }

  server {

    listen 10.0.0.5:443;

    server_name www.abc.com;

  

    #开启 https 注意要添加在server区块 不能在http区块中放置

    ssl on;

    ssl_certificate /application/nginx/conf/key/server.crt;

    ssl_certificate_key /application/nginx/conf/key/server.key;

  

    location / {

      proxy_pass https://server_pools;

      proxy_set_header Host $host;

      proxy_set_header X-Forwarded-For $remote_addr;

    }

  }

}

#检查nginx负载均衡配置

   /application/nginx/sbin/nginx -t

#重启nginx负载均衡

   /application/nginx/sbin/nginx -s stop
   /application/nginx/sbin/nginx

浏览器访问测试

注意修改hosts对应的是负载均衡的IP地址信息

 

关于nginx+tomcat搭建反向代理时加载静态资源找不到的问题

本文链接:https://blog.csdn.net/zhaoxiaohua125/article/details/78751953
1 、在配置nginx的反向代理时在localtion下需要

upstream tomcat_nginx{
   server 127.0.0.1:8888;
  server 127.0.0.1:8889;
}

location / {

            proxy_pass http://tomcat_nginx;
            proxy_set_header Host      $host;//解决加载静态资源找不到的问题
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect default ;

root   html;
            index  index.html index.htm;
proxy_connect_timeout       1;//解决多个tomcat一个崩溃后加载慢的问题
proxy_read_timeout          1;//解决多个tomcat一个崩溃后加载慢的问题
proxy_send_timeout          1; //解决多个tomcat一个崩溃后加载慢的问题
        }

2.当nginx代理端口不是80的时候需要修改tomcat的server.xml文件将connector的节点中增加一个proxyPort="nginx的端口号" nginx代理端口的位置
————————————————
版权声明:本文为CSDN博主「zhaoxiaohua125」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhaoxiaohua125/article/details/78751953

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值