xqdoo00o / chatgpt-web

Pure Javascript ChatGPT demo based on OpenAI API
https://xqdoo00o.github.io/chatgpt-web/
MIT License
983 stars 369 forks source link

basic_auth一开启就报错 #48

Closed eureka6 closed 1 year ago

eureka6 commented 1 year ago

大佬,配置了下面的东西后,Nginx的basic_auth一开启就报错

map $http_authorization $gptauth {
    default  $http_authorization;
    ""       "Bearer sb-your-token"; #这里改了
}
server {
    listen       80;
    server_name  example.com;
    gzip  on;
    gzip_min_length 1k;
    gzip_types text/event-stream;

    location ^~ /v1 {
        proxy_pass https://api.openai-sb.com/v1;   #这里改了
        proxy_set_header Host api.openai-sb.com;   #这里改了
        proxy_ssl_name api.openai-sb.com;   #这里改了
        proxy_ssl_server_name on;
        proxy_set_header Authorization $gptauth;
        proxy_pass_header Authorization;
        proxy_buffering off;
    }

    location / {
        alias /usr/share/nginx/html/;
        index index.html;
    }
}

开启basic_auth后,网页上提示API密钥错误或失效,请检查API密钥! 关掉basic_auth就又正常了

另外,我用的是别人代理的api,需要把nginx配置里的代理地址、请求头之类从api.openai.com改成api.openai-sb.com,还要把sk-开头的密钥改成了sb-开头的密钥。这没影响吧?

主要是关闭basic_auth就一切都正常。

xqdoo00o commented 1 year ago

你的basic_auth配置 放到单html配置里看能生效吗 location / {这里, 然后打开网页,输入账号密码能正常进去吗

eureka6 commented 1 year ago

你的basic_auth配置 放到单html配置里看能生效吗 location / {这里, 然后打开网页,输入账号密码能正常进去吗

这样的话,可以正常使用。

如果把basic_auth放到最外边,或者location ^~ /v1 {下面,就会报错:API密钥错误或失效,请检查API密钥!

xqdoo00o commented 1 year ago

放到location ^~ /v1 { 里就不能用了吗

eureka6 commented 1 year ago

是的

xqdoo00o commented 1 year ago

是的

我知道了。basic_auth也占用Authorization头,如果你不在前端设置api密钥使用的话,proxy_set_header Authorization 这后面改回之前的那个默认密钥。map可以不要 并且把代码里的credentials: "omit"都删掉

xqdoo00o commented 1 year ago

可以参考最新文档加密HTML使用