Ilya Baranovskiy
Ilya Baranovskiy

Reputation: 21

Multiple projects for one domain nginx

I need to setup two projects under one domain(react+symfony), here is my nginx config:

server {
    listen 80;
    server_name domain.ltd;
    rewrite_log on;
    root /var/www/frontend;

    access_log      /var/log/nginx/project_access.log;
    error_log       /var/log/nginx/project_error.log;

    location / {
        index /index.html;
    }

    location /api/ {
        alias  /var/www/backend;
        try_files $uri /index.php$is_args$args;

        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;
            internal;
        }
    }
}

root / works fine so static index.html I'm getting okay without any issues. but symfony application for /api throws an error(403 Forbidden) in nginx error.log:

32349 open() "/var/www/frontend/index.php"

For some reason alias ignored, what i'm doing wrong? Thanks in advance

Upvotes: 1

Views: 1598

Answers (1)

Richard Smith
Richard Smith

Reputation: 49742

The location and the alias values should both end with a / or neither end with a /. The last parameter of the try_files statement is a URI, so you need to include the /api/ prefix.

For example:

location /api/ {
    alias  /var/www/backend/;
    try_files $uri /api/index.php$is_args$args;

    location ~ \.php$ {
        try_files $uri =404;      
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        internal;
    }
}

Also, add a try_files statement to the nested location block to avoid passing uncontrolled requests to PHP. The correct value for SCRIPT_FILENAME is $request_filename which works with either root or alias.

Upvotes: 1

Related Questions