大网数据广告活动
服务器运维问答

Nginx not getting root right directory

· 52CE 我爱测

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 …

问题原文

原帖链接 · Jan Wytze · CC BY-SA 3.0

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 · CC BY-SA 3.0

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;
        }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。