对于我的后端API应用程序,我有带有Laravel Passport oAuth2插件的Laravel 6。我的routes/web.php
我正在使用Auth::routes();
进行所有oAuth路由。我的Nginx配置(在Amazon实例上运行):
/etc/nginx/conf.d/app.conf
server {
server_name my-app-domain.net;
listen 80;
client_max_body_size 20M;
include /etc/nginx/default.d/*.conf;
root /var/www/app/public;
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.php;
location ~ /\. {
deny all;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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;
error_page 500 502 503 504 /index.php;
location ~* \.(?:ico|css|otf|gif|jpe?g|png)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
root /var/www/app/public;
}
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
如果我使用grant_type
和其他登录凭据将POST发送到http://<domain name>/oauth/token
,则会收到此奇怪的错误:
“ Symfony \组件\ HttpKernel \异常\MethodNotAllowedHttpException此方法不支持GET方法路线。支持的方法:POST。“
此外,其他API请求都可以正常工作,因此可能不是CORS限制
但是,如果我运行php artisan serve
并将POST发送到http://localhost:8080/oauth/token
,它将按预期工作
我对CORS错了。尽管我已经安装了barryvdh/laravel-cors
软件包,并且设置设置为“ *”,但CORS仍然是问题。所以我不得不调整一下Nginx
server {
server_name my-app-domain.net;
listen 80;
client_max_body_size 20M;
include /etc/nginx/default.d/*.conf;
root /var/www/app/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Headers' '*' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
index index.html index.php;
charset utf-8;
.....
插件barryvdh/laravel-cors
也必须删除