Nginx cross-domain setup nginx apache nginx php nginx rewrite

Source: Internet
Author: User
    • When doing a project, the design of front-end separation is adopted, and the rest-style HTTP interface is designed to meet the background service call and the front-end to use the AJAX call directly. , then encountered a cross-domain problem, the background is the spring MVC structure, the HTTP method supports GET, post, put, delete, option and other methods, because at the time of the post call, the front-end HTTP will first take option to the server side, 204 and then submit the request data, in addition to the nginx level to do the adjustment, Spring Web. XML also needs to be set up, this scheme does not need to add filter on the server and other things, the possible version is as follows:

    • Spring Web. XML

<servlet><servlet-name>root</servlet-name><servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param><param-name> contextconfiglocation</param-name><param-value>classpath:applicationcontext-web.xml</ Param-value></init-param><init-param><param-name>dispatchoptionsrequest</param-name ><param-value>true</param-value></init-param><load-on-startup>1</load-on-startup ></servlet><servlet-mapping><servlet-name>root</servlet-name><url-pattern>/ </url-pattern></servlet-mapping>
    • Nginx.conf Some places are more verbose.

Location ~/api/* {dav_methods PUT DELETE;                if ($request _method = ' OPTIONS ') {add_header ' access-control-allow-origin ' $http _origin ';                Add_header ' Access-control-allow-methods ' GET, Post,put,delete, OPTIONS '; Add_header ' Access-control-allow-headers ' authorization,dnt,x-customheader,keep-alive, User-Agent,X-Reques                Ted-with,if-modified-since,cache-control,content-type ';                Add_header ' access-control-max-age ' 3600;                Add_header ' Content-type ' Text/plain charset=utf-8 ';                Add_header ' content-length ' 0;            return 204;                } if ($request _method = ' POST ') {add_header ' access-control-allow-origin ' $http _origin ';                Add_header ' Access-control-allow-methods ' GET, Post,put,delete, OPTIONS '; Add_header ' Access-control-allow-headers ' authorization,dnt,x-customheader,keep-alive, User-Agent, X-requested-with,if-modified-since,cache-control,content-type ';                } if ($request _method = ' GET ') {add_header ' access-control-allow-origin ' $http _origin ';                Add_header ' Access-control-allow-methods ' GET, post,delete,put, OPTIONS '; Add_header ' Access-control-allow-headers ' authorization,dnt,x-customheader,keep-alive, User-Agent,X-Reques            Ted-with,if-modified-since,cache-control,content-type ';                } if ($request _method = ' PUT ') {add_header ' access-control-allow-origin ' $http _origin ';                Add_header ' Access-control-allow-methods ' GET, Post,put,delete, OPTIONS '; Add_header ' Access-control-allow-headers ' authorization,dnt,x-customheader,keep-alive, User-Agent,X-Reques            Ted-with,if-modified-since,cache-control,content-type '; } if ($request _method = ' DELETE ') {add_header ' Access-control-allow-origin ' $http _Origin ';                Add_header ' Access-control-allow-methods ' GET, post,delete,put, OPTIONS '; Add_header ' Access-control-allow-headers ' authorization,dnt,x-customheader,keep-alive, User-Agent,X-Reques            Ted-with,if-modified-since,cache-control,content-type ';            } root/opt/www/web/;            Index index.jsp; Proxy_pass <a rel= "nofollow" href= "http://localhost.html" target= "_blank" > <a rel= "nofollow" href= "htt            P://localhost "target=" _blank ">http://localhost</a></a>:8089;        include/opt/conf/nginx/proxy.conf; }

Above is the Nginx cross-domain setting nginx nginx php nginx rewrite content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.