在Gatsby网站的GraphQL api的nginx中关闭用户身份验证

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

我正在尝试在我的Gatsby网站上设置用户身份验证,但允许使用GraphQL api。

这是我现在拥有的:

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

    location "^~ /api/.*" {
       auth_basic "off";
    }
} 

编辑,基于以下评论

我试图添加〜.php $中的内容以及重写中的内容,但出现相同的错误。

location / {
    try_files $uri $uri/ @rewrites;

    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location ^~ /api/ {
        rewrite ^(.*) /index.php?p=$1 last;
        auth_basic off;
    }
}

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

# PHP
location ~ \.php$ {
     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;
}
nginx graphql gatsby
1个回答
0
投票

一个错误是引用了off

只需删除引号:将"off"更改为off。然后它将是实际的off配置,而不是带引号的字符串。

另一个是^~的非正则表达式位置(/api/)。最好将location ^~解释为具有最高优先级的前缀位置。它不是位置的正则表达式类型。

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;
}

# PHP
location ~ \.php$ {
     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;
} 
© www.soinside.com 2019 - 2024. All rights reserved.