【问题标题】:Laravel 6.0.* Passport doesn't work with php-fpm+nginx : "The GET method is not supported for this route" when POST sentLaravel 6.0.* Passport 不适用于 php-fpm+nginx:发送 POST 时“此路由不支持 GET 方法”
【发布时间】:2020-03-30 20:30:56
【问题描述】:

对于我的后端 API 应用程序,我有 Laravel 6 和 Laravel Passport oAuth2 插件。我是我的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\Component\HttpKernel\Exception\ MethodNotAllowedHttpException 不支持 GET 方法 路线。支持的方法:POST。”

除此之外,其他 API 请求工作正常,所以可能不是 CORS 限制

但是,如果我运行 php artisan serve 并将 POST 发送到 http://localhost:8080/oauth/token,它会按预期工作

【问题讨论】:

    标签: laravel nginx oauth laravel-passport


    【解决方案1】:

    我对 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也必须删除

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-11
      • 2021-08-05
      • 2020-06-05
      • 2020-03-21
      • 2021-01-10
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多