Nginx - 为一个特定路由重写 fastcgi_param

Nginx - 为一个特定路由重写 fastcgi_param

我只想更改fastcgi_param下述特定航线:

server {
    # (...)

    # PHP entry point for main application
    location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ {
        try_files $uri =404;
        fastcgi_pass   fastcgi_backend;
        fastcgi_buffers 1024 4k;

        fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
        fastcgi_param  PHP_VALUE "memory_limit=756M \n max_execution_time=18000";
        fastcgi_read_timeout 600s;
        fastcgi_connect_timeout 600s;

        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # Changed memory_limit for that path
    location ^~ ^/rewritten_path {
        # Change memory_limit only for all routes: example.com/rewritten_path/...
        fastcgi_param  PHP_VALUE "memory_limit=1024M \n max_execution_time=18000";
        # Allow /index.php$is_args$args
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # (...)
}

答案1

fastcgi_param您可以在另一个块中定义一个新值location,但您需要包含完整的上下文、所有其他fastcgi_param语句和fastcgi_pass语句。

在现有块中使用变量会更简单location。您可以使用 定义变量map。请参阅这个文件了解详情。

例如:

map $request_uri $php_value {
  default             "memory_limit=756M \n max_execution_time=18000";
  ~^/rewritten_path   "memory_limit=1024M \n max_execution_time=18000";
}
server {
    ...
    location ... {
        try_files $uri =404;
        fastcgi_pass   fastcgi_backend;
        include        fastcgi_params;
        ...
        fastcgi_param  PHP_VALUE  $php_value;
        ...
    }
}

相关内容