代码之家  ›  专栏  ›  技术社区  ›  zerociudo

授权头仅在GET请求时才到达API(nginx)

  •  2
  • zerociudo  · 技术社区  · 6 年前

    我有一个基于laravel的应用程序,它在本地运行良好,但在服务器上却不能正常工作。

    应用程序托管在nginx上,除了GET请求外,PUT、POST、DELETE请求都可以向API发送授权头。

    当我调用route时,我也进行了调试 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;
        }
    }
    

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

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

    1 回复  |  直到 6 年前
        1
  •  4
  •   Marc    5 年前

    也许你得把这个加到 允许邮件头

    add_header Access-Control-Allow-Headers "Authorization";
    

    几乎相同的船,可能会有相同的问题,因为它是我的开发环境 allowHeaders 设置为通配符。

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

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

    或使用 * (通配符)。。。