【问题标题】:Serving Laravel from a directory从目录服务 Laravel
【发布时间】:2019-01-04 02:58:59
【问题描述】:

我目前正在使用一个使用 Nginx 提供的 javascript 框架,例如在以下 url 上

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;
}

【问题讨论】:

  • 在 nginx 中为你的 nodejs 创建一个反向代理,然后你将两者都放在同一个域下。
  • @N.B.这是在 Nginx 中完成的吗?
  • 是的。在 Nginx 中。你可以用谷歌搜索配置示例。

标签: php laravel nginx nginx-location


【解决方案1】:

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

server {
    server_name mysjapp.com;

    #other configurations like root, logs

    location / {
        #node server config

    }

    location /api {
        #laravel server config
    }
}

【讨论】:

  • 嘿,谢谢你的回答,这是有道理的,但我对编辑 nginx 文件一无所知......如果我将我的原始站点可用文件粘贴到 myjsapp.com 域,你会能告诉我你的意思吗?
  • 根据我对 nginx 的了解(当我必须自己配置它时,我经历了很多试验和错误),你应该用 location /api { try_files $uri $uri/ /index.php?$query_string; } 更改代码中的 location / { try_files $uri $uri/ /index.php?$query_string; } 然后你会编写一个新的location / {} 块,您将根据 nginx 的 node.js 要求在其中放置配置。我自己没有使用过节点,但我相信你可以在 SO 的某个地方找到配置示例。希望有所帮助
猜你喜欢
  • 2020-08-21
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 2017-06-28
相关资源
最近更新 更多