Separate the front and back ends and use nginx to solve cross-domain problems Front-end: vue.js+nodejs+webpack Backstage: SpringBoot Reverse proxy server: nginx Idea: Package the front-end code, let nginx point to static resources, and nginx forwards the background requests. 1. Package the front-end code: npm run build A dist folder will be generated. Contains an index.html file and a static folder. The path is based on my local location as an example: /Users/xxx/ideaProjects/webtest/dist 2. Open In the nginx.conf file in the /usr/local/etc/nginx directory, add the following to the server: listen 80; #Original 8080, to avoid conflicts, change to 80 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /Users/xxx/ideaProjects/webtest/dist; index index.html; # This is used to handle the rewriting problem when Vue, Angular, and React use H5's History if (!-e $request_filename) { rewrite ^(.*) /index.html last; break; } } #Proxy server interface location /api/ { proxy_pass http://localhost:8080/;# proxy interface address} test The front end sends a request: http://localhost/test, vue-router redirects it to http://localhost/api/demo/1, and the actual access is http://localhost:8080/demo/1. Send a request directly to the backend: visit http://localhost/api/demo/1, the actual access is: http://localhost:8080/demo/1 Content expansion thinking: 1). location /api/ { proxy_pass http://localhost:8080/;# proxy interface address} The proxy interface address only reaches 8080, so it will automatically add the name of the background project? ? ? For example, the interface is http://148.70.110.87:8080/project name/method name. . . 2). The request is made in .js. Nginx is configured as above, but the request fails with http://148.70.110.87/api/index2 404 (Not Found) axios.post('/api/index2') .then( (response) => { console.log(response); }) .catch( (error)=> { console.log(error); }); 3). I really don't understand your third step, testing. It would be great if you could provide relevant code. You may also be interested in:
|
<<: MySQL example of getting today and yesterday's 0:00 timestamp
>>: In-depth understanding of the role of Vuex
Preface I looked at the previously published arti...
// It took me a whole afternoon to install this, ...
Detailed explanation of HTML (select option) in ja...
First time using docker to package and deploy ima...
This article uses an example to illustrate how to...
This article shares the specific code of JavaScri...
Install Nginx on Docker Nginx is a high-performan...
MyISAM, a commonly used storage engine in MySQL c...
Programs in Docker containers often need to acces...
In the process of web front-end development, UI d...
Table of contents Preface 1. Preparation 2. Insta...
Table of contents 1. The role of array: 2. Defini...
The question is referenced from: https://www.zhih...
I took the bus to work a few days ago. Based on m...
This article shares the installation tutorial of ...