我正在尝试使用以下配置在NGINX上设置Laravel应用程序:
server {
listen 443 ssl;
server_name {MY-IP}; # The project is mounted on an IP
# ssl_certificate ...
# ssl_certificate_key ...;
root /var/www/myproject-ui;
index index.html index.htm index.php;
charset utf-8;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
location / {
try_files $uri $uri/ /index.html;
}
location /api {
alias /var/www/myproject/public;
try_files $uri $uri/ @laravelapi;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_read_timeout 120s; # Set the maximum timeout to 2 minutes
}
}
location /auth {
alias /var/www/myproject/public;
try_files $uri $uri/ @laravelapi;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_read_timeout 120s; # Set the maximum timeout to 2 minutes
}
}
location /logs {
alias /var/www/myproject/public;
try_files $uri $uri/ @laravelapi;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_read_timeout 120s; # Set the maximum timeout to 2 minutes
}
}
location /storage {
alias /var/www/myproject/public/storage;
}
location /tinker {
alias /var/www/myproject/public;
try_files $uri $uri/ @laravelapi;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_read_timeout 120s; # Set the maximum timeout to 2 minutes
}
}
location @laravelapi {
rewrite /api/(.*)?$ /api/index.php?$is_args$args last;
rewrite /auth/(.*)?$ /auth/index.php?$is_args$args last;
rewrite /logs/(.*)?$ /logs/index.php?$is_args$args last;
rewrite /storage/(.*)?$ /storage/index.php?$is_args$args last;
rewrite /tinker/(.*)?$ /tinker/index.php?$is_args$args last;
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
location = /robots.txt {
access_log off;
log_not_found off;
}
error_page 404 /index.php;
location ~ /\.(?!well-known).* {
deny all;
}
}
字符串
我需要路由/api
,/auth
和/logs
成为Laravel项目的一部分,而所有其他路由属于前端(静态HTML)。
我的问题是:
如何让NGINX/Laravel从根目录(/)而不是从定义的路径/api
或/auth
解释路由?
举例来说:
如果我试图访问路由/api/v1/myroute
,在Laravel中,我必须将其定义为/v1/myroute
,而这不是我想要的。我想将其定义为/api/v1/myroute
。问题是我有像log-viewer和web-tinker这样的包,它们都是从根定义的路由(/tinker
,/logs
),它们不能正常工作,因为我必须像这样访问它们:/tinker/tinker
个/logs/logs
我还想知道是否可以使用正则表达式来定义单个块中的所有路由,例如:^(api|auth|logs)
因为我已经试过了:^/(api|auth)
但对我没用
1条答案
按热度按时间b1zrtrql1#
如果您不需要API特定的安全功能,如CORS,Scantum和绕过Laravel框架提供的CSRF令牌
你可以改变文件App\Providers\RouteServiceProvider.php前缀
字符串
删除v1的前缀并运行cache clear artisan命令这将允许路由从Laravel运行带有API前缀的URL
所以,上面的代码按照你的Nginx配置将输出为
型
在Nginx中,你定义/API端点到laravel主机文件夹,这样它就可以在新的url中添加