The Unity Webgl version client handles the problem of cross-domain inaccessibility

I haven’t done the Unity web version before, but it’s the first time I did it. I debugged it well in the editor, and there is no problem with the front-end and back-end login docking. I took it for granted that everything will be fine after the release. Who knew that the release would be the beginning of the nightmare. Good guy, I can't log in directly. Looking at the output log, it turned out to be an unknown error, which completely confused me. Later, by checking the operation log of the web version, I found out that it was a problem that the browser could not access across domains. (Press F12 to directly view the operation log of the web page, which I didn't understand at the time, and it took me a long time).

 

According to my superficial understanding, cross-domain is caused by inconsistency between client and server protocols, domain names, and ports. For more details, please check the link below.

Causes of cross-domain problems

The best way to solve the cross-domain problem is to solve it on the server side, but if it is due to some special reasons, we cannot solve it on the server side, does it mean that this problem cannot be solved. of course not. Do you still remember the reasons for cross-domain we mentioned above? Cross-domain is caused by inconsistent protocols, domain names, and ports. If we can make the protocols, domain names, and ports consistent, then the cross-domain problem will be solved? Some people say, aren't you talking nonsense? If the protocol, domain name, and port are all the same, I still need to read this rubbish you wrote. Don't worry about it, just imagine, if we deploy a proxy server locally, through the proxy server to access the server we really need to access. All communication between us and the real server goes through this proxy server. Because the proxy server is deployed locally, there is naturally no cross-domain problem. As for the direct cross-domain problem between the proxy server and the server, we have no way to modify the real server directly, but we can modify the configuration of the proxy server we deploy by adding a protocol that allows cross-domain access to it. Isn't it solved? Of course, no matter how good the theory is, it depends on practice.

First download an nginx server, don't ask why you choose it, just find it just right. For detailed download and deployment, please refer to the link below.

Download, install and deploy nginx

After the deployment is complete, the most important thing is to open the conf folder under the nginx path you decompressed, find the nginx.conf configuration file below, change the configuration according to the following settings, and execute nginx -s reload on the cmd console after modification method

 

   location / {
        #实际需要访问的服务端,这里需要替换成你想要访问的服务端
            proxy_set_header Host www.ilab-x.com;
            proxy_pass http://www.ilab-x.com/;
            
            #root   html;
            #index  index.html index.htm;
            #allow all;
            
            charset utf-8;   #显示中文
            #设置代理服务器允许所有的跨域访问
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            
            #加上这一句防止post方法还是出现跨域的问题,刚开始就是因为少了这个,折腾了好久
            if ($request_method = 'OPTIONS') {
                 return 204;
            }

        }

Click to view more detailed parameter configuration and explanation.

 nginx configuration proxy server

Subsequent filling pits, after configuration, it is found that it can only be accessed using localhost, and cannot be accessed through IP. After searching for a long time, I found out that both Alibaba Cloud and Tencent Cloud need to log in to the server controller to set port mapping. You can refer to the link below for how to set it up. It has been explained in detail.

Ali cloud server nginx public network IP cannot access the browser

Of course, these servers have opened port 80 of nginx by default, but who called me cheap? Looking for a long time.

Guess you like

Origin blog.csdn.net/qq_20991149/article/details/122433336