After being tortured by the front-end cross-domain problem for almost 2 days, I finally solved it using ngnx, so I will summarize it here. This article only discusses how to use Ngnx to solve cross-domain problems, and does not discuss the principles. 1. First, introduce the relevant command operations of Nignx in Windows environment Common nginx commands:
After stopping nginx, nginx.pid in the /logs directory will be automatically deleted
Check whether the nignx listening port is started successfully
Solution: The port is still listening after closing nignx 1. netstat -ano | findstr port number#Get PID 2. tasklist | findstr "PID" #command to find nginx process information 3. taskkill /f /t /im nginx.exe #End the nginx process 2. Introduce how to configure Nignx to solve cross-domain problems Front-end IP port number: http://localhost:8080/ Backend IP port number: http://localhost:8082/ Now when we do not set up cross-domain, the front-end request is as follows uni.request({ url:'http://localhost:8082/ApiController/test', success:(res)=>{ console.log(res.data) }, }) Access address: 'http://localhost:8082/ApiController/test', it will appear Then we configure Nignx Edit /config/nginx.conf this file 1) Add header information and add cross-domain access configuration in the http block of the nginx.conf configuration file add_header Access-Control-Allow-Origin *; //Allow all domain names to access the proxy address across domains add_header Access-Control-Allow-Headers X-Requested-With; add_header Access-Control-Allow-Methods GET; //Cross-domain request access request method, 2) Set up a reverse proxy server { listen 80; #Configure nignx's listening port server_name localhost; #Configure nignx's listening address location /ApiController{ #Listening address starts with /ApiController proxy_pass http://localhost:8082; #Forwarding address} } After configuration, our front-end access url http://localhost:8082/ApiController/test should be changed to http://localhost:80/ApiController/test #Monitoring at this time Use localhost as the domain name Use port 80 The address starts with /ApiController Address forwarding will be performed uni.request({ url:'http://localhost:80/ApiController/test', success:(res)=>{ console.log(res.data) }, }) Result: (Access successful) Summarize This is the end of this article about how Nginx solves the problem of cross-domain access to front-end resources. For more relevant content about how Nginx solves the problem of cross-domain access to front-end resources, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: HTML line spacing setting methods and problems
>>: MySQL lock control concurrency method
During system maintenance, you may need to check ...
Table of contents 1 Introduction 2 Prerequisites ...
Preface I believe everyone has used the top comma...
Recently I have used the function of renaming sto...
Introduction to temporary tables What is a tempor...
This article example shares the specific code of ...
1. Rounded border: CSS CodeCopy content to clipbo...
1. Overflow content overflow settings (set whether...
There are many reasons why an application is as s...
Requirements: Remove HTTP response headers in IIS...
In general guestbooks, forums and other places, t...
Preface Any application that can be written in Ja...
SMIL adds support for timing and media synchroniz...
Table of contents Overview What is Big O notation...
This article shares the installation and configur...