Nginx solve cross-domain problems and downtime

First, the downtime problem solving

Prepare two Tomcat, when one of Tomcat hang (server is down), then find another server continues to run

1, analog outages

  

2, modify nginx.conf file restart Nginx

  

3, the normal response

  

    

4, analog downtime problems again will find the next surviving server continues to run

   

Second, the cross-domain problem solving

Nginx solve cross-domain issues, implementation:
  www.a.com:8080/a
  www.b.com:8081/b

  cross-domain problem occurs if the ajax request is sent directly to a page in engineering b, then the solution is: the A and B at the same time agents to Nginx, do the Nginx request routing, direct direct access to the Nginx B project page

1, show directory

  

 2, import dependencies

  

 3, NginxServlet

package com.a;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/NginxServlet")
public class NginxServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1、接收数据
        String username = request.getParameter("username");
        System.out.println("接收的数据:"+username);

        //2、响应结果
        response.getWriter().write("success");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

 4、index.jsp

<%--
  Created by IntelliJ IDEA.
  User: zheng
  Date: 2020/2/10
  Time: 15:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Nginx解决跨域问题</title>
    <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
    <script>
        $(function () {
            $("#button").click(function () {
                $.ajax({
                    url:"http://www.znzn.com/A/NginxServlet?username="+$("#username").val(),
                    type:"GET",
                    success:function (result) {
                        alert(result);
                    }
                })
            });
        })
    </script>
</head>
<body>
    数据:<input type="text" name="username" id="username"/>
    <input type="button" id="button" value="请求"/>
</body>
</html>

5、修改nginx.conf文件、重启Nginx

  

 6、效果展示

  

   

 

 

  

Guess you like

Origin www.cnblogs.com/Zzzzn/p/12291165.html