我在Laravel上制作了一个项目。我有网页的路线和api的路线。我的问题是:如何为这两个组设置不同的超时?
我尝试使用中间件,只是玩type A = B & C;
,但它没有用。
所以我想我可以通过我的Nginx vhost文件做到这一点,而且我有点坚持这个。到目前为止我的结局如何:
set_time_limit
(当然,我将我的超时设置为server {
listen 80;
listen 443 ssl http2;
server_name mysiste;
root "/home/vagrant/www/mysite/public";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/mysite-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ ^/api/v1 {
try_files $uri $uri/ /index.php?$query_string;
client_body_timeout 1;
send_timeout 1;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
}
只是为了进行我的研究。)
有人知道如何处理这个问题吗?
谢谢!
答案 0 :(得分:4)
您的配置不起作用的原因是,try_files
指令重定向到您的location ~ \.php$
。
为避免这种情况,您必须删除特定路径中的try_files
并对fastcgi_param SCRIPT_FILENAME
进行硬编码。
这是我解决此问题的方法,在该问题中,我不得不允许用于视频上传的路由更长的超时时间:
set_time_limit(1800);
位于我的laravel控制器顶部(从/api/posts
解析)然后使用如下所示的nginx位置:
location ~ ^/api/posts {
include fastcgi_params;
fastcgi_connect_timeout 30s;
fastcgi_read_timeout 1800s;
fastcgi_send_timeout 1800s;
fastcgi_buffers 256 4k;
fastcgi_param SCRIPT_FILENAME '${document_root}/index.php';
fastcgi_pass php:9000;
}
location ~ \.php$ {
try_files $uri /index.php?$query_string;
include fastcgi_params;
fastcgi_connect_timeout 30s;
fastcgi_read_timeout 30s;
fastcgi_send_timeout 30s;
fastcgi_buffers 256 4k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php:9000;
}
您可能必须针对用例调整参数。
出于记录,我使用PHP 7.3和nginx 1.12.2
答案 1 :(得分:0)
你不能用nginx做,你必须在nginx中设置max timeout并在你的Classes或php.ini中控制你的app-timeout
here is the answer incl. info why it so
所以在你的Nginx配置"位置〜.php $"部分,将request_terminate_timeout设置为一个非常高的值(或更好的是,将其设置为0以禁用超时)。并且还将fastcgi_read_timeout设置为您可能希望任何脚本运行的最高秒数。
然后通过在php.ini中设置max_execution_time来设置默认值来微调它。现在,如果您希望允许脚本长时间运行,请在这些脚本中使用set_time_limit()PHP命令。