gpt4 book ai didi

php - 在 NGINX 子位置配置 laravel 时出现问题

转载 作者:行者123 更新时间:2023-12-02 04:23:04 24 4
gpt4 key购买 nike

我的应用程序包括一个只有 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 内部服务器错误

这是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 路由?

最佳答案

为vue.js解释器添加location/

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 作为示例。将其设置为您的节点监听端口。

关于php - 在 NGINX 子位置配置 laravel 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57852260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com