在Gatsby网站上关闭GraphQL api的nginx认证。

问题描述 投票:0回答:1

我在关闭GraphQL api的验证方面遇到了困难。它一直让构建失败。这是我现在的情况,我认为是正确的。

location / {
    try_files $uri $uri/ @rewrites;
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

location ^~ /api/ {
   auth_basic off;
   fastcgi_read_timeout 1200;

   fastcgi_split_path_info ^(.+\.php)(/.+)$;
   fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

   # below for some setups on php 7.4
   #include snippets/fastcgi-php.conf;
   include /etc/nginx/fastcgi_params;
   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location @rewrites {
    rewrite ^(.*) /index.php?p=$1 last;
}

nginx graphql gatsby
1个回答
1
投票

解决方案 & 更新

location ^~ /api {
    auth_basic off;
    try_files $uri $uri/ /index.php?$args;
}

参考。

https:/stackoverflow.coma6218083212257950

在Gatsby网站上关闭GraphQL api的nginx认证。


我已经尝试在我的终端复制这个问题,但工作正常。

我使用OpenResty,并添加了一个标题 X-reachedmore_set_headers (你可以用 add_header/api/ 的位置块。

  1. 你可以尝试用同样的方法,看看请求是否真的到达了正确的位置块。

这是我的Nginx配置

location / {
    try_files $uri $uri/ @rewrites;
    auth_basic "restricted";
    auth_basic_user_file /usr/local/openresty/nginx/conf/conf.d/.htpasswd;
}

location ^~ /api/ {
    auth_basic off;
    try_files $uri $uri/ /index.php?$args;
    more_set_headers 'X-reached true';
}

location @rewrites {
    rewrite ^(.*) /index.php?p=$1 last;
}

你可以用下面的命令检查响应中的头。

 curl -XGET -IL https://site-name.tld/api/path

或者你可以使用浏览器中的网络选项卡检查。

2.检查是否有一些重定向发生。


0
投票

感谢Devarshi,这才是有效的方法。

location ^~ /api {
    auth_basic off;
    try_files $uri $uri/ /index.php?$args;
}
© www.soinside.com 2019 - 2024. All rights reserved.