Server Side (Apache) solves JavaScript cross-domain problems __java

Source: Internet
Author: User

Because of the limitations of the front-end to solve cross-domain problems, for Ajax Cross-domain or IFRAME Cross-domain, it is recommended to use server-side solutions.

The principle of this scenario is to accept the request from the client, send the request to the target server through the domain Server Agent, and return the response data to the client.

use Apache's Mod_proxy module to turn on the reverse proxy function to achieve:

1 Modify the Apache configuration file httpd.conf, remove the following two lines before # #

    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so

2 increase in server Config or virtual host:

    Proxyrequests off

    <proxy *> order
      Deny,allow
      allow to all
    </Proxy>

    proxypass/ Folder Http://****.com/floder

Restart Apache.

Note: The proxyrequests off instruction refers to a reverse (reverse) proxy, which is like the original server for the client, and the client does not need to make any special settings, and the forward proxy allows the client to access any Web site through it and hide the client itself. Therefore, security measures must be taken to ensure that only authorized clients are serviced. The PROXYPASS directive allows you to map a remote server to the URL space of the local server, where the local server does not act as a proxy role, but instead acts as a mirror of the remote server. /folder is a local virtual path, Http://****.com/floder is a partial URL to a remote server

If you do not want to reverse proxy for a subdirectory, you can use the "!" Instructions. For example:

    Proxypass/folder/exception!
    Proxypass/folder Http://****.com/floder

All requests for http://****.com/floder, except/folder/exception, will be represented.

You can also use the URL rewrite method to implement the

1 Modify the Apache configuration file httpd.conf, remove the following three lines before # #

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module
modules/mod_proxy_http.so LoadModule Rewrite_module modules/mod_rewrite.so

2 increase in server Config or virtual host:

<Location/folder>
  sethandler proxy-server order
  Allow,deny allow to all
</Location>

Rewriteengine
on Rewriterule ^/folder/(. *) $ http://****.com/floder [L,R=301,P,NC]

Note: The Location instruction provides URL based access control, and access to any resource under the/folder directory in this domain is first handled by the Proxy-server handler (a mod_proxy defined within the handler module). The SetHandler proxy-server directive is to force all matching files to be processed by a single proxy server. The rewriteengine on instruction refers to opening the rewrite engine. The rewriterule directive is an overriding rule. Last| This tag is used to block the currently overridden URL from being rewritten again by a successor rule. Redirect| R [=code] If substitution begins with http://thishost[:thisport]/( making the new URL a URI), it is mandatory to perform an external redirect, which is the essential medicine for Cross-domain or directed to the external domain. The default is HTTP response code 302, which I usually specify as 301. proxy| P This tag causes the replacement component to be internally forced to send as a proxy request, indicating that the rewrite is passed through the Mod_proxy proxy, rather than through an externally redirected past. nocase| NC ignores case, that is, when pattern matches the current URL, there is no difference between ' A-Z ' and ' A-Z '.

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.