Laravel在nginx上的子文件夹中

时间:2015-09-13 20:10:50

标签: php laravel nginx config

我正在尝试配置nginx,以便我可以运行/的主网站,然后从/ myapp运行一个新的Laravel网站。我已经摆脱了应用程序本身(公共文件夹部分)中/ 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;
}

}

1 个答案:

答案 0 :(得分:2)

经过几天的修修补补后,我终于解决了这个问题。它现在正确加载主站点,子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;
    }

}