Magento2 payment gateway is getting bad gateway error

Whenever I am placing order with NAB or PAYPAL express in magento site,
It gets 504 Gateway time-out error. Then site cannot be accessed in any browser window.

This happens only when I am using site with cloudlflare domain. I have created replica of the server and It works fine with IP address.