Configuration operation of accessing different items in the secondary directory after nginx domain name configuration

  • 2021-09-16 06:01:41
  • OfStack

Scene description:

Access different applications from port 1ip + through level 2 directories (virtual directories, applications), for example, location is a user use page, location/admin/is a management page, location is deployed on port 80 of 192.168. 1.100, and location/admin is deployed on port 8080 of 172.20. 1.32.

Solution:

Using the nginx reverse proxy, the configuration is as follows:


server {
    listen 80;
    server_name demo.domain.com;
    # By accessing service2 Level directory to access the background 
  location /service {
      #DemoBackend1 The slash at the back is 1 A key, if there is no slash, it will be passed service To the back-end node causes 404
      proxy_pass   http://DemoBackend1/;
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    # Other paths access the foreground website by default 
    location / {
      proxy_pass http://DemoBackend2;
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }
 
# Simple Load Balancing Node Configuration 
upstream DemoBackend1 {
   server 192.168.1.1;
   server 192.168.1.2;
   ip_hash;
 }
upstream DemoBackend2 {
   server 192.168.2.1;
   server 192.168.2.2;
   ip_hash;
}

However, in this way, the style files in the Level 2 directory will not be displayed normally. They will not automatically find them in the Level 2 directory, but in the root directory. When jumping to the page, they will also report 404 errors. I don't know if there is a configuration error. Configuring root or rewrite in server block can't solve it.

Try adding a level 2 directory after proxy_pass, and the same level 2 directory as the location block, with the following configuration:


server {
    listen 80;
    server_name demo.domain.com;
    # By accessing service2 Level directory to access the background 
  location /service {
      #DemoBackend1 The slash at the back is 1 A key, if there is no slash, it will be passed service To the back-end node causes 404
      proxy_pass   http://DemoBackend1/service;#DemoBackend1 To configure in the Web site 1 The name is service The virtual directory of, and and the location Adj. 2 Level directory name 1 To 
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    # Other paths access the foreground website by default 
    location / {
      proxy_pass http://DemoBackend2;
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }
 
# Simple Load Balancing Node Configuration 
upstream DemoBackend1 {
   server 192.168.1.1;
   server 192.168.1.2;
   ip_hash;
 }
upstream DemoBackend2 {
   server 192.168.2.1;
   server 192.168.2.2;
   ip_hash;
}

Problem solving

In addition, in practical application, I used mvc of asp. net, and there is no problem in setting mvc as a website. If it is a virtual directory, I can't find the path, because many of my addresses in the website are not standardized. The correct way should be:

Here's a typical example of what you should never do:


<script type="text/javascript">
  $.ajax({
    url: '/home/index'
  });
</script>
and here's how this should be done:

<script type="text/javascript">
  $.ajax({
    url: '@Url.Action("index", "home")'
  });
</script>
Here's another typical example of something that you should never do:

<a href="/home/index" rel="external nofollow" >Foo</a>
and here's how this should be written:

@Html.ActionLink("Foo", "Index", "Home")
Here's another example of something that you should never do:

<form action="/home/index" method="opst">

</form>
and here's how this should be written:

@using (Html.BeginForm("Index", "Home"))
{

}

Additional knowledge: Use nginx server, realize the same as 1IP with 1 port to access different items, distinguish the accessed items by domain name

Here I used two nginx servers, one to bind different projects to different ports, and one to distribute different domain names to projects on different ports.

The server section of the conf file for the first nginx:


server {
    listen    8000;
    server_name localhost;
    root  E:/test/pro1;
    location / {
      index  index.html index.htm; 
    }
  }

  server {
    listen    8001;
    server_name localhost;
    root  E:/test/pro2;
    location / {
     index  index.html index.htm;  
    }
  }

The server section of the conf file for the 2nd nginx:


server {
    listen    80;
    server_name www.testpro01.com testpro01.com;
    location / {
      proxy_pass  http://127.0.0.1:8000;
    }
  }

  server {
    listen    80;
    server_name www.testpro02.com testpro02.com;
    location / {
     proxy_pass  http://127.0.0.1:8001/;
    }
  }

Finally, the bat file is used to operate two nginx servers

The startup file is as follows: start. bat


@echo off
echo [start...]
cd /d E:
cd spiovnet\nginx-1.16.1
call start nginx.exe
cd /d D:
cd nginx-1.16.1
call start nginx.exe
echo [end...]
@pause

Others are the same, but the orders are not the same

nginx Start Command: start nginx. exe or nginx

nginx Reload Configuration Command: nginx-s reload

ngin Restart Command: nginx-s reopen

ngin shutdown command: nginx-s stop


Related articles: