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

Nginx反向代理Apache与Spring Boot时/api接口请求返回404问题排查求助

Nginx反向代理Apache与Spring Boot时/api接口请求返回404问题排查求助

各位大佬好,我现在碰到一个反向代理的棘手问题,折腾好一阵都没搞定,想请大家帮忙分析下。

我的技术栈是Angular前端 + Spring Boot后端,用Nginx做统一反向代理:

  • Angular项目计划通过Nginx转发到Apache服务
  • Spring Boot的内嵌服务器也通过Nginx做反向代理转发

现在前端Angular能正常加载,但调用Spring Boot的/api接口时一直返回404。不过我直接用curl localhost:8085/api/hello能正常拿到响应,甚至把localhost通过隧道暴露到公网也能正常访问,说明后端本身没问题,肯定是Nginx或者相关配置哪里出了疏漏。

我的Nginx配置:

server {
    listen       154.62.107.99:80 default;
    server_name  _;
    access_log  /var/log/nginx/154.62.107.99.log main;
    error_log  /var/log/nginx/error.log debug;

    location / {
        proxy_pass  http://154.62.107.99:8080;
    }

    location /api/ {
        # Simple requests
        if ($request_method ~* "(GET|POST)") {
            add_header "Access-Control-Allow-Origin"  "*";
        }
        proxy_pass http://0.0.0.0:8085/;
        proxy_buffering off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;
    }
}

server {
    listen      154.62.107.99:443 ssl http2;
    server_name _;
    ssl_certificate      /usr/local/hestia/ssl/certificate.crt;
    ssl_certificate_key  /usr/local/hestia/ssl/certificate.key;
    access_log  /var/log/nginx/154.62.107.99_https.log main;
    error_log  /var/log/nginx/error_https.log debug;

    # return 301 http://$host$request_uri;

    location / {
        #  root /var/www/document_errors/;
        # Handle preflight requests (OPTIONS)
        if ($request_method = OPTIONS ) {
            add_header "Access-Control-Allow-Origin"  "*";
            add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
            add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
            return 200;
        }
        # Handle other requests
        # Add any additional configuration for handling other requests
        # Redirect other requests
        return 301 http://$host$request_uri;
    }

    location /error/ {
        alias /var/www/document_errors/;
    }
}

我的预期逻辑是:

  • 所有/开头的请求转发到Apache(端口8080)
  • 所有/api/开头的请求转发到Spring Boot(端口8085)

目前的观察和疑惑:

  1. 停掉Nginx后网站直接打不开,说明请求确实经过Nginx
  2. 停掉Apache会返回500内部错误,这符合预期
  3. 但我把Nginx里location /下的proxy_pass http://154.62.107.99:8080注释掉后,前端居然还能加载!这完全超出我的预期,我本来以为前端是通过Nginx转发到Apache的,现在搞不清前端到底是从哪加载的了。

更新补充:Apache配置情况

后来我查了Apache的配置,发现有两个相关的VirtualHost:

第一个全局的:

Listen 154.62.107.99:8443
Listen 154.62.107.99:8080

<VirtualHost 154.62.107.99:8080>
    ServerName 154.62.107.99
    DocumentRoot /var/www/html/
    Alias /error/ /var/www/document_errors/
</VirtualHost>

<VirtualHost 154.62.107.99:8443>
    ServerName 154.62.107.99
    DocumentRoot /var/www/html/
    Alias /error/ /var/www/document_errors/
    SSLEngine on
    SSLVerifyClient none
    SSLCertificateFile         /usr/local/hestia/ssl/certificate.crt
    SSLCertificateKeyFile      /usr/local/hestia/ssl/certificate.key
</VirtualHost>

这个配置里的/var/www/html只有一个测试用的success文件,根本不是我的Angular dist包。

然后还有一个域名相关的配置文件(在/domains目录下),这个配置的DocumentRoot是对的:

<VirtualHost 154.62.107.99:8080>
    ServerName nirbhaysingh.dev
    ServerAlias www.nirbhaysingh.dev
    ServerAdmin info@nirbhaysingh.dev
    DocumentRoot /home/admin/web/nirbhaysingh.dev/public_html
    ScriptAlias /cgi-bin/ /home/admin/web/nirbhaysingh.dev/cgi-bin/
    Alias /vstats/ /home/admin/web/nirbhaysingh.dev/stats/
    Alias /error/ /home/admin/web/nirbhaysingh.dev/document_errors/
    #SuexecUserGroup admin admin
    CustomLog /var/log/apache2/domains/nirbhaysingh.dev.bytes bytes
    CustomLog /var/log/apache2/domains/nirbhaysingh.dev.log combined
    ErrorLog /var/log/apache2/domains/nirbhaysingh.dev.error.log
    IncludeOptional /home/admin/conf/web/nirbhaysingh.dev/apache2.forcessl.conf*

    <Directory /home/admin/web/nirbhaysingh.dev/stats>
        AllowOverride All
    </Directory>

    <Directory /home/admin/web/nirbhaysingh.dev/public_html>
        AllowOverride All
        Options +Includes -Indexes +ExecCGI
    </Directory>

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php/php8.0-fpm-nirbhaysingh.dev.sock|fcgi://localhost"
    </FilesMatch>

    SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0
    IncludeOptional /home/admin/conf/web/nirbhaysingh.dev/apache2.conf_*
    IncludeOptional /etc/apache2/conf.d/*.inc
</VirtualHost>

我现在猜测可能是Nginx的转发没生效,前端其实是直接通过Nginx的域名配置(而不是转发到Apache)加载的?现在还在排查,有进展会更新,也希望大佬们能给点思路!

备注:内容来源于stack exchange,提问作者sarp

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.21 08:33:12