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

Laravel Octane搭配Nginx无法加载网页问题求助

Laravel Octane(Swoole)迁移后网页弹出下载index.php的解决办法

问题根源

API能正常工作,但访问网页会触发index.php下载,核心原因是Nginx没把动态请求正确转给Octane服务,反而直接把本地的index.php文件当静态资源返回了;另外你贴的Nginx配置开头缺了关键的map块,这也会导致请求转发逻辑出问题。

修复步骤

1. 补全Nginx的map配置块

在你的server配置块之前必须加上这段,这是Octane处理长连接的基础配置:

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

2. 修正Nginx服务器配置

把原来的server块替换成下面的内容,重点补全了响应头设置,确保浏览器识别返回内容为HTML:

server {
    listen 80;
    listen [::]:80;
    server_name domain.com;
    server_tokens off;
    root /home/dir/public;

    index index.php;

    location = /index.php {
        try_files /not_exists @octane;
    }

    location / {
        try_files $uri $uri/ @octane;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log /dir/to/error/log;

    error_page 404 /index.php;

    location @octane {
        set $suffix "";

        if ($uri = /index.php) {
            set $suffix ?$query_string;
        }

        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        # 确保和Octane启动的端口一致,默认是8000
        proxy_pass http://127.0.0.1:8000$suffix;
        # 强制告诉浏览器返回内容是HTML,避免被识别为下载文件
        proxy_set_header Content-Type text/html;
    }
}

3. 检查Octane服务状态

先确认Octane是不是真的在8000端口跑着:

# 查看Octane进程
ps aux | grep octane
# 查看端口占用
ss -tulpn | grep 8000

如果没运行,重新启动Octane:

php artisan octane:start --server=swoole --port=8000

4. 刷新配置与缓存

  • 重新加载Nginx配置:
sudo nginx -s reload
  • 浏览器按Ctrl+Shift+R强制刷新,清除缓存

额外注意事项

  • 确认/home/dir/public目录的权限,Nginx进程得能读这个目录
  • 静态资源(CSS、JS、图片)Nginx会直接返回,不用走Octane,这是正常的,能提升性能
  • 如果用了HTTPS,记得把listen改成443,加上SSL相关配置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 04:45:11