linux下配置nginx反向代理例子
官方说明:
例子:
虚拟机ip:192.168.85.3,物理机VMware Network Adapter VMnet8 ip:192.168.85.1
1,准备tomcat
准备一tomcat,端口,8080
准备一Jsp,用于获取客户端真实IP和nginx IP ,test.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Page</title>
</head>
<body>
Test1 Page!!!<br/>
remote ip : <%-- <%=request.getHeader("X-real-ip") %> --%> <br/>
nginx server ip : <%=request.getRemoteAddr()%>
</body>
</html>
将test.jsp 上传到 tomcat的/ROOT目录下。
2,配置nginx
注意,反向代理之后获取到客户端IP地址为nginx服务器地址,这里需要nginx进行forward,设置真实的ip地址。往请求头set一变量 X-real-ip ,值取客户端ip,这样就可以在jsp中获取该变量:
proxy_set_header X-real-ip $remote_addr;
匹配.jsp结尾的请求:
location ~ \.jsp$
3,测试
启动tomcat,启动nginx,浏览器访问:http://192.168.85.3:70/test.jsp
在Java里面一般有一个工具类来获取 IP:
public class IpUtils {
private IpUtils() {
} public static String getIpAddr(HttpServletRequest request) {
if (request == null) {
return "unknown";
}
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Forwarded-For");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
} if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
linux下配置nginx反向代理例子的更多相关文章
- Linux 下的 Nginx 反向代理配置.
最近实践中遇到了需要利用 nginx 进行反向代理服务器请求的需求,以前没怎么碰触过,因此花了1个多小时,快速阅览了一下nginx官网在反向代理服务中给出的基本定义: 说实话,官网给予的定义是精准的, ...
- linux下配置nginx负载均衡例子
准备2台虚拟机: 分别在两个虚拟机上安装tomcat,并在服务器A安装nginx,其中nginx端口设置为了 70. 服务器A的tomcat安装目录: 服务器B的tomcat安装目录: 服务器A的ng ...
- Linux服务-配置Nginx反向代理
任务目标:实现基于轮询的方式调度三台web,并验证结果:实现基于权重的方式调度三台web,并验证结果:实现基于hash的方式调用三台web,并验证结果 由于刚刚做了nfs设置,为了提现实验结果,我在w ...
- 【netcore基础】CentOS 7.6.1810 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动
之前写过一篇Ubuntu的环境搭建博客,感觉一些配置大同小异,这里重点记录下 nginx 作为静态 angular 项目文件服务器的配置 参考链接 [netcore基础]ubuntu 16.04 搭建 ...
- CentOS 7 学习(二) 配置Nginx反向代理
CentOS 7 学习(二) 配置Nginx反向代理 Nginx可以通过php-fpm来运行PHP程序,也可以转向apache,让apache调用php程序来运行. 不过对于Nginx来说,其反向代理 ...
- 为docker私有registry配置nginx反向代理
公司的Docker私有registry已经搭建好了,用官方的registry image很容易就搭建好了.现在就是要用nginx的反向代理把它放出来,以便在外网可以访问. 我的上一篇blog 讲了如何 ...
- 配置LANMP环境(7)-- 配置nginx反向代理,与配置apache虚拟主机
一.配置nginx反向代理 1.修改配置文件 vim /etc/nginx/nginx.conf 在35行http下添加一下内容: include /data/nginx/vhosts/*.conf; ...
- 使用SSL配置Nginx反向代理的简单指南
反向代理是一个服务器,它接收通过Web发出的请求,即http和https,然后将它们发送到后端服务器(或服务器).后端服务器可以是单个或一组应用服务器,如Tomcat,wildfly或Jenkins等 ...
- [亲测]ASP.NET Core 2.0怎么发布/部署到Ubuntu Linux服务器并配置Nginx反向代理实现域名访问
前言 ASP.NET Core 2.0 怎么发布到Ubuntu服务器?又如何在服务器上配置使用ASP.NET Core网站绑定到指定的域名,让外网用户可以访问呢? 步骤 第1步:准备工作 一台Liun ...
随机推荐
- python note 06 编码方式
1.有如下值li= [11,22,33,44,55,66,77,88,99,90],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中.即: {'k1': 大 ...
- [leetcode]77. Combinations组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- 激活prompt
1.下载SQLPrompt 2. 断网, 打开注册机,拷贝验证码 2. 点击activate, 拷贝代码
- Python项目--Scrapy框架(一)
环境 win8, python3.7, pycharm 正文 1.Scrapy框架的安装 在cmd命令行窗口执行: pip install Scrapy 即可完成Scrapy框架的安装 2. 创建Sc ...
- django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call
Error info: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, ...
- boost 编写finger服务
本篇是模仿PYTHON TWISTED写一个FINGER示例. 从最简单的链接到通过接收字符串返回不同的内容 1 最简单的链接 #include <ctime> #include < ...
- JavaBean四个作用域范围
使用 useBeans的scope属性可以用来指定javabean的作用范围 page //仅在当前页面有效 request //可以通过HttpRequest.getAttribute()方法取得J ...
- a标签使用href=”javascript:void(0); 在火狐浏览器跟chrome 不兼容
使用如下方式的链接.在Chrome中点击后行为符合预期,但在IE下会新开标签卡(根据参考资料,Firefox中有相同问题).<a href=”javascript:void(0);” targe ...
- HashSet源码
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java. ...
- 6.装配Bean基于注解
1.注解:就是一个类,使用@注解名称 开发中:使用注解 取代 xml配置文件. @Component取代<bean class=""> @Component(" ...