[英]NGINX configuration for serving Laravel API at root as well as at subdirectory
下面是我在端點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;
}
}
但由於某種原因,我需要在http://api.example.com/Laravel-project-1/public 服務 API
Laravel-project-1 文件夾也有 resources 文件夾,其中包含一些圖像,這些圖像也需要在http://api.example.com/Laravel-project-1/resources/images/img.jpg 提供
如何配置nginx來實現呢?
注意:現在如果不能在http://api.example.com服務,那沒關系。 但它必須在http://api.example.com/Laravel-project-1/public
我敢打賭,如果您更改root /var/www/public;
root /var/www/public/public;
你的問題消失了。
您可能犯的錯誤是您已將整個項目放入/var/www/public
中,這意味着 Laravel 的public
位於該文件夾中,這是您的實際文檔根目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.