Nginx's practical method for solving cross-domain problems

Nginx's practical method for solving cross-domain problems

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).
# Proxy server interface

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:
  • Nginx configuration cross-domain request Access-Control-Allow-Origin * detailed explanation
  • How to use Nginx to handle cross-domain Vue development environment
  • Solution to invalid Nginx cross-domain setting Access-Control-Allow-Origin
  • Detailed explanation of using Nginx reverse proxy to solve cross-domain problems
  • How to use Nginx to solve front-end cross-domain problems
  • Detailed explanation of solving the problem of cross-domain access of nginx/apache static resources

<<:  MySQL example of getting today and yesterday's 0:00 timestamp

>>:  In-depth understanding of the role of Vuex

Recommend

Supplementary article on front-end performance optimization

Preface I looked at the previously published arti...

Tutorial on binary compilation and installation of MySql centos7 under Linux

// It took me a whole afternoon to install this, ...

Basic understanding and use of HTML select option

Detailed explanation of HTML (select option) in ja...

How to use Docker to package and deploy images locally

First time using docker to package and deploy ima...

JavaScript implements mouse drag to adjust div size

This article shares the specific code of JavaScri...

How to install Nginx in Docker

Install Nginx on Docker Nginx is a high-performan...

Introduction to common MySQL storage engines and parameter setting and tuning

MyISAM, a commonly used storage engine in MySQL c...

Steps to set up and mount shared folders on Windows host and Docker container

Programs in Docker containers often need to acces...

Three ways to implement text color gradient in CSS

In the process of web front-end development, UI d...

An article to help you learn more about JavaScript arrays

Table of contents 1. The role of array: 2. Defini...

How to add a column to a large MySQL table

The question is referenced from: https://www.zhih...

Design Association: Why did you look in the wrong place?

I took the bus to work a few days ago. Based on m...

Installation tutorial of mysql 8.0.11 compressed version under win10

This article shares the installation tutorial of ...