You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同一服务器部署前端与API的Nginx配置问题:API非index.php路径被路由至前端index.php

Fixing Nginx API Routing Issues for Your Frontend + API Setup

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

  • alias instead of root: Unlike root, alias replaces the matched /api/v1 part of the request path with the directory path. So /api/v1/your-endpoint correctly maps to /var/www/apps/api/v1/your-endpoint, which is exactly where your API files live.
  • Adjusted try_files fallback: The /api/v1/index.php?$query_string ensures 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/v1 has 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

  1. Save the updated config file.
  2. Test the config for syntax errors with nginx -t.
  3. If everything checks out, restart Nginx with systemctl restart nginx.
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 11:32:37