gpt4 book ai didi

angularjs - 在 NGINX 上运行 Laravel 和 AngularJS

转载 作者:行者123 更新时间:2023-12-05 07:58:23 25 4
gpt4 key购买 nike

我为后端运行 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.

最佳答案

我的情况与你的略有不同,因为我已尽最大努力将 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;
}
}

关于angularjs - 在 NGINX 上运行 Laravel 和 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24771516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com