【问题标题】:Running Laravel and AngularJS on NGINX在 NGINX 上运行 Laravel 和 AngularJS
【发布时间】:2014-09-06 10:25:39
【问题描述】:

我为后端运行 Laravel,为前端运行 AngularJS。我的应用程序层次结构是

/application
    /app
    /bootstrap
    /vendor   
    /public
        /api 
            index.php <-- This is the Laravel "public" index
        index.html
        bootstrap.js

所以基本上,默认的 public 文件夹用于 AngularJS 前端,/public/api 用于 Laravel。

我不知道如何编写 NGINX 配置!这就是我目前所拥有的

server {
        listen   80;

        root PATH_TO_PUBLIC;
        index index.php index.html index.htm;

        server_name mysite.com;

        # AnuglarJS UI Front /index.html
        location / {
                try_files $uri $uri/
        }

        # Laravel Back-end /api/index.php
        location /api/ {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/html;
        }

        # pass the PHP scripts to FastCGI server listening on the php-fpm socket
        location ~ \.php$ {
                try_files $uri / =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

Laravel 中唯一有效的页面是 /api/index.php。即使像/api/index.php/my-resource 这样的东西也不起作用。

编辑

当我访问/api/index.php 时,Laravel 开始工作并开始工作。当我访问任何其他页面时(例如/api/sessions/api/index.php/sessions,它会加载主页/index.html

【问题讨论】:

  • 当你说不起作用时,你是什么意思? nginx 是否抛出 404 或其他错误,或者您是否收到 NotFoundHttpExceptions(来自 laravel)?还是别的什么?
  • 不,它会加载主页/index.html。我会更详细地更新问题
  • 你为什么要改变 Laravel 的结构?我不明白。

标签: angularjs laravel nginx laravel-4


【解决方案1】:

我的情况与您的情况有些不同,因为我已尽最大努力将 Angular 和 Laravel 应用程序保存在单独的目录和单独的源代码控制中,但我面临的挑战与您和我的完全相同解决方法如下。

主要区别在于 .php$ 位置中“别名”的使用。它告诉 nginx 使用不同的基本路径开始查找文件,这将有助于它正确找到 index.php。这是必要的,因为 Angular 和 Laravel 需要从不同的基本路径引用文件。

server {
    listen   80;

    root /path/to/application/app/public; # Path to Public
    index index.php index.html index.htm;

    server_name mysite.com;

    # AnuglarJS UI Front /index.html
    location / {
        try_files $uri $uri/
    }

    # Laravel Back-end /api/index.php
    location /api/ {
        try_files $uri $uri/ /index.php$query_string; # Minor adjustment here - a simpler solution that achieves the same thing
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on the php-fpm socket
    location ~ \.php$ {
        #---------------------------------------------------------------
        alias /path/to/application/app/public/api; # Path to directory containing index.php
        #---------------------------------------------------------------                try_files $uri / =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

【讨论】:

  • 我希望每个主机都应该有一个单独的 nginx conf 文件。我遇到了同样的情况并试图解决,一旦完成将发布正确的答案。
猜你喜欢
  • 2014-11-10
  • 2018-02-05
  • 2014-01-23
  • 2017-06-11
  • 2018-08-10
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2020-11-12
相关资源
最近更新 更多