私は過去 3 日間のかなりの部分を、インターネット上にあるすべての解決策を試し、絶望的な気持ちで過ごしてきました。問題文は次のとおりです。
次の 3 つのサービスを含む Docker 化されたアプリがあります。
- gunicorn を使用した django アプリケーション (
web) - Nginx サーバー (
nginx) - PostgreSQL (
db)
私の Web アプリケーションでは、かなり標準的な OAuth プロセスを使用して、ユーザーが GitHub アカウントでログインする必要があります。これは常になくても機能しnginxます。ユーザーは「github でログイン」ボタンをクリックし、GitHub に送信してアプリケーションを承認し、完了ページにリダイレクトします。
「認証コールバック URL」をhttp://localhost:8000として入力しました。Nginx がなくても、localhost のアプリに移動し、ボタンをクリックすると、認証時に localhost のアプリにリダイレクトされます。
Nginx では、常にエラーで失敗します (コンソールの nginx):
GET /auth/complete/github/?error=redirect_uri_mismatch&error_description=The+redirect_uri+MUST+match+the+registered+callback+URL+for+this+application.&error_uri=https%3A%2F%2Fdeveloper.github.com%2Fapps %2Fmanaging-oauth-apps%2Ftroubleshooting-authorization-request-errors%2F%23redirect-uri-mismatch&state=nmegaLb41b959su31nRU4ugFOzAqE8Cbl HTTP/1.1
これは私のNginx構成です:
upstream webserver {
# docker will automatically resolve this to the correct address
# because we use the same name as the service: "web"
server web:8000;
}
# now we declare our main server
server {
listen 80;
server_name localhost;
location / {
# everything is passed to Gunicorn
proxy_pass http://webserver;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
}
location /static {
autoindex off;
alias /static_files/;
}
location /media {
alias /opt/services/starport/media;
}
}
これは私の Dockerfile です:
version: '3.7'
services:
web:
build: .
command: sh -c "cd starport && gunicorn starport.wsgi:application --bind 0.0.0.0:8000"
volumes:
- static_files:/static_files # <-- bind the static volume
networks:
- nginx_network
nginx:
image: nginx
ports:
- 8000:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- static_files:/static_files # <-- bind the static volume
depends_on:
- web
networks:
- nginx_network
networks:
nginx_network:
driver: bridge
volumes:
static_files:
Nginxがポートをリッスンしてから別のポートに転送するため、Nginxなしでは機能したがNginx httpサーバーでは機能しなかった理由は、リダイレクトに関係しているというのが私の推測です。GitHub のドキュメントでは、リダイレクト URI は登録済みのコールバック URL とまったく同じである必要があると具体的に述べられています。インスペクター ツールも使用しました。これらは私のリクエスト ヘッダーです。
GET /accounts/login/ HTTP/1.1
Cookie: ...
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Upgrade-Insecure-Requests: 1
Host: localhost:8000
User-Agent: Mozilla/5.0
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: keep-alive
Nginxで表示されるエラーメッセージは次のとおりです(10回のうち10回、nginxなしでエラーなしで魅力的に機能することを強調します):
追加の詳細として、私はsocial-auth-app-djangoパッケージを使用していますが、これは問題ではありません。
さらなるトラブルシューティング
数え切れないほどの時間を調査した後、ローカル オン デバッグ モードでこれを実行し、今度はリクエスト情報を綿密に監視しました。OAuth を介して GitHub で承認するためのリンクに使用すると、これがすべてのヘッダー情報を含むリクエストになります。
CSRF_COOKIE 'abc'
HTTP_ACCEPT 'text/html,application/xhtml+xml'
HTTP_ACCEPT_ENCODING 'gzip, deflate'
HTTP_ACCEPT_LANGUAGE 'en-us'
HTTP_CONNECTION 'close'
HTTP_COOKIE ('csrftoken=...; ')
HTTP_HOST 'localhost'
HTTP_REFERER 'http://localhost:8000/accounts/login/?next=/'
HTTP_USER_AGENT ('Mozilla/5.0')
HTTP_X_FORWARDED_FOR '172.26.0.1'
HTTP_X_FORWARDED_PROTO 'http'
PATH_INFO '/auth/complete/github/'
SERVER_NAME '0.0.0.0'
SERVER_PORT '8000'
QUERY_STRING
'error=redirect_uri_mismatch&error_description=The+redirect_uri+MUST+match+the+registered+callback+URL+for+this+application.&error_uri=https%3A%2F%2Fdeveloper.github.com%2Fapps%2Fmanaging-oauth-apps%2Ftroubleshooting-authorization-request-errors%2F%23redirect-uri-mismatch&state=NwUhVqfOCNb51zpvoFbXVvm1Cr7k3Fda'
RAW_URI
'/auth/complete/github/?error=redirect_uri_mismatch&error_description=The+redirect_uri+MUST+match+the+registered+callback+URL+for+this+application.&error_uri=https%3A%2F%2Fdeveloper.github.com%2Fapps%2Fmanaging-oauth-apps%2Ftroubleshooting-authorization-request-errors%2F%23redirect-uri-mismatch&state=NwUhVqfOCNb51zpvoFbXVvm1Cr7k3Fda'
すぐに目立ったのは、HTTP_HOST、HTTP_REFERRER、およびSERVER_NAMEの値です。私にとって興味深いのは、次のエラーメッセージです。
の代わりにhttp://localhost:8000しかありhttp://localhostません。これは、物事を正しく構成していないという大きなヒントのように見えます。どんな手がかりや支援も役に立ちます!
試したリソース
このようなStackOverflow スレッドは有望に思えますが、このような同様の質問は、エラーを説明する以外に意味のある応答を受け取りません。
