Reverse Proxy for ASP.NET Core Websites or Docker Using Nginx, Baota Panel for ASP.NET Core Reverse Proxy

2019年12月15日 4329点热度 1人点赞 2条评论
内容目录

1. Nginx Configuration File

Nginx can be configured for reverse proxying, load balancing, etc. Its default configuration file is named nginx.conf.

Generally, it is stored under /your_installation_directory/nginx/conf.

Nginx loads its configuration information, allowing all configurations to be placed inside the nginx.conf file, or separate configurations to be placed in different files for each site. Then, the include command can be used within the nginx.conf file to include the configuration files.

The configuration of nginx takes effect immediately, meaning that there is no need to stop or restart nginx. After modifying and saving the configuration file, the changes take effect immediately.

Custom configuration files can be named arbitrarily.

2. Reverse Proxy Configuration Code

The following is a simple example of configuring a reverse proxy, suitable for ASP.NET Core, Docker, etc. The content is relatively simple and purely for reverse proxying purposes. The aim is to allow external access to resources on the server.

Note: ASP.NET Core uses Kestrel by default, and it requires reverse proxying to enable external access in a cross-platform manner.

Configuration text:

server {
listen port;    # Use English commas to separate multiple domain names, e.g., 80,81
server_name ip_or_domain;   # Use English commas to separate multiple domain names
location / {
proxy_pass http://ip:port;  # Example: http://localhost:8111
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

Only three areas need to be changed.

Every configuration uses one server

server
{

}

Notes:

If you want to configure access for multiple ports and multiple domain names, you will need multiple server{} blocks.

In Nginx, the ports within the same server{} configuration are shared. Ports and domain names are cross-related. (Demonstration shown in the image below)

Assuming

server {
listen 80,81,82,83;    # Use English commas to separate multiple domain names, e.g., 80,81
server_name a.com,b.com,c.com,d.com;   # Use English commas to separate multiple domain names
...
...
}

Then, in actual access, there are a total of 4*4 scenarios.

a.com  80,81,82,83

b.com  80,81,82,83

c.com  80,81,82,83

d.com  80,81,82,83

- - - - -

 

 

 

You can either directly place the code content into the nginx.conf file or store it in another accessible directory, then use

//1 A single file
include /www/server/panel/vhost/nginx/test1.conf;

//2 All .conf files in the directory include /www/server/panel/vhost/nginx/*.conf;

// It can also include other text files; this won't be elaborated here

 

 

3. Using the Baota Panel to Manually Add Configurations

Baota Panel is a powerful management tool, but on Linux, it only automatically supports adding websites for PHP.

If your server has Baota Panel installed and you have used Baota to install nginx and other components, then the installation directory of those components is different from the default installation directory.

For nginx installed using Baota Panel,

nginx.conf is located at /www/server/nginx/conf

Separate files are located at /www/server/panel/vhost/nginx

Baota creates a configuration file for each website, which is stored in the /www/server/panel/vhost/nginx directory.

If users want to manually add configurations, they should open this directory, create a new file, and paste the code into it.

 

痴者工良

高级程序员劝退师

文章评论