I've this configuration:

server {
    server_name app.local;
    index index.php;

    location /api/v1 {
        alias /app/api/v1/code;

        try_files $uri /api/v1/index.php$is_args$args;

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass api-v1-php:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $request_filename;

Removing the outer try_files, it seems to find the index.php, BUT I need to preserve the behavior where every request (e.g. app.local/api/v1/test) are being handled anyway by the index.php. How can I mantain that? Is this a bug?

  • try_files and alias have side-effects, but they are consistent side-effects. That statement is necessary to the function of your website. The statement either works or it doesn't work, you will need to test it. I tend to avoid using try_files with alias and prefer to use if (!-e $request_filename) { rewrite ^ /api/v1/index.php last; } - as you already know from the link I gave you on our sister site. – Richard Smith Jan 25 at 11:02
  • @RichardSmith Getting rid of constructs like that is the whole reason behind try_files. – Michael Hampton Jan 25 at 11:29

Here is a working configuration of nginx with php7.4-fpm for Linux Ubuntu.


# You can use NGINX to "sanitize" the input to the application by setting the HTTP_PROXY FastCGI parameter to
# an empty string. This removes the parameter completely from the FastCGI request.
fastcgi_param  HTTP_PROXY         "";
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;


# handle .php
location ~ \.php$ {
  # 404
  try_files           $uri $uri/ =404;
  # default fastcgi_params
  include             fastcgi.conf;
  # fastcgi settings
  fastcgi_pass        /run/php/php7.4-fpm.sock;
  fastcgi_index       index.php;
  fastcgi_buffers     8 16k;
  fastcgi_buffer_size   32k;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.