Quantcast
Channel: ADACHIN SERVER LABO
Viewing all articles
Browse latest Browse all 164

[Ruby][Rails][Sidekiq][Nginx][Docker][403 forbidden]WARN — : attack prevented by Rack::Protection::IPSpoofing

$
0
0

これはハマった。😵

既存で動いているNginx + Ruby + Rails + Sidekiq開発環境(Docker)を再構築していると、以下のように出落ち並に403が返ってくる。
ちなみにSidekiq非同期処理を行いたい時に使うライブラリで、複数のジョブを同時に実行するとメモリーを節約すること可能

今回やっとのことで原因解明したのでブログします!


■Cannot render console from 10.100.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

# cd log
# ls
development.log  sidekiq.log  unicorn.log

# tail -f *
==> development.log <==
Started GET "/sidekiq" for 127.0.0.1 at 2018-09-21 04:18:57 +0000
Cannot render console from 10.100.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

==> unicorn.log <==
W, [2018-09-21T04:18:57.482253 #54151]  WARN -- : attack prevented by Rack::Protection::IPSpoofing

上記のようにエラーログから見ていきましょう。2つあります。
まずはRails/Dockerのエラーなので、下記のように追加すればエラーはなくなります。(これは楽ちん😐)

$ cd config/environments/
$ vim development.rb
  config.web_console.whitelisted_ips = '0.0.0.0/0' #追加


■WARN — : attack prevented by Rack::Protection::IPSpoofing

https://github.com/mperham/sidekiq/issues/3671

これや!!🤗
え、Nginxの設定漏れか!?

・nginx.conf

~省略~
    upstream unicornapp {
      server unix:/tmp/unicorn.sock;
    }
~省略~

・conf.d/adachin.jp.conf

server {
    listen 80;
    server_name adachin.jp;

    # force https
    if ($http_x_forwarded_proto != https) {
        return 301 https://$server_name$request_uri;
    }

    # ELB SSL Termination
    set $elb_https off;
    if ($http_x_forwarded_proto = https) {
        set $elb_https on;
    }

    root /var/www/adachin;
    location / {
        proxy_pass http://unicornapp;
        proxy_set_header    Host                $http_host;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_set_header    X-Forwarded-Ssl     on;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto   https;
        proxy_set_header    X-Frame-Options     SAMEORIGIN;
        proxy_set_header    X-Sendfile-Type     X-Accel-Redirect;

    }

    access_log /var/log/nginx/access.log main;
    error_log  /var/log/nginx/error.log warn;

ドメインはadachin.jpでござんす。
ハマったのはNginxのプロキシの部分。X-Forwarded-Forの設定漏れだった。。🤮
/sidekiqにアクセスできるようにするにはroutes.rbに以下を追記すれば閲覧OK!!

# cat routes.rb
Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  # sidekiq
  require 'sidekiq/web'
  mount Sidekiq::Web => '/sidekiq'
end

・restart

https://github.com/RVIRUS0817/shellscripts/tree/master/init.d

起動スクリプト系はギッハブに置いてるのでぜひ。

# /etc/init.d/unicorn restart
# /etc/init.d/sidekiq restart
# /etc/init.d/nginx restart

😎

https://adachin.jp/sidekiq


■まとめ

しかしコード自体がダメ!!!と疑うのはアカン。


Viewing all articles
Browse latest Browse all 164

Trending Articles