从目录提供Laravel服务

时间:2018-07-27 03:16:46

标签: php laravel nginx nginx-location

我目前正在使用通过Nginx提供服务的javascript框架,例如在以下网址上

www.myjsapp.com

我还使用Laravel 5.6构建API。

我希望能够在以下URL上提供Laravel API,而不是构建2个主机,一个用于JS应用程序,一个用于Laravel。

www.jsapp.com/api

这是否可行,还是我必须始终使用2台主机?

myjsappcom的nginx服务器块如下;

server {
    listen 80;
    listen 443 ssl http2;
    server_name .myjsapp.com;
    root "/home/project/myjsapp";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/myjsapp.com-error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;


        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }

    location ~ /\.ht {
        deny all;
    }

    ssl_certificate     /etc/nginx/ssl/myjsapp.com.crt;
    ssl_certificate_key /etc/nginx/ssl/myjsapp.com.key;
}

1 个答案:

答案 0 :(得分:2)

您可以在nginx配置文件中使用location块来分隔服务器: 不同的/location块将捕获不同的url方案,并将它们传递给相应的服务器(节点或laravel)。

server {
    server_name mysjapp.com;

    #other configurations like root, logs

    location / {
        #node server config

    }

    location /api {
        #laravel server config
    }
}
相关问题