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

How do I tell Nginx to wait a number of seconds before serving an asset?

· 52CE 我爱测

So when I'm locally testing things such as Ajax in apps I'm writing, I often like to add a delay in server side scripts using a sleep statement. It helps simulate slow connections etc. Is there a way to specify a similar delay be…

问题原文

原帖链接 · BaronVonKaneHoffen · CC BY-SA 3.0

So when I'm locally testing things such as Ajax in apps I'm writing, I often like to add a delay in server side scripts using a sleep statement. It helps simulate slow connections etc.

Is there a way to specify a similar delay behaviour directly in Nginx config that would work for the flat HTML files it's serving?

I'm aware you can do a similar delay simulation at the network level (see here) but it seems pretty messy and has never worked very well for me.

社区高分解答

原帖链接 · Sam Bull · CC BY-SA 4.0

Giving a more detailed explanation of how you might use the echo module:

If you're starting from a basic config, that loads static files and PHP files, with something like this:

location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_pass php;
}

That can then be converted to something like this to add a delay to both static and PHP requests:

# Static files
location / {
    echo_sleep 5;
    echo_exec @default;
}
location @default {}

# PHP files
location ~ \.php$ {
    echo_sleep 5;
    echo_exec @php;
}
location @php {
    include fastcgi.conf;
    fastcgi_pass php;
}

This can obviously be modified for anything you want. Basically, move each location block into a named @location. Then use echo_sleep and echo_exec in the original location block.

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

资料来源与适用范围

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

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

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