【问题标题】:AngularJS and Laravel config for nginx. Running php from url segment用于 nginx 的 AngularJS 和 Laravel 配置。从 url 段运行 php
【发布时间】:2014-11-10 00:49:04
【问题描述】:

我在配置将从主域的 /api/ 段运行的 laravel 配置时遇到问题。 想法是在 subdomain.domain.com 主要 Angular 应用程序和 subdomain.domain.com/api/ laravel php 服务器配置中提供服务。

更新 #1

我已经设法使用这个配置从 /api/ 段运行 laravel

server {
    listen 80;
    server_name subdomain.domain.com;
    root /var/www/subdomain.domain.com/client/monkey/dist;

    access_log /var/www/subdomain.domain.com.access.log;
    error_log  /var/www/subdomain.domain.com.error.log;
    rewrite_log on;

    index index.php index.html;

    location /  {

        root /var/www/subdomain.domain.com/client/monkey/dist;
        index index.html;
        if (!-e $request_filename){ # handles page reload
            rewrite ^(.*)$ /index.html break;
        }
    }

    location /api/ {
        root /var/www/subdomain.domain.com/server/;
        try_files $uri $uri/ /api/index.php$is_args$args;
    }

    location ~ /api/.+\.php$ {

        root /var/www/subdomain.domain.com/server/;
        rewrite ^/api/(.*)$  /$1  break;

        fastcgi_pass                    unix:/var/run/php5-fpm.sock;
        include                         /etc/nginx/fastcgi_params;
        fastcgi_param                   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index                   index.php;
        fastcgi_param                   MONKEY_ENV production;
        fastcgi_split_path_info         ^(.+?\.php)(/.*)?$;
        # fastcgi_split_path_info         ^(.+\.php)(.*)$;
    }
}

唯一剩下的问题是,当我使用 subdomain.domain.com/api/segment1/segment... 时,我需要添加 routes.php

Route::group(['prefix' => 'api'], function () {
    // my routes settings
});

因为 laravel 从 suddomain.domain.com/ 作为根路径开始。如何重写 /api/ 以便 laravel 以 /api/ 作为路由起点?

【问题讨论】:

  • 你在用 Laravel 做任何事情外部 /api?如果没有,那你为什么不把 /api 设为 Laravel 的公用文件夹?
  • /api 路径仅适用于 laravel,因为这是我的 laravel 处理的后端。因此,如果我将 /server 重命名为 /api 它应该可以工作吗?我是否需要删除配置中的任何规则或添加额外的规则?

标签: php angularjs laravel nginx


【解决方案1】:

如果我了解您的问题,我想您可以使用 alias 指令 而不是 root 来解决这个问题。使用 root 指令,Nginx 只需获取通知 location 并将其连接到通知根路径的末尾。

所以,在你的情况下,这个 conf:

location /api/ {
   root /var/www/subdomain.domain.com/server/;
   try_files $uri $uri/ /api/index.php$is_args$args;
}

nginx 会将其解析为/var/www/subdomain.domain.com/server/api 作为最终路径。

使用 alias 指令,这个 conf,

location /api {
   alias /var/www/subdomain.domain.com/server/;
   try_files $uri $uri/ /api/index.php$is_args$args;
}

将被解析为任何旨在 '/api' 但 nginx 不会在它使用的最终路径中连接 'api' 字符串。

http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2014-09-06
    • 2014-01-01
    • 1970-01-01
    • 2015-01-05
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多