Home  >  Q&A  >  body text

html - Ask a cross-domain question weakly

For example, on the same server, the main directory of the website is bound to www.domain.com, and the backend of the website is bound to www.domain.com:8080.

Even if the ports are different, it will be regarded as cross-domain, right? I feel that this situation is a bit unfair across domains, and POST is not cross-domain convenient, right?

Do you have any good ways to solve this problem? Can you use certain settings to prevent cross-domain between the website and the backend? This method of separating the website and the backend will face cross-domain problems sooner or later, right?

phpcn_u1582phpcn_u15822714 days ago480

reply all(2)I'll reply

  • PHPz

    PHPz2017-05-16 17:12:52

    There are many cross-site solutions, the common ones are JSONP, proxy forwarding, and the new feature of HTLM5's Allow Origin that allows cross-site access.

    In your case, it is recommended to set up a proxy in Ngingx of the website and forward it to the website backend. The configuration is probably like this:

    upstream backend {
        server backend1.example.com:8080;
    }
    
    server {
        location /api {
            proxy_pass http://backend;
        }
    }

    On the front-end page, the address accessed through ajax is: http://www.example.com/api/xxx

    reply
    0
  • 高洛峰

    高洛峰2017-05-16 17:12:52

    Different ports are inherently different sites, and sites are cross-domain! By separating the front-end and back-end, the website can be more secure and can be maintained separately. I don’t think it is necessary to use the front-end to cross-domain requests to the back-end. This will expose the back-end, and the program design should not retrieve data across domains through the front-end. Bar! Unless your database is a file database such as Access or SQLite, then it is a design problem. Requesting data only requires a data connection. No need to cross domains.

    reply
    0
  • Cancelreply