Nginx搭建企业网关(可以解决ajax跨域问题)

第一步:修改nginx.conf配置文件

  1. 修改nginx.conf配置文件
    vim /usr/local/webserver/nginx/conf/nginx.conf
  2. 在server标签中添加下列配置
location /a {
#此ip为tomcat的ip地址,因为我两台tomcat在同一台linux上,所以ip地址跟下面一样
proxy_pass http://192.168.10.10:8080/; 
index index.jsp index.html;
}
location /b {
proxy_pass http://192.168.10.10:8088/;
index index.jsp index.html;
}
  1. 重新加载配置 /usr/local/webserver/nginx/sbin/nginx -s reload
  2. 找到nginx服务器中index页面并进行修改
    vim /usr/local/webserver/nginx/html/index.html

第二步:修改nginx服务器中的index.html

  1. 将下列代码替换到index.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
     <script src="js/jquery-1.12.4.js"></script>
    <script>
        $(function(){
            $("#doAjax").click(function(){
                $.ajax({
                    type:"post",
                    url:"http://192.168.10.10/b",   //此处为上面conf文件中配置的nginx访问地址
                    success:function(){
                        alert("请求成功");
                    },
                    error:function(){
                        alert("请求失败");
                    }
		 });
            });
	 }); 
      </script>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>      
<p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    <button id="doAjax">13号技术</button>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
  1. 在该目录创建一个js文件夹,放入jquery-1.12.4.js

第三步:测试

  1. 打开浏览器输入nginx的服务器ip,出现请求成功则配置成功
    在这里插入图片描述
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值