Nginx reverse proxy is used to avoid cross-origin ajax requests. nginxajax

Source: Internet
Author: User
Tags nginx reverse proxy

Nginx reverse proxy is used to avoid cross-origin ajax requests. nginxajax

Nginx + tomcat on the server, where nginx listens to port 80 and tomcat listens to port 8080.

Because I am not familiar with the front-end, I think that callback is not required when ajax is used. However, the front-end students say that callback is not required when cross-origin is not used, so that I can add it to the returned json. However, I have just learned the most basic spring-mvc usage and do not know how to add callback.

Some feasible code will be found on the Internet, like this:

@ RequestMapping (method = RequestMethod. GET, value = "getProjectStatusList", produces = "text/html; charset = UTF-8") @ ResponseBodypublic String getProjectStatusList (HttpServletRequest request, HttpServletResponse response) {Map <String, object> map = new HashMap <String, Object> (); try {String callback = request. getParameter ("callback"); // System. out. println ("token:" + request. getHeader ("token"); List <String> list = ss. GetProjectStatusList (); map. put ("status", "success"); map. put ("data", list); ObjectMapper mapper = new ObjectMapper (); // This stitching is important... String result = callback + "(" + mapper. writeValueAsString (map) + ")"; // String result = mapper. writeValueAsString (map); return result;} catch (Exception e) {JSONObject jo = new JSONObject (); jo. put ("status", "fail"); jo. put ("data", e. getMessage (); return jo. toString ();}}

However, this change is really frustrating for me, because I have too many URL ing and the modification cost is too high.

So I think of nginx. Isn't this guy involved in reverse proxy? Really wit me

With this idea, it is easy to do. Add a location directly to the server listening to port 80:

location /myApp {proxy_pass http://localhost:8080/myApp;}

Reload nginx:

{NGINX_HOME}/sbin/nginx -s reload

Then, the previous cross-origin request of http: // site: 8080/myApp is changed to a non-Cross-origin request of http: // site/myApp.

The above section describes how to use Nginx reverse proxy to avoid ajax cross-origin requests. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.