nginx configure reverse proxy to solve cross-domain

1. Download nginx

 

 Avoid detours and download the stable version

Two, two methods to start nginx

1. Double-click nginx.exe

2. Go to the nginx folder and enter start nginx

There will be a pop-up box flashing, and then enter localhost in the browser, the page shows welcome even if the startup is successful

Three, nginx commonly used commands

start nginx    // Start nginx 

nginx -s stop    // Force close 

nginx -s quit    // Safely close 

nginx -s reload   // Restart 

nginx -s reopen   // Open log file

Fourth, configure cross-domain

1. Principle

Front end: localhost: 8081 access Back end: localhost: 7070

Use nginx to specify a port such as 8080, and proxy both to the same port to solve cross-domain

2. Configuration file

For convenience, we create a new proxy.conf in the conf folder

server{
  #Specify a unified port
  listen     8080;
  #IP address to be monitored, local is localhost
  server_name   localhost;
  #Reverse proxy to the corresponding url
  # / demo means you can visit / demo1, you can also visit / demo / index
  # / demo / can only visit / demo / index
  location /demo {
      #Address of your front end
      proxy_pass  http://localhost:8081;  
  }
  
  location  /api {
      #Backend address
      proxy_pass  http://localhost:7070;   
  }
} 

Introduce proxy.conf in nginx.conf

Add a line to http

include      proxy.conf;

Every time you change the configuration file, you must run nginx -s reload to restart

This way localhost: 8080 / demo can access the front end, localhost: 8080 / api can access the back end

 

 

Guess you like

Origin www.cnblogs.com/hess/p/12704296.html