我有一个 / 位置块,它执行我所有的 fastcgi 操作,并且它的正常超时时间为 10 秒。我希望能够为某些文件(/admin、sitemap.xml)设置不同的超时时间。有没有一种简单的方法可以做到这一点,而无需为每个位置复制整个位置块?
location /admin{
fastcgi_read_timeout 5m;
#also use the location info below.
}
location /sitemap.xml{
fastcgi_read_timeout 5m;
#also use the location info below.
}
location / {
fastcgi_pass 127.0.0.1:8014;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param HTTP_X_FORWARDED_FOR $http_x_forwarded_for;
}
答案1
我认为有两种可能性,其中第二种是最灵活的。
1
您可以尝试:
location /admin{
fastcgi_read_timeout 5m;
include snippets/location.fcgi.conf;
}
location /sitemap.xml{
fastcgi_read_timeout 5m;
include snippets/location.fcgi.conf;
}
文件snippets/location.fcgi.conf
(不需要位置块):
fastcgi_pass 127.0.0.1:8014;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
# ...
2
或者我想你可以这样做。
location / {
# set you default, so we don't bomb out
set $timeout 2s;
# your other general stuff
}
location /sitemap.xml{
set $timeout 5m;
include snippets/location.fcgi.conf;
}
location /admin{
fastcgi_read_timeout 5m;
include snippets/location.fcgi.conf;
}
笔记:所有包含路径都是总是相对于 Nginx 主配置文件夹。
是的,只要您有符号链接,就可以将它们放在任何您想要的地方。
答案2
您不能在位置块之间继承,但您可以使用include 指令将所有常用配置指令放在一个文件中,然后在每个位置包含该文件。