同一服务器部署前端与API的Nginx配置问题:API非index.php路径被路由至前端index.php
Hey there! Let’s break down what’s causing your API routing problem and fix it step by step. The core issue here is how Nginx resolves file paths for your /api/v1 routes, plus how it handles internal redirects when a requested path doesn’t exist.
What’s Wrong with Your Current Config?
Your /api/v1 location uses root /var/www/apps/api/v1, which is the mistake. When Nginx processes a request like /api/v1/your-endpoint, it appends the entire request path to the root directory—so it looks for /var/www/apps/api/v1/api/v1/your-endpoint, which doesn’t exist. Then the try_files directive falls back to /apps/api/v1/index.php, but since the server’s root is /var/www, this triggers a new location match. The path doesn’t hit the /api/v1 location again, so it lands in the global / location’s try_files rule, sending the request to your frontend’s /index.php instead of the API’s.
The Fixed Config
Here’s the adjusted Nginx config that will correctly route API requests to your backend index.php:
error_log /var/log/nginx/error.log; server { listen 443 ssl; server_name abc.com; # Certificate ssl_certificate /etc/nginx/ssl/abc.com.pem; # Private Key ssl_certificate_key /etc/nginx/ssl/abc.com.key; root /var/www; index index.html index.php; # Handle API routes correctly location ^~ /api/v1 { alias /var/www/apps/api/v1; try_files $uri $uri/ /api/v1/index.php?$query_string; # Process PHP files within the API directory location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; } } location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; } }
Key Changes Explained
aliasinstead ofroot: Unlikeroot,aliasreplaces the matched/api/v1part of the request path with the directory path. So/api/v1/your-endpointcorrectly maps to/var/www/apps/api/v1/your-endpoint, which is exactly where your API files live.- Adjusted
try_filesfallback: The/api/v1/index.php?$query_stringensures that when a requested API path doesn’t exist (like most framework routes), it redirects to the API’s index.php (not the frontend’s) and preserves any query parameters your backend might need. - Nested PHP location: Since
location ^~ /api/v1has higher priority than the global PHP location, we nested a PHP handler inside it to make sure API PHP files are processed by PHP-FPM correctly. Without this, Nginx would skip the global PHP location for API requests.
How to Test
- Save the updated config file.
- Test the config for syntax errors with
nginx -t. - If everything checks out, restart Nginx with
systemctl restart nginx. - Now try accessing
https://abc.com/api/v1/your-api-endpoint—it should route to your API’s index.php instead of the frontend’s.
内容的提问来源于stack exchange,提问作者AmmyTech

