提问人:Gerardo 提问时间:11/17/2023 更新时间:11/17/2023 访问量:22
NGINX:从路径 (/api) 设置项目,并在代码中从根 (/) 定义路由
NGINX: Set up a project from a path (/api) and define routes in the code from the root (/)
问:
我正在尝试使用以下配置在 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;
}
}
我需要路由 , 和 成为 Laravel 项目的一部分,而所有其他路由都属于前端(静态 HTML)。/api
/auth
/logs
我的问题是:
如何使 NGINX/Laravel 从根 (/) 而不是从定义的路径或?/api
/auth
例如:
如果我尝试访问路由,在 Laravel 中,我只能将其定义为 ,这不是我想要的。我想将其定义为 .问题是我有像 log-viewer 和 web-tinker 这样的包,其中包含从根 (, ) 定义的路由,但它们无法正常工作,因为我必须像这样访问它们:/api/v1/myroute
/v1/myroute
/api/v1/myroute
/tinker
/logs
/tinker/tinker
/logs/logs
我还想知道是否可以使用正则表达式来定义单个块中的所有路由,例如:
^(api|auth|logs)
因为我已经尝试过:
^/(api|auth)
它对我不起作用。
答:
如果您不需要 API 特定的安全功能,例如 CORS、Scantum 和绕过 laravel 框架提供的 CSRF 令牌
可以在文件 App\Providers\RouteServiceProvider.php 中更改前缀
Route::prefix('api/v1') ->namespace($this->namespace) ->group(base_path('routes/api.php'));
删除 v1 的前缀并运行 cache clear artisan 命令 这将允许路由以 Laravel 的形式运行 api 前缀的 url
因此,根据您的 Nginx 配置,上述代码将输出为
old
domain.com/api/api/v1/myroute
new
domain.com/api/v1/myroute
在 Nginx 中,您将 /api 端点定义为 laravel 主机文件夹,以便将其附加到新 url 中
评论
/api
logs
/auth
评论