1

I've stuck on simple thing, please help. I have 2 directories with PHP projects: /var/www/api/ and /var/www/api-beta/. I want to forwarding each of them to PHP-FPM. Nginx config:

server {
    listen 80;
    set $doc_root /var/www/api;
    root $doc_root;
    index  index.php index.html;


  location /beta {
            alias /var/www/api-beta;
    }


    location ~ \.php$ {
        set $php_root /var/www/api;
        if ($request_uri ~* /beta) {
             set $php_root /var/www/api-beta;
              }

            fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;


    }
}

I've tried do this with if ($request_uri ~* /beta) but it didn't work. I think problem this, because project from /var/www/api works fine, but from /var/www/api-beta I have "File not found." error.

1 Answer 1

0

It may be simpler to create a location block for each PHP root:

server {
    listen 80;
    root /var/www/api;
    index  index.php index.html;

    location ~ \.php$ {
        try_files $uri =404;

        include /etc/nginx/fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $request_filename;
        fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
    }

    location ^~ /beta {
        alias /var/www/api-beta;

        location ~ \.php$ {
            if (!-f $request_filename) { return 404; }

            include /etc/nginx/fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $request_filename;
            fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
        }
    }
}

Notes:

  • avoid using alias and try_files together. See this long standing issue.
  • the ^~ modifier cause the prefix location to take precedence over the regular expression location above. See this document for more.
Sign up to request clarification or add additional context in comments.

1 Comment

It works, thanx! I thought, that we can use same directive location for PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.