Featured Image Banner

Axios behind firewall NOPROXY setting

This is a problem during local development where a firewall is blocking or routing traffic incorrectly through a proxy for localhost.

This code is fine for local development but would need to be removed, or conditionally disabled in a production build.

Before you do an axios call add this line of code to override any local proxy setting;

export const getServerSideProps = async () => {
 // override env variable if local development
   if (process.env.ENV === "local") {
    process.env.no_proxy = process.env.LOCALPROXY;
   }

   const res = await axios.get("http://127.0.0.1:9999/api/posts");
   return {
    props: {
     users: res.data,
    },
   };
}

Create a .env.local file with similar values;

ENV='local'
LOCALPROXY=127.0.0.1
Make sure the .local.env file does not go into a production environment.
Posted: 20/12/2021 Author: Jake Jenkins
Jake Wales Logo
Built by Jake Jenkins with love in Wales.