'How to configure the conf of nginx to make a good proxy for my background server?

First I started with an nginx 80 server and a backend server (ports 5000 and 5001) locally, here is my nginx configuration:

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }

        location /api {
            proxy_pass      http://localhost:5000;
            proxy_set_header host $host;
            proxy_set_header X-Real-IP      $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            root   html;
            index  index.html index.htm;
        }
}

For backend servers(NET CORE Web API), I found that if you specify such default settings, it is inaccessible through intranet IP (http://myIP/api). Server Info(start without arguments):

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.

So I changed the settings of the backend server to something like this: Server Info(start with --urls "https://0.0.0.0:5001;http://0.0.0.0:5000")

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://0.0.0.0:5001
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://0.0.0.0:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.

This way it can be accessed through the intranet IP, and then I put my nginx 80 with ngrok to penetrate the extranet:

Web Interface http://127.0.0.1:4040
Forwarding https://***********.jp.ngrok.io -> http://localhost:80                                                        

Now I can access the homepage through the domain name of the Internet (https://###.jp.ngrok.io), but if I access the /api (https://###.jp.ngrok.io/api), it will jump to the 5001 port of the Internet(https://###.jp.ngrok.io:5001/api), how should I set it to be correctly forwarded to my local 5001 server, is it necessary to penetrate the port 5001 of the backend? P.S. English is not my native language, it may be difficult to read the text above, sorry.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source