ngnix学习(反向代理配置)
windows下安装ngnix
到:http://nginx.org/download/nginx-1.3.11.zip下载nginx
解压,取到nginx的根目录下;
如cd E:\nginx-1.3.11\nginx-1.3.11
start nginx
在浏览器端输入:
http://127.0.0.1/
将会看到如下:
页面则表示nginx安装成功;
配置反向代理:
到conf目录下面找到文件nginx.conf,打开文件找到server {}所在的位置
然后加上
location /google {
proxy_pass http://www.google.com.hk/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#http://localhost:8080/QSGreatStrategy/error.jsp
location /QSGreatStrategy_error {
proxy_pass http://localhost:8080/QSGreatStrategy/error.jsp;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
如图:
输入:http://127.0.0.1/QSGreatStrategy_error
找到页面:
Ngnix详细参数配置:http://www.cnblogs.com/xiaogangqq123/archive/2011/03/02/1969006.html
Nginx负载均衡:http://www.cnblogs.com/xiaogangqq123/archive/2011/03/04/1971002.html
-------------------------------------------------------------------------------------------------------------------------------
*Nginx反向代理解决跨域问题
第一:建立服务器端工程
如图在vs中建立web应用程序:NginxBackTest
建WebService1.asmx文件,代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace NginxBackTest { /// <summary> /// WebService1 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } } }
第二、建立客户端工程
我们使用Jsp作为客户端
在eclipse下建立一个web工程:NginxfeTest
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>NginxfeTest</display-name> <welcome-file-list> <welcome-file>Test.jsp</welcome-file> </welcome-file-list> </web-app>
工程图:
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>Insert title here</title> </head> <body> 前台 <div> <input type="button" id="btn1" value="调用无参数方法" /> </div> </body> <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript"> $(function () { //调用无参数方法 $("#btn1").click(function () { $.ajax({ type: "POST", contentType: "application/json", url: "/HelloWorld", //url: "http://127.0.0.1/NginxBackTest", data: "{}", dataType: 'json', success: function (result) { alert(result.d); } }); }); }); </script> </html>
第三、Ngnix配置文件:
location /NginxfeTest { proxy_pass http://localhost:8080/NginxfeTest; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /HelloWorld { proxy_pass http://localhost:4569/WebService1.asmx/HelloWorld; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
*PS:因为我只有一台电脑,所以勉强使用不同的端口作为非同源的区分,其实道理都是这样,很多资料上面都介绍了什么是非同源了
访问:http://127.0.0.1 /NginxfeTest/
如图,我们跨域成功了!!!
我们将反向代理的设置定到具体的目录:
location /LBI-AppServer/WebService { proxy_pass http://localhost:4569/WebService; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
目录结构如图:
<%@ 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>Insert title here</title> </head> <body> 前台 <div> <input type="button" id="btn1" value="调用无参数方法" /> </div> </body> <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript"> $(function () { //调用无参数方法 $("#btn1").click(function () { $.ajax({ type: "POST", contentType: "application/json", //url: "/WebService/WebService1.asmx/HelloWorld", url:"/LBI-AppServer/WebService/WebService1.asmx/HelloWorld", //url: "http://127.0.0.1/NginxBackTest", data: "{}", dataType: 'json', success: function (result) { alert(result.d); } }); }); }); </script> </html>
同样我们访问:http://127.0.0.1 /NginxfeTest/
这个页面,反向代理也是成功的!!
但是这种方式较前一种方式有一些好处,就是你不要具体到某个资源,只需要具体到该资源对应的目录下即可,配置文件少写了一些,而且同样管理Url
通过本机ip访问:
另外一台电脑访问:
再用其他的浏览器进行测试:
ie6:
测试通过;
搜狗:
测试通过;
ie9:
测试通过;
360:
测试通过;
firefox:
测试通过;
之前默认使用chrome测试也是通过的;
另外付,Nginx源代码:http://www.cnblogs.com/yjf512/archive/2012/06/13/2548515.html