大网数据广告活动
服务器运维问答

Nginx match path on any domain name

· 52CE 我爱测

I want to configure nginx so that a specific path on every domain name routes to a certain location. The use case is I want to be able to point lets encrypt at this location to drop the verification files and have it work for eve…

问题原文

原帖链接 · Qwertie · CC BY-SA 4.0

I want to configure nginx so that a specific path on every domain name routes to a certain location. The use case is I want to be able to point lets encrypt at this location to drop the verification files and have it work for every domain.

I am aware nginx has location blocks that can be used in server blocks but I'm not aware of a way to have a server block that accepts all domain names when the path is correct even when other server blocks specify a domain to listen on.

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

Nginx selects a server block to process a request based on the values of the listen and server_name directives. The best way to insert common code into a number of server blocks (or anywhere else in an Nginx configuration) is to place them into a separate file and use the include directive. See this document for details.

For example:

In a file called wellknown.conf:

location ^~ /.well-known/ {
    root /path/to/root;
}

In each server block:

server {
    ...
    include wellknown.conf;
    ...
}

Without any directory path, the file will be located within the same directory as the nginx.conf file. Obviously, these files can be placed elsewhere within the file system, but avoid directories like sites-enabled, sites-available and conf.d as these already have a specific purpose.

The ^~ modifier on the location directive makes it unambiguous and avoids any regular expression location taking precedence. See this document for details.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-04-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。