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

Ubuntu下Nginx同时托管.NET API与Laravel的配置问题求助

问题描述

在Ubuntu服务器的Nginx上部署了两个应用:给Flutter用的.NET 7 API,以及管理数据库的Laravel后台面板。两个应用单独运行都正常,但同时运行时遇到问题:

  • 如果其中一个配置的server_name是服务器IP,只有这个应用能访问
  • 如果两者server_name都设为服务器IP,Laravel会接管所有请求,API请求会返回Laravel的404错误
解决方案

下面提供两种可行的解决方法,根据实际情况选择:

方案1:通过路径前缀区分(无需域名)

给API设置专属路径前缀(比如/api),让Nginx根据请求路径转发到对应应用,适合只用IP访问的场景。

修改.NET API的Nginx配置(/etc/nginx/sites-available/default)

server {
    listen  80;
    server_name 65.2*.***.***;

    # 所有以/api开头的请求转发到.NET API
    location /api {
        # 去掉/api前缀后转发到后端,避免后端收到带/api的路径
        rewrite ^/api/(.*)$ /$1 break;
        proxy_pass         http://localhost:8888;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }   
}

修改Laravel后台的Nginx配置(/etc/nginx/sites-available/adminPanel)

保持server_name不变,让它处理根路径及其他非/api的请求:

server {
    listen 80;
    server_name 65.2*.***.***;
    root /var/www/adminPanel/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    index index.php;
 
    # 处理根路径及非/api的请求
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 1y;
        add_header Cache-Control "public, no-transform";
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

方案2:通过子域名区分(需域名支持)

如果有域名,可以给两个应用分配不同子域名(比如api.yourdomain.com和admin.yourdomain.com),先确保域名已解析到服务器IP,再修改配置:

.NET API的配置

server {
    listen  80;
    server_name api.yourdomain.com;

    location / {
        proxy_pass         http://localhost:8888;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }   
}

Laravel后台的配置

server {
    listen 80;
    server_name admin.yourdomain.com;
    root /var/www/adminPanel/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    index index.php;
 
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 1y;
        add_header Cache-Control "public, no-transform";
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
验证步骤
  1. 保存修改后的配置文件
  2. 测试Nginx配置是否合法:sudo nginx -t
  3. 重启Nginx生效:sudo systemctl restart nginx
  4. 测试访问:
    • 方案1:访问http://你的IP/api测试API,访问http://你的IP测试Laravel后台
    • 方案2:分别访问对应的子域名测试两个应用

内容的提问来源于stack exchange,提问作者Bashar Al-shaibani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 06:31:05