1

I a trying to make an angular app with an api. Every route should go to the angular app, except the API route. The API route should go to an laravel project. But the API route(/api) won't work, it just redirects to the angular app. And when I remove the angular app route(/) I am able to get an error at /api.

This is my config file:

server {
        listen 80;
        server_name example.nl *.example.nl;

        access_log /home/example/logs/access.log;
        error_log /home/example/logs/error.log;

        root /home/example/public_html;

        location /api {
                root /home/example/public_api/public;
                index index.php;

                try_files $uri $uri/ /index.php?$query_string;

                location ~ \.php$ {
                        include snippets/fastcgi-php.conf;
                        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                }
        }

        location / {
                index index.html;
                try_files $uri$args $uri$args/ /index.html;
        }
}

When I removed the last block I was able to get an error: open() "/usr/share/nginx/html/index.php" failed (2: No such file or directory)

But I am saying root /home/example/public_api/public;. Why it is picking /usr/share/nginx/html?

/usr/share/nginx/html is my default route.

Update

Nginx is now reading the right directory, but PHP is broken, this is my updated config file:

server {
        listen 80;
        server_name example.nl *.example.nl;

        access_log /home/example/logs/access.log;
        error_log /home/example/logs/error.log;

        root /home/example/public_html;

        location /api {
                alias /home/example/public_api/public;
                index index.php;
                try_files $uri $uri/ /index.php?$query_string;

                location ~ \.php$ {
                        include snippets/fastcgi-php.conf;
                        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                }
        }

        location / {
                index index.html;
                try_files $uri$args $uri$args/ /index.html;
        }
}

In the error log I am getting:

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

This is the content I get in the browser: File not found.

And this is the curl content(/api): <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.10.0 (Ubuntu)</center> </body> </html>

And this is the curl content of /api/index.php: File not found.

Jan Wytze
  • 185
  • 1
  • 5
  • 1
    Please add a curl of the api and the matching access log. – Tim Mar 15 '17 at 18:01
  • @Tim The curl is showing the content of `/`. – Jan Wytze Mar 15 '17 at 19:40
  • Please edit your question to add a curl (showing response headers) of the api and the matching access log. If there's an error log show that too. – Tim Mar 15 '17 at 19:48
  • Did you try Googling your latest error? http://serverfault.com/questions/517190/nginx-1-fastcgi-sent-in-stderr-primary-script-unknown suggests "is always related to a wrongly set SCRIPT_FILENAME in the nginx fastcgi_param directive". You don't have that directive, all of my PHP configurations do. – Tim Mar 15 '17 at 19:55
  • @Tim This php configuration works on all my laravel projects(That have only the `/` as location) – Jan Wytze Mar 15 '17 at 19:57
  • The access log might tell you if Nginx or PHP is sending that error. PHP access / error log might tell you the same thing. No harm trying what the linked post suggested. – Tim Mar 15 '17 at 20:01
  • @Tim I don't know what it exacly was, but the `SCRIPT_FILENAME` was a part of the problem. I still don't understand why a laravel subdirectory is so hard to make. – Jan Wytze Mar 15 '17 at 20:46

1 Answers1

1

After a lot of searching I finally found the answer:

server {
        listen 80;
        server_name example.nl *.example.nl;

        access_log /home/example/logs/access.log;
        error_log /home/example/logs/error.log;

        root /home/example/public_html;

        location ^~ /api {
                alias /home/example/public_api/public;
                try_files $uri $uri/ @laravel;
                index index.php;

                location ~ \.php$ {
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME /home/example/public_api/public/index.php;
                }
        }

        location @laravel {
                rewrite /api/(.*)$ /api/index.php?/$1 last;
        }


        location / {
                index index.html;
                try_files $uri$args $uri$args/ /index.html;
        }
}
Jan Wytze
  • 185
  • 1
  • 5
  • 2
    Perhaps you could point out the parts that you changed, to help anyone who has this problem in future. – Tim Mar 15 '17 at 20:44