NGINX是一款高性能的开源Web服务器软件,也是一个反向代理服务器和负载均衡器。它可以在根目录和子目录中为Laravel API提供服务的NGINX配置如下:
server {
listen 80;
server_name your_domain.com;
root /path/to/laravel/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据实际情况修改php版本
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
解释上述配置:
listen 80;
:监听80端口,可以根据需要修改端口号。server_name your_domain.com;
:将your_domain.com替换为你的域名或IP地址。root /path/to/laravel/public;
:将/path/to/laravel/public替换为你的Laravel项目的公共目录路径。index index.php;
:指定默认的索引文件为index.php。location /
:处理根目录的请求。try_files $uri $uri/ /index.php?$query_string;
:尝试查找请求的文件,如果不存在则重写到index.php。location ~ \.php$
:处理以.php结尾的请求。fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
:将unix:/var/run/php/php7.4-fpm.sock替换为你的PHP-FPM套接字路径,确保与你的PHP版本一致。sudo service nginx restart
。现在,NGINX将为根目录和子目录中的Laravel API提供服务。你可以通过your_domain.com访问API,并根据需要添加更多的location块来支持其他功能或路由。
腾讯云相关产品推荐:
领取专属 10元无门槛券
手把手带您无忧上云