Use nginx to solve the problem of application restart caused by domain name binding in IIS

Source: Internet
Author: User

When migrating multiple sites to one site (Tenant feature), the user needs to bind a custom domain name, however, when you bind a domain name to IIS, this site will be restarted. As long as a user binds a domain name, other tenants on this application will be affected. The following describes our solution, but not necessarily optimal.

The problem occurs when a domain name is bound to IIS, causing the application to restart a simple test.

1. Create an empty MVC project and define a global static variable in the mvcapplication class.

2. Create a controller and view

3. On the view page, ++ the count in mvcapplication and Output

4. Access several pages

5. Bind a domain name

6. Refresh the page

Now the verification is complete. You can see that the application has been restarted.

Why does domain name binding cause application restart?

Principles and changesWeb. configFile will cause the application to restart and start the same, because when the domain name is boundC: \ windows \ system32 \ inetsrv \ config \ applicationhost. configAdd a configuration in the file as follows:

Will IIS restart only?

The answer is no. Tomcat and so on also need to be restarted when I first came into contact with Java, but the service needs to be restarted manually. Now I am confused.

Use nginx to solve the problem

In order to solve this problem, I found a lot of information and finally had no choice but to use a proxy server to solve this problem. Although I felt that nginx was wronged here, because it is more important for Server Load balancer, but it can solve the problem. In the future, it can also be used in large traffic volumes. The following describes how to use nginx to solve this problem.

The principle is actually very simple. When a user accesses a URL, it is allocated by nginx, so that the domain name does not need to be directly bound to IIS, and nginx allocates the domain name to which it points.

Bind numerous wildcard domain names to IIS in advance

For example: site1.cs.xxx.com ~ Site9999999.cs.xxx.com removes the need to bind a domain name when adding tenant.

Bind a custom domain name to nginx

Nginx does not provide any APIs, only one configuration file and several commands, so what we need to do is to automate the configuration of domain name actions on nginx, we have written a daemon service to complete this action. The following describes and releases the use instructions and source code of this daemon.

In this way, the problem of binding domain names to IIS is solved, but we have other solutions before this solution.

Scheme of pass-out

1. A server only has one site, and this site is not bound to any domain name. All requests directed to port 80 of this server IP address are responded by this site.

Reason for pass: resources are wasted, and port 80 is greatly wasted.

2. domain name binding at a specified point every day

Reason for pass: the validity period of a domain name after it is bound is unknown and may not take effect until PM.

Nginx automatic server configuration

The main principle of the daemon service is as follows. The Daemon Service provides a WCF Service that includes two operations: binding a Domain Name and deleting a domain name. The service constraints are as follows:

The specific implementation code will not be demonstrated.

Instructions for use

1. Configure the Daemon Service (App. config)

There are two main configurations:

Nginxpath: Nginxstorage, because I need to find the configuration file and nginx.exe, you know.

Runmodel: Nginx only provides command line mode for execution, so I need to use powershell or cmd,Note that if powershell is used, the powershell version must be 3.0 or later.

2. Configure nginx

The default nginx configuration file contains many configurations, So we separate the configuration of the domain name separately, which requires some modifications to the original configuration of nginx.

OpenA nginx-1.7.2 \ conf \ nginx. confFile, add configuration under the HTTP NodeInclude custom. conf;

The configuration is complete, and then start the service.

Client

1. Add service reference

2. Add the following test code

After running, you will findNginx-1.7.2 \ conf \There is one moreCustom. confFile.

Content:

Now we have passed the test,If you want to see the effect, map the hosts to test1.com and test2.com to the local IP address..

To prevent binding failure, we savedDomains. xmlFile and domain name binding information will also be retained here, so that you can restore it on your own.

Daemon Service source code

Http://files.cnblogs.com/ants/ChunSun.NginxServer.DaemonService.zip

Note: the source code is simplified. The original source code is based on rabbit kernel. Therefore, the simplified source code does not provide support for functions such as logs. You can add the source code on your own.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.