0

I have two projects with the different path locations that need to be configured under a single domain with separate upstream PHP 7.1 and HHVM. I am trying to achieve the goal with nginx alias directive, but it renders 403 Forbidden on my specified location. The default root provided inside the server just works fine.

server {
    listen 80;
    listen [::]:80;

    server_name site.local;
    root /srv/project1;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    #This renders a 403 forbidden
    location ~ /catalog/category/view/id {
        alias /srv/project2/public;
        index index.laravel.php;
        if (!-e $request_filename) { rewrite ^ index.laravel.php last; }
        location ~ \.laravel\.php$ {
            if (!-f $request_filename) { return 404; }
            include fastcgi_params;
            fastcgi_pass php-upstream;
            fastcgi_index index.laravel.php;
            fastcgi_param SCRIPT_FILENAME $document_root$request_filename;

        }
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass hhvm-upstream;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

HHVM Upstream When I try site.local the hhvm upstream just works fine and the page is successfully rendered.

PHP Upstream When I try http://site.local/catalog/category/view/id/11 I get a 403 forbidden error and I see this in nginx error log

> site_openresty | 2018/05/21 11:34:43 [error] 6#6: *1 directory index
> of "/srv/project2/public" is forbidden, client: 172.19.0.1, server:
> site.local, request: "GET /catalog/category/view/id/11/??? HTTP/1.1",
> host: "site.local"

I have been trying this for 3 days now and tried different solutions provided on Stackoverflow, github and serverfault but none seems to be fixing my problem.

Aftab Naveed
  • 153
  • 1
  • 7

1 Answers1

0

The possible reason for this error to occur is that the file index.laravel.php does not exist in the directory /srv/project2/public.

Second thing to double-check is ownership and permission of /srv/project2/public and its contents.

Make sure that the owner of this directory and the files in it is the user running nginx ( perhaps www-data or nginx ). Directory mode should be 755 and file mode be 644.

EDIT:

I missed the line fastcgi_param SCRIPT_FILENAME $document_root$request_filename; at the location matched by /catalog/category/view/id

You should replace $request_filename by $fastcgi_script_name so you will end up with fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Inside the location, you can use echo $document_root$fastcgi_script_name; and echo $document_root$request_filename;to see the difference between the two.

Do not forget to reload the service ;)

Craft
  • 161
  • 1
  • 5
  • 1
    I am using docker for windows which sets permission to 777 for everything mounted inside it, I don't think permission is a problem here. And yes of course the index.laravel.php exists. – Aftab Naveed May 21 '18 at 14:56
  • @Aftab Did you try to access http://site.local/catalog/category/view/id ? Does it give the same result ? – Craft May 21 '18 at 17:34
  • Yes it doesn't make any difference. – Aftab Naveed May 21 '18 at 23:46