php - 在 NGINX 子定位中配置 laravel 时出现问题
问题描述
我的应用程序包含一个仅 vue 的前端,其 api 是用 laravel 编写的。我想这样设置它的方式:
http://myapp.local
--> 指向 vue 前端。
http://myapp.local/api
--> 指向 laravel 应用 api 路由。
这是我的 nginx 配置:
server {
listen 80;
server_name myapp.local *.myapp.local;
root /var/www/myapp-frontend/dist;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location ^~ /api {
alias /var/www/myapp-api/public;
try_files $uri $uri/ @api;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location @api {
rewrite /api/(.*)$ /api/index.php/$1 last;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
http://myapp.local/api
点击打开根 laravel 路线。但是,如果我在我的应用程序中打开任何其他路由,我会收到500 Internal Server Error。
这是 nginx 的错误日志中的错误:
2019/09/09 15:58:18 [error] 20954#20954: *10 rewrite or internal redirection cycle while redirect to named location "@api", client:
127.0.0.1, server: myapp.local, request: "GET /api/admin/features HTTP/1.1", host: "myapp.local"
更新
我设法让它以某种方式工作,这就是更新后的配置
server {
listen 80;
server_name *.myapp.local;
root /var/www/myapp-api/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.php;
charset utf-8;
add_header X-debug-request-filename "$request_filename" always;
add_header X-debug-document-root "$document_root" always;
add_header X-debug-fastcgi-script-name "$fastcgi_script_name" always;
add_header X-debug-query-string "$query_string" always;
location / {
root /var/www/myapp-frontend/dist;
try_files $uri $uri/ /index.html = 404;
}
location /api {
alias /var/www/myapp-api/public;
try_files $uri $uri/ @api;
add_header X-debug-request-filename "$request_filename" always;
add_header X-debug-document-root "$document_root" always;
add_header X-debug-fastcgi-script-name "$fastcgi_script_name" always;
add_header X-debug-query-string "$query_string" always;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location @api {
rewrite ^/api/(.*)$ /api/index.php?/$1 last; # THIS IS THE IMPORTANT LINE
}
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 ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
不过,我有一个小问题,每次访问 api 路由时,我都需要写/api
两次才能获得正确的地址。例如像这样http://myapp.local/api/api/login
。/api/login
是实际的路由,并且http://myapp.local/api/
指向 laravel 应用程序,所以需要写/api
两次才有意义。但是无论如何有可能只使用http://myapp.local/api/login
访问/api/login
路线吗?
解决方案
location /
为 vue.js 解释器添加
server {
listen 80;
server_name myapp.local *.myapp.local;
root /var/www/myapp-frontend/dist;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location ^~ /api {
alias /var/www/myapp-api/public;
try_files $uri $uri/ @api;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location @api {
rewrite /api/(.*)$ /api/index.php/$1 last;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
在此处使用端口3000
作为示例。将其设置为您的节点侦听端口。
推荐阅读
- google-chrome-extension - Chrome 商店扩展,设置从公开到私人可见性选项 - 更新怎么样?
- javascript - 如何在 React js 中为选择字段设置默认值
- spring-boot - 使用Pageable查询后的JPA排序查询结果
- django - Django Rest Framework 非模型序列化程序显示选择性字段
- c# - 双击时编译的c#文件不打开
- c# - 显示在 Label.Text 中的 FOR 循环内运行的操作的进度百分比
- python - Python类将空行添加到最后一个值
- python - 没有 CSS 的 Django 3 管理链接即将到来
- flutter - Dart if(A is B) 条件不能正常工作
- python - peewee IntegrityError:唯一约束失败:userpost.user_id,userpost.post_id