V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Hopetree
V2EX  ›  Django

使用了容器之后,第三方登陆失效了

  •  
  •   Hopetree ·
    Hopetree · 2019-07-09 18:40:30 +08:00 · 3140 次点击
    这是一个创建于 1743 天前的主题,其中的信息可能已经有所发展或是发生改变。

    描述一下场景: 我的服务是 django 写的,然后添加了第三方登陆(微博,GitHub ),之前登陆没问题,但是我把服务使用容器化部署之后( python 容器当服务-->nginx 跟本地映射)

    因为服务的容器名称是 web,所以 nginx 上面的配置是

    location / {
            proxy_pass http://web:8000;
        }
    
    

    其他页面都没啥问题,但是第三方登陆的跳转发现了问题,在登陆的时候选择微博的时候,我发现回调地址不是我自己的域名,而是容器的那个 http://web

    具体可以看一下效果: https://tendcode.com/accounts/login/?next=/ 点击微博登陆,会发现

    https://api.weibo.com/oauth2/authorize?client_id=1858902686&redirect_uri=http%3A%2F%2Fweb%3A8000%2Faccounts%2Fweibo%2Flogin%2Fcallback%2F&scope=&response_type=code&state=gtjDNiAYOO7Y
    

    redirect_uri变成了 http://web 而不是我自己的域名

    我不知道这个问题到底是 nginx 可以解决还是要去看 django-allauth

    3 条回复    2019-07-09 19:32:36 +08:00
    Hopetree
        1
    Hopetree  
    OP
       2019-07-09 19:15:40 +08:00
    自己解决了,查看了一个 flask 容器化项目的 nginx,发现多了几个设置,我试着添加了,发现 OK 了,应该是那个 host 可以解决,

    ```
    location / {
    proxy_pass http://web:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    }
    ```
    siteshen
        2
    siteshen  
       2019-07-09 19:17:59 +08:00   ❤️ 1
    nginx 通常是这么配置的(其中 proxy_params 里会设置主机名等 HTTP 头):

    location / {
    include proxy_params;
    proxy_pass http://web:8000;
    }

    # file proxy_params
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;


    改成这样应该就可以了。
    a663
        3
    a663  
       2019-07-09 19:32:36 +08:00 via Android
    跨域了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1285 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:47 · PVG 01:47 · LAX 10:47 · JFK 13:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.