前面几篇记录下nginx的基本运功,代理服务器的访问,这里来试验下nginx的反向代理。

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

在我们的java项目中的体现就是,通过80端口访问,Nginx接收到,然后进行转发给tomcat服务器,再将服务器的结果给返回。

这里需要修改nginx.conf文件。

	upstream backend {
#代理的IP weight权重大的,接收的访问量就大,反之
server localhost:8084 weight=50;
server localhost:8088 weight=50;
}

 将接收的请求进行转发:

# / 所有做负载均衡 + 反向代理
location / {
root /data/wwwroot1;
index index.html index.htm;#索引文件
proxy_pass http://backend;
}

 这样,通过请求nginx的请求,就可以被分配转发到tomcat上去。这里我是定义了两台tomcat服务器,同时用来做负载均衡的处理。通过设置weight,可以控制访问量。

具体配置代码如下;

#user  nobody;
# worker 工作进程 一般设置 CPU数 * 核数
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;#1个worker产生多少个连接数
} # 配置HTTP服务器的主要段
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压缩功能设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on; #设定负载均衡后台服务器列表
upstream backend {
#代理的IP weight权重大的,接收的访问量就大,反之
server localhost:8084 weight=50;
server localhost:8088 weight=50;
} server {
listen 2022;
server_name localhost;
charset utf-8;
access_log logs/wwwroot2.access.log main;
location / {
root /data/wwwroot2;
index index.html index.htm;#索引文件
}
}
# 虚拟主机段
server {
listen 80;
server_name localhost;
root /data/wwwroot1;
charset utf-8;
#访问日志
access_log logs/wwwroot1.access.log main;
# / 所有做负载均衡 + 反向代理
location / {
root /data/wwwroot1;
index index.html index.htm;#索引文件
proxy_pass http://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$ {
# 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 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;
# }
#} }

 测试结果发现,通过访问80端口的地址,展现的结果是基本五五开的。

还会随机访问到

以上两个截图,分别对应了我的两个tomcat服务器下的测试文件。

 

Linux 下 nginx反向代理与负载均衡的更多相关文章

  1. Linux系统——Nginx反向代理与负载均衡

    集群集群是指一组(若干个)相互独立的计算机,利用高速通信网路组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运用各自服务的独立服务器.这些服务器之间可以彼此通信,协同向用户提供 ...

  2. Linux 下 Nginx 反向代理 负载均衡配置

    转载请注明出处:http://blog.csdn.net/smartbetter/article/details/52036350 上一篇分享了 Nginx + JDK + Tomcat + MySQ ...

  3. Nginx反向代理,负载均衡,redis session共享,keepalived高可用

    相关知识自行搜索,直接上干货... 使用的资源: nginx主服务器一台,nginx备服务器一台,使用keepalived进行宕机切换. tomcat服务器两台,由nginx进行反向代理和负载均衡,此 ...

  4. 【转】Nginx反向代理和负载均衡

    原文链接:http://www.cnblogs.com/shuoer/p/7820899.html Nginx反向代理和负载均衡 环境说明 由于我使用的是windows系统,所以我用虚拟机虚拟出来了3 ...

  5. Nginx反向代理与负载均衡应用实践(一)

    Nginx反向代理与负载均衡应用实践(一) 链接:https://pan.baidu.com/s/1xB20bnuanh0Avs4kwRpSXQ 提取码:migq 复制这段内容后打开百度网盘手机App ...

  6. Nginx反向代理实现负载均衡配置图解

    Nginx反向代理实现负载均衡配置图解 [导读] 负载均衡配置是超大型机器需要考虑的一些问题,同时也是数据安全的一种做法,下面我来介绍在nginx中反向代理 负载均衡配置图解,大家可参考本文章来操作. ...

  7. nginx反向代理与负载均衡

    一:nginx反向代理与负载均衡配置思路与用法 1.nginx反向代理:就是你去相亲时,媒婆就是这里的代理,让媒婆带你去见姑娘 2.nginx负载均衡:就是有很多的媒婆经过商量给你选出最适合你的姑娘, ...

  8. 谁说前端不需要懂-Nginx反向代理与负载均衡

    转:https://juejin.im/post/5b01336af265da0b8a67e5c9 学到老活到老 前端圈一直很新,一直要不停的学习,而且在进入大厂的路上,还要求熟悉一门后台语言等等.用 ...

  9. [转]Nginx反向代理和负载均衡部署指南

    Nginx反向代理和负载均衡部署指南   1.        安装 1)         从Nginx官网下载页面(http://nginx.org/en/download.html)下载Nginx最 ...

随机推荐

  1. Redis工具类

    /** * Copyright © 2012-2016 * <a href="https://github.com/thinkgem/smkj">smkj</a& ...

  2. NPM 上传自己的包

    NPM 上传自己的包 项目中经常 npm install,npm init啥的,那么如何上传自己的包到npm上呢. 注册账号 NPM官网 本地弄一个包 创建npm_test 文件夹 注:不能够有一些特 ...

  3. 006.ks.cfg文件相关

    一 图形化生成ks.cfg文件 [root@server ~]# yum -y install system-config-kickstart #安装图形化kickstart工具 [root@serv ...

  4. sicp 习题

    1.11 求f(n)=f(n-1)+2*f(n-2)+3*f(n-3) #lang racket (define (fff n) (define (fff-iter a b c n) (if (= n ...

  5. python 反射的用法

    class Foo(): def __init__(self,name): self.name=name def text(self): f=Foo() ''' hasattr(obj,name) 判 ...

  6. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索

    A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...

  7. 调用 微信接口报错 {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}

    如下截图,仅为备份,本文转载地址: http://www.cnblogs.com/liaolongjun/p/6080240.html 以下正文↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ...

  8. ds18b20采集温度并上报服务器

    交叉编译器:arm-linux-gcc-4.5.4 Linux内核版本:Linux-3.0 主机操作系统:Centos 6.5 开发板:FL2440 温度传感器:ds18b20 注:此程序的客户端是在 ...

  9. 出现System.web.mvc冲突的原因及解决方法CS0433

    1.问题描述 CS0433:类型“System.Web.Mvc.WebViewPage<TModel>”同时存在于URL1和URL2中 2.解决方案 找到Web.config文件中的< ...

  10. XMPP增加删除好友

    在现阶段的通信服务中,各种标准都有,因此会出现无法实现相互连通,而XMPP(Extensible Message and presence Protocol)协议的出现,实现了整个及时通信服务协议的互 ...