简体   繁体   English

NGINX 配置,用于在根目录和子目录中服务 Laravel API

[英]NGINX configuration for serving Laravel API at root as well as at subdirectory

Below is my configuration for serving Laravel API at endpoint http://api.example.com下面是我在端点http://api.example.Z4D236D9A2D102C5FE6AD1A14FZ服务 Laravel API 的配置

server {
  listen 80;
  server_name api.example.com;
  index index.php index.html;
  error_log  /var/log/nginx/error.log;
  access_log /var/log/nginx/access.log;
  root /var/www/public;


  location ~ \.php$ {
    try_files $uri =404;

    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Authorization';
    add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
    if ($request_method = 'OPTIONS') {
      add_header 'Access-Control-Max-Age' 1728000;
      add_header 'Content-Type' 'text/plain charset=UTF-8';
      add_header 'Content-Length' 0;
      return 204;
    }

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass app:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }
  location / {
    try_files $uri $uri/ /index.php?$query_string;
    gzip_static on;
  }
}

But due to some reason I need to serve API at http://api.example.com/Laravel-project-1/public但由于某种原因,我需要在http://api.example.com/Laravel-project-1/public 服务 API

And Laravel-project-1 folder also has resources folder that contains some images that too needs to be served at http://api.example.com/Laravel-project-1/resources/images/img.jpg Laravel-project-1 文件夹也有 resources 文件夹,其中包含一些图像,这些图像也需要在http://api.example.com/Laravel-project-1/resources/images/img.jpg 提供

How to configure nginx to achieve it?如何配置nginx来实现呢?

NOTE: right now if it's not possible to serve at http://api.example.com then it's okay.注意:现在如果不能在http://api.example.com服务,那没关系。 But it must be served at http://api.example.com/Laravel-project-1/public但它必须在http://api.example.com/Laravel-project-1/public

I bet if you change root /var/www/public;我敢打赌,如果您更改root /var/www/public; to root /var/www/public/public; root /var/www/public/public; your issue disappears.你的问题消失了。

The mistake you're probably making is that you've put your entire project into /var/www/public and that means that Laravel's public folder resides in that folder, which is your actual document root.您可能犯的错误是您已将整个项目放入/var/www/public中,这意味着 Laravel 的public位于该文件夹中,这是您的实际文档根目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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