IIS web.config - same file for multiple websites

2019-02-25 17:29发布

问题:

We have an IIS 7.5 (not using ASP) with the following sites:

  • Site A
  • Site B
  • Site C
  • Site D
  • Site E

Site B, Site C and Site D belong to a centralized application that uses the same web.config. While the IIS allows us to use a web.confg for all of them plus an inherited web.config for each site, there does not seem to be an option to group sites to restrict this global inheritance.

To illustrate the problem, I will call the web.config after their content:

  • Site A uses web.config "company-website" in /www/static/site_A/web.config
  • Site B uses web.config "central-cms" in /www/cms/site_B/web.config
  • Site C uses web.config "central-cms" in /www/cms/site_C/web.config
  • Site D uses web.config "central-cms" in /www/cms/site_D/web.config
  • Site E uses web.config "community-board" in /www/boards/site_E/web.config

As you can see, we have 5 physical web.config files although 3 of them (B, C, D) contain the same content.

We want Site B, C and D to share a single web.config file, but still want Site A and E to use their own web.config without having to negate inheritance.

Possible structure:

  • /www/static/site_A/web.config (no inheritance)
  • /www/cms/web.config (base inheritance)
    • /www/cms/site_B/web.config (+ inheritance)
    • /www/cms/site_C/web.config (+ inheritance)
    • /www/cms/site_D/web.config (+ inheritance)
  • /www/boards/site_E/web.config (no inheritance)

Note: All web.config files declare various rewrite rules and directives for <system.webServer>.

Is IIS 7.5 capable of this?

回答1:

The web.config should be capable of doing this actually. Put the common web.config file into a folder that each site has read permission for. Then modify the individual web.config files to call the common file:

<appSettings file="/path/to/some/common/web.config">

Then this common file can be used for any files that need it and ignored for those that don't. You can even rename it to common.config or something so that it doesn't get inherited. So this would work:

<appSettings file="/www/common.config">

All children directories should have read access to it, and since it's not a web.config it shouldn't mess with any application settings that are derived.

Note: When including a config file in this manner, changes to the included file do NOT trigger an application recycle in the same manner that a direct change to the web.config would. This should not truly be a problem since you simply have to restart the application pool, but it can be confusing at first.