仅在 GET 请求 (nginx) 上授权标头不会到达 API

zer*_*udo 5 nginx laravel

我有一个建立在 laravel 上的应用程序,在本地它一切正常,但在服务器中它不能正常工作。

该应用程序托管在 nginx 上,PUT、POST、DELETE 请求能够向 API 发送授权标头,GET 请求除外。

这让它很奇怪,因为我知道在 apache 上您需要允许 Authorization 标头,而在 nginx 上则不需要。

我也调试了当我调用路由时Route::get('reports/{amount}','ReportsController@show'); 授权头没有到达 API 但它确实存在于请求头中。

当我将路由方法更改为 POST 时: Route::post('reports/{amount}','ReportsController@show');授权标头到达 API。

这是服务器的 nginx 配置:

server {
listen 80;
listen [::]:80;

client_max_body_size 10M;

#add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

add_header X-Frame-Options SAMEORIGIN;
add_header X-Frame-Options 'allow-from https://www.someweb.com';
    add_header X-Frame-Options 'allow-from https://www.someweb.com';
add_header X-Content-Type-Options nosniff;
    add_header 'Referrer-Policy' 'strict-origin';
    add_header X-XSS-Protection "1; mode=block";

root /var/www/html;

index index.html index.htm index.nginx-debian.html, index.php;

error_page 404 /404.html;

include snippets/fastcgi-php.conf;

location /security {
    alias /var/www/html/security/public;
    try_files $uri $uri/ @security;

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
                fastcgi_param SCRIPT_FILENAME $request_filename;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
}

location @security {
        rewrite /security/(.*)$ /security/index.php?/$1 last;
    }
}
Run Code Online (Sandbox Code Playgroud)

我对 nginx 不是很熟悉,但我没有看到对标头或 GET 请求的任何排除。有没有人遇到过这个问题?

有没有办法确定问题所在?由于我的浏览器有标头而 API 没有得到它,我认为这是服务器的问题,但我不知道如何修复它。

Mar*_*ton 5

也许您必须将此添加到可以接收的允许标头列表中,可在您的 Nginx 配置中进行配置。

add_header Access-Control-Allow-Headers "Authorization";
Run Code Online (Sandbox Code Playgroud)

几乎相同的船,可能会有同样的问题,就目前而言,我的开发人员环境有 allowHeaders设置为通配符。

您可能还需要设置允许的方法:

add_header Access-Control-Allow-Methods "GET POST DELETE OPTIONS";

或使用*(通配符):

add_header Access-Control-Allow-Methods *;