是否可以在两个单独的块中定义单个nginx位置?
是否可以在两个单独的块中定义单个nginx位置 例如:是否可以在两个单独的块中定义单个nginx位置?,nginx,Nginx,是否可以在两个单独的块中定义单个nginx位置 例如: server { listen *:80; server_name someserver; location / { proxy_cache off; } location /assets { proxy_cache mycache; } # What do I need to do to get this second root location # to append to the
server {
listen *:80;
server_name someserver;
location / {
proxy_cache off;
}
location /assets {
proxy_cache mycache;
}
# What do I need to do to get this second root location
# to append to the 1st? If it's even possible.
location / {
proxy_pass http://foo;
}
}
其思想是根位置实际上变成:
location / {
proxy_cache off;
proxy_pass http://foo;
}
如果你想了解更多的原因,请继续阅读
背景: 我正在运行GitLab综合安装。根据,管理员可以将自定义nginx配置注入GitLab服务器块 但是,此注入会将内容添加到终端服务器块,而不是特定的位置块 因此,如果我尝试注入
包含我的_config.conf代码>,我将获得:
server {
listen *:80;
server_name someserver;
location / {
proxy_cache off;
}
location /assets {
proxy_cache mycache;
}
include my_config.conf;
}
server {
listen *:80;
server_name someserver;
location / {
proxy_cache off;
}
location /assets {
proxy_cache mycache;
}
location ^~ / {
include my_config.conf;
}
}
如果我试图包括location^ ~/{\n包括我的_config.conf;\n}\n
我会得到:
server {
listen *:80;
server_name someserver;
location / {
proxy_cache off;
}
location /assets {
proxy_cache mycache;
}
include my_config.conf;
}
server {
listen *:80;
server_name someserver;
location / {
proxy_cache off;
}
location /assets {
proxy_cache mycache;
}
location ^~ / {
include my_config.conf;
}
}
这会导致nginx配置失败,并在/var/opt/gitlab/nginx/conf/gitlab http.conf:106
错误中出现重复位置“/”
注意:
my_config.conf
包含用于CORS的add_header
内容,所以它必须如此。您可能需要了解如何使用。但位置块不是相加的,必须单独完成。您可以在服务器
块中放置某些语句,并允许根据需要覆盖单个位置
块(例如代理缓存
)
假设您的所有URI都是代理的,您可以使用如下内容:
proxy_cache off;
location / {
proxy_pass http://foo;
}
location /assets {
proxy_cache mycache;
proxy_pass http://foo;
}
通用语句可以卸载到include
文件中