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

Vagrant+Laravel9+Vue.js3环境下Nginx配置异常求助

问题描述

之前为解决Vue.js页面刷新404问题,修改了/etc/nginx/sites-available/default配置文件,尝试恢复原状后出现异常:首页可正常显示,但调用后端接口(如http://localhost:8081/profiles)获取数据库信息时,返回的却是Nginx欢迎页面。已清除Laravel和Nginx缓存,怀疑配置仍存在问题,当前Nginx配置如下:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 8081 default_server;
        listen [::]:8081 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /vagrant/app/shop-web/public;

        # Add index.php to the list if you are using PHP
        index index.php;

        server_name _;
        client_max_body_size 20M;
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                 try_files $uri $uri/ /index.php?$query_string;

        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }
        ## denay access hidden files
        location ~ /\.(?!well-known).* {
                 deny all;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}
排查与解决方案

1. 确认Nginx配置生效

  • 检查配置语法是否正确:
    sudo nginx -t
    
    出现test is successful才正常,否则根据提示修复语法错误。
  • 重启Nginx加载新配置:
    sudo systemctl restart nginx
    
  • 检查启用的配置文件,避免端口监听冲突:
    ls -l /etc/nginx/sites-enabled/
    
    若存在除当前default外的其他配置文件,执行sudo unlink /etc/nginx/sites-enabled/[多余文件名]删除无效链接。

2. 验证PHP-FPM服务状态

接口请求返回欢迎页,大概率是PHP-FPM未正常处理请求:

  • 检查PHP-FPM是否运行:
    sudo systemctl status php8.2-fpm
    
    若显示inactive,启动服务:
    sudo systemctl start php8.2-fpm
    
  • 确认socket文件存在:
    ls -l /run/php/php8.2-fpm.sock
    
    若不存在,打开/etc/php/8.2/fpm/pool.d/www.conf,找到listen指令,确保值为/run/php/php8.2-fpm.sock,修改后重启PHP-FPM:
    sudo systemctl restart php8.2-fpm
    

3. 调整Nginx配置适配前后端混合部署

当前配置未区分前端SPA路由和后端接口请求,导致接口未正确转发到Laravel。建议调整配置如下(适配Vue+Laravel同目录部署):

##
# Nginx配置参考提示(可保留)
##

# Default server configuration
server {
        listen 8081 default_server;
        listen [::]:8081 default_server;

        root /vagrant/app/shop-web/public;

        # 优先加载前端index.html,再加载后端index.php
        index index.html index.php;

        server_name _;
        client_max_body_size 20M;

        # 处理Vue SPA路由,解决刷新404问题
        location / {
                try_files $uri $uri/ /index.html;
        }

        # 处理后端接口(假设接口以/api开头,可根据实际调整)
        location /api {
                try_files $uri $uri/ /index.php?$query_string;
        }

        # 转发PHP请求到FastCGI服务
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        }

        # 禁止访问隐藏文件
        location ~ /\.(?!well-known).* {
                 deny all;
        }
}

注意:如果后端接口没有/api前缀(比如直接用/profiles),可将location /api替换为location ~ ^/(profiles|其他接口路径),或者调整location /的try_files顺序为try_files $uri $uri/ /index.php?$query_string /index.html;,但后者可能影响前端SPA路由,建议给接口统一加前缀。

4. 清理项目缓存与检查目录

  • 清理Laravel路由和配置缓存:
    php artisan route:clear
    php artisan config:clear
    
  • 检查Laravel的public目录,确保不存在与接口路径同名的文件或目录(比如public/profiles),否则Nginx会直接返回该文件,不会转发到index.php。

内容的提问来源于stack exchange,提问作者Alexander

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 19:56:13