简单实现Nginx的反向代理+负载均衡

一、引言

上次我们体验了Nginx反向代理的使用,配置是非常简单的,一句配置搞定。这章我们来讲讲在Nginx如何使用反向代理+负载均衡。负载均衡估计程序员都听说过,比如开发一个电商、web端项目什么后期优化需要做负载均衡,不然同时10w用户同时访问,程序就容易相对应的崩溃。

所谓负载均衡,是由多台服务器或服务共同完成一个功能点,从而达到负载均衡的效果。打个比方:用户请求发起一个请求,网站显示的图片量又比较大,如果说这个时候有N个用户同时访问,那么全部的工作量都放在了一台服务器上,指不定什么时候就崩溃了。如果说有多台服务器平分这个任务,那么这样就很轻松了,效率也会有相对应的提高。

二、实现

proxy_pass如何指向多台服务器?

答:把多台服务器用upstream绑定在一起并起一个组名,然后使用proxy_pass指向该组即可。

小编为了做演示,使用了tomcat发布了一个web页面,页面简简单单就一张图片,代码如下:

<html>

<head>

<title>welcome</title>

</head>

<body>

<img src="/images/test.jpg"/>

</body>

</html>

实现效果:访问这个页面时,由多个服务来提供图片的显示。

实现步骤:

1、先建立几个虚拟主机,有多少个服务提供就可以创建多少个,小编在同一台服务器进行演示,就只创建两个了。端口分别是81、82,也有分别的日志文件保存。

server {

listen 81;

server_name localhost;

location / {

root /var/www;

index index.html;

}

access_log logs/access_81.log main;

}

server {

listen 82;

server_name localhost;

location / {

root /var/www;

}

access_log logs/access_82.log main;

}

2、使用upstream绑定多个虚拟主机,起名为imgserver。绑定了如上两个虚拟主机,并设置一系列参数。

weight = 1 //表示权重,意思就是优先谁来处理这次请求,这里小编设置两个都是一样的。

max_fails = 2 //连接失败次数,如果该地址连接失败两次,则表示该服务器已经挂了,就不会在分配任务给它。

fail_timeout = 3 //超时时长,多久没连接上则表示连接失败

upstream imgserver {

server 111.231.51.81:81 weight=1 max_fails=2 fail_timeout=3;

server 111.231.51.81:82 weight=1 max_fails=2 fail_timeout=3;

}

3、proxy_pass 指向该组即可。

通过ip访问,默认就是80端口,会转发到tomcat发布的服务上,如果有请求地址中包含images,则会由upstream分配给不同的地址处理。 访问地址:http://111.231.51.81/ ,最后的效果可以查看不同的日志文件,则可以区分是哪一个处理的请求。

server {

listen 80;

server_name 111.231.51.81;

location /{

proxy_pass http://111.231.51.81:8086/;

}

location ~* images {

proxy_pass http://imgserver;

}

}

三、整个配置文件展示,如果还有不清楚的小伙伴,可以参考一下,最好自己动动小手实战一遍哟。以前文章中的实战演示也包含在其中了。

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

#tcp_nopush    on;

#keepalive_timeout  0;

keepalive_timeout  65;

#gzip  on;

upstream imgserver {

server 111.231.51.81:81 weight=1 max_fails=2 fail_timeout=3;

server 111.231.51.81:82 weight=1 max_fails=2 fail_timeout=3;

}

server {

listen 81;

server_name localhost;

location / {

root /var/www;

}

access_log logs/access_81.log main;

}

server {

listen 82;

server_name localhost;

location / {

root /var/www;

}

access_log logs/access_82.log main;

}

server {

listen 80;

server_name www.suyouge.com;

location /{

proxy_pass http://www.suyouge.com/;

}

}

server {

listen 80;

server_name 111.231.51.81;

location /{

proxy_pass http://111.231.51.81:8086/;

}

location ~* images {

proxy_pass http://imgserver;

}

}

server {

listen      8088;

server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {

# if ($remote_addr = 116.238.62.103) {

# return 404;

#  }

if ($http_user_agent ~ Firefox) {

set $isfox  1;

}

if ($fastcgi_script_name ~ firefox.html){

set $isfox  0;

}

if ($isfox = 1){

rewrite ^.*$ /404.html;break;

}

root /usr/local/nginx/html;

index index.html index.htm;

}

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

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

#    listen      somename:8080;

#    server_name  somename  alias  another.alias;

#    location / {

#        root  html;

#        index  index.html index.htm;

#    }

#}

# HTTPS server

#

server {

listen      443;

server_name  localhost;

ssl                  on;

ssl_certificate      1_www.suyouge.com_bundle.crt;

ssl_certificate_key  2_www.suyouge.com.key;

ssl_session_timeout  5m;

ssl_protocols  SSLv2 SSLv3 TLSv1;

ssl_ciphers  HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers  on;

location / {

root  /var/www/html;

index  index.html index.htm;

}

}

}

https://www.suyouge.com

原文地址:https://www.cnblogs.com/suyouge/p/12539028.html

时间: 2024-08-05 15:51:36

简单实现Nginx的反向代理+负载均衡的相关文章

Nginx实现反向代理负载均衡与静态缓存

介绍: Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.在连接高并发的情况下,Nginx是Apache服务器不错的替代品,能够支持高达50000个并发连接数的响应. 实验环境: Hostname IP 系统 规划 n2.preferred 192.168.1.2 Centos 6.5 Web server n3.preferred 192.168.1.3 Centos 6.5 Web server n6.preferred 192.168.1.6

Nginx实现反向代理负载均衡功能

反向代理软件Nginx:本身支持反向代理.负载均衡功能,属于L7层负载均衡.Nginx反向代理简单易用,受到大部分中小企业的青睐.LVS:支持L4层负载均衡,haproxy:支持L4.L7层负载均衡L4.L7是指OSI模型中的第四层和第七层:L4:TCP负载均衡:L7:http负载均衡nginx.lvs.haproxy区别参考资料https://www.cnblogs.com/ahang/p/5799065.htmlhttps://www.cnblogs.com/like-minded/p/51

Nginx安装-反向代理-负载均衡-动静分离

安装 1.需要素材 后两个用命令下载安装 openssl-1.0.1t.tar.gzzlib -1.2.8.tar.gz 2:在/usr/src/ 下吧 " nginx-1.16.1.tar.gz " "pcre-8.37.tar.gz" 这两个文件放进去并且解压然后在pcre-8.37这个文件下先 : ./configure 在敲 make && make install pcre-conffig --verison 查看版本 下面安装nginx

Nginx + Tomcat 反向代理 负载均衡 集群 部署指南

转载请注明出处:http://blog.csdn.net/smartbetter/article/details/53535435 Nginx是一种服务器软件,也是一种高性能的http和反向代理服务器,同时还是一个代理邮件服务器.也就是说,我们在Nginx上可以发布网站,可以实现负载均衡(提高应答效率,避免服务器崩溃),还可以作为邮件服务器实现收发邮件等功能.而最常见的就是使用Nginx实现负载均衡. Nginx与其他服务器的性能比较: Tomcat服务器面向Java语言,是重量级的服务器,而N

nginx实现反向代理负载均衡

Nginx实现反向代理 nginx代理基于是ngx_http_proxy_module模块的功能,该模块有很多属性配置选项,如: proxy_pass:指定将请求代理至server的URL路径:     proxy_set_header:将发送至 server的报文的某首部进行重写 proxy_send_timeout:在连接断开之前两次发送到server的最大间隔时长:过了这么长时间后端还是没有收到数据,连接会被关闭 proxy_read_timeout:是从后端读取数据的超时时间,两次读取操

NGINX 实现反向代理负载均衡服务器

一.nginx负载均衡与反选代理的区别? 答:我觉得没什么区别,一台就叫反向代理,多台就叫负载均衡,它们相结合使用 二.nginx 负载均衡原理 三.配置nginx负载均衡 修改nginx.conf http {     include       mime.types;     default_type  application/octet-stream;     sendfile        on;     keepalive_timeout  65;     upstream backe

Nginx的反向代理 负载均衡 配置

在ubuntu下安装Nginx: sudo apt install nginx nginx的配置文件有两个: /etc/nginx/nginx.conf /etc/nginx/sites-enabled/default 在server块中增加配置,设置反向代理: server{ listen 9001; server_name 127.0.0.1; location ~ /edu/ { proxy_pass http://127.0.0.1:8080; } location ~ /vod/ {

nginx ----&gt; nginx配置/反向代理/负载均衡

1 server { 2 listen 80; 3 server_name localhost; 4 5 location / { 6 #将请求与我们定义的服务器进行映射 7 proxy_pass http://localhost:8080/loginForm; //分号不能少 8 #root html; 9 #index index.html index.htm; 10 } 11 12 error_page 500 502 503 504 /50x.html; 13 location = /5

nginx+tomcat 反向代理 负载均衡配置

1.nginx的安装和配置见:http://www.cnblogs.com/ll409546297/p/6795362.html 2.tomcat部署项目到对应的服务器上面并启动,不详解 3.在nginx中配置nginx.conf文件: user nobody; worker_processes 4; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid