简体   繁体   中英

AngularJS and Laravel config for nginx. Running php from url segment

I have problem configuring laravel configuration that would run from /api/ segment of main domain. Idea is to serve at subdomain.domain.com main Angular aplication and in subdomain.domain.com/api/ laravel php server configuration.

UPDATE #1

I have managed to run laravel from /api/ segment with this configuration

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)(.*)$;
    }
}

Only problem that has left is that when i'm using subdomain.domain.com/api/segment1/segment... then I need to add in routes.php

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

As laravel is starting from suddomain.domain.com/ as root path. How can i rewrite /api/ so that laravel takes /api/ as routing starting point ?

If I hava understanded your problem, I guess that you could solve this with the alias directive instead root . Using the root directive Nginx just takes de informed location and concat it at the end of the informed root path.

So, in your case, this conf:

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

nginx would resolved it to /var/www/subdomain.domain.com/server/api as the final path.

Using the alias directive, this conf,

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

would be resolved to anything that's aim to '/api' but nginx is not concatening the 'api' string in the final path it uses.

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

Hopes this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM