0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Devise】本番環境でログインできないときの解決方法

Last updated at Posted at 2021-09-11

#はじめに
deviseを使用したログインを本番環境で行ったが、再度ログイン画面へリダイレクトされてしまった。
結論、nginxの設定がうまくいっていなかったため、以下のように解決方法をまとめた。

#環境
rails 6.1.4
ruby 2.7.3
nginx 1.20.1
puma 4.3.8
devise 4.8.0

#解決方法
①nginx設定ファイルに「proxy_set_header X-Forwarded-Proto $scheme;」を追加

#/etc/nginx/conf.d/hoge.com.conf

upstream puma-production {
    server unix:/var/www/html/<app名>/shared/tmp/sockets/puma.sock;
}

server {
    listen       80;
    server_name  <ドメイン>;
    return 301 https://$host$request_uri;
}

server {
    listen  443 ssl;
    server_name     <ドメイン>;
    ssl_certificate       /etc/letsencrypt/live/<ドメイン>/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/<ドメイン>/privkey.pem;
    root   /var/www/html/<app名>/current/public;
    location / {
        try_files $uri @app;
    }
    location @app {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;  <---追記
        proxy_pass http://puma-production;
    }
}

②nginxを再起動

$ sudo systemctl restart nginx

#まとめ
リバースプロキシを使用していたため、nginxの設定に追記が必要だった。

#参考
https://stackoverflow.com/questions/30733375/devise-throws-401-unauthorized-and-redirects-only-in-production-staging

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?