php - Laravel 位于 nginx 的子文件夹中

标签 php laravel nginx config

我正在尝试配置 nginx,以便可以在/之外运行主网站,然后在/myapp 之外运行新的 Laravel 站点。我已经摆脱了应用程序本身(其中的公共(public)文件夹部分)对/myapp/public 的需要,所以我需要的只是正确提供/myapp 文件夹。现在我可以正确访问主页/,但是/myapp 上的新 Laravel 应用程序会抛出 404:nginx/1.4.6 (Ubuntu)。我在下面发布了我的 nginx 配置。如果有帮助的话,它正在 AWS 上的 Ubuntu 上运行。

谢谢!

[更新!] - 我现在已经在根目录和/myapp Laravel 应用程序文件夹中一切正常,除了资源似乎不起作用。例如http://example.com/myapp/public/css/app.css 404,其他 js 和 css 文件也是如此。不确定是什么原因造成的。我已确保公用文件夹具有完全权限,至少出于测试目的。还检查了所有者/组,这与我所有其他运行良好的文件相同,ubuntu:ubuntu。

server {

listen  80;
server_name *.example.com;
set $root_path '/usr/share/nginx/html/';
root $root_path;

index index.php index.html index.htm;

try_files $uri $uri/ @rewrite;

location @rewrite {
    rewrite ^/(.*)$ /index.php?_url=/$1;
}

location ~ \.php {

    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index /index.php;

    include /etc/nginx/fastcgi_params;

    fastcgi_split_path_info       ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO       $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
    allow all;
}

rewrite ^/myapp/?(.*)$ /myapp/index.php?$1 last;

location /myapp {
    allow all;
}

location ~ /\.ht {
    deny all;
}

}

最佳答案

经过几天的修补,我终于解决了这个问题。现在它可以正确加载主站点、子 Laravel 站点以及所有资源。这是最终的配置,供引用。

server {

    listen  80;
    server_name *.example.com;
    set $root_path '/usr/share/nginx/html/';
    root $root_path;

    index index.php index.html index.htm;

    try_files $uri $uri/ @rewrite;

    location @rewrite {
        rewrite ^/myapp/?(.*)$ /index.php?$1;
    }

    location ~ \.php {

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index /index.php;

        include /etc/nginx/fastcgi_params;

        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        allow all;
    }

    location ~* ^/myapp/?(.+)/(css|img|js|flv|swf|download)/(.+)$ {
        allow all;
    }

    location ~ \.css {
        add_header  Content-Type    text/css;
    }

    location ~ \.js {
        add_header  Content-Type    application/x-javascript;
    }

    location /myapp {
        allow all;
        rewrite ^/myapp/?(.*)$ /myapp/index.php?$1 last;
    }

    location ~ /\.ht {
        deny all;
    }

}

关于php - Laravel 位于 nginx 的子文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32554089/

相关文章:

php - hasManyThrough 不工作

PHP 页面不会重新加载 # HTML

php - Laravel 5.4 PHP 要求(5.6.30 与 5.6.40)

php - Laravel 使用带参数的模型工厂生成对象

PHP Laravel GraphQL 查询声明不兼容问题

ubuntu - 当我使用 client_body_temp_path 时,Linux(Ubuntu)和 Nginx 权限在另一个磁盘上

nginx - WebSocket 连接到 'ws://localhost/_next/webpack-hmr' 失败 : WebSocket is closed before the connection is established in Next. js 与 Nginx

nginx - 将 nginx rtmp 片段发送到 WebRTC

php - 使用 Woocommerce 方法保存产品属性不同步到前端

php - 我想使用 paypal 支付包裹销售,哪种 paypal 方式最好?