Nginx solves cross-domain problems in front-end development

1. First, you need to have an nginx server (nginx is not specifically mentioned here)

2. Configure the reverse proxy in nginx.config, the specific code is as follows:

##前端开发环境配置代理
	server {
        listen       9898;
        server_name  192.168.1.222;
		# 配置前端页面代理
        location / {
            proxy_pass http://192.168.1.222:8080; #前端开发环境地址
        }	 
		# 配置后端接口代理
        location ^~ /xbproject/ {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Credentials' 'true';
			proxy_pass http://xxx/xbproject/;	#后端接口地址
        }
		error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

3. After the configuration is complete, start the nginx server.

4. Note: The original front-end address developed is: http://192.168.1.222:8080 , the debugging address is now http://192.168.1.222: 9898; the original front-end directly requests the back-end address http://xxx/xbproject , Now request http://192.168.1.222: 9898/xbproject address in the code .

In the end, the front end and the back end are the same, so they do not cross domains.

Guess you like

Origin blog.csdn.net/qq_29407683/article/details/105731769