How to send requests from nginx & react container to a spring boot container?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I have two docker containers, which have the separated front-end and back-end of my application.



In the first container I have already built reactjs code and a nginx web server.
Here is the Dockerfile,



FROM nginx:1.15.2-alpine
COPY ./build /var/www
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
ENTRYPOINT ["nginx","-g","daemon off;"]


This is my nginx.conf file,



worker_processes 1;
events {
worker_connections 1024;
}

http {
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;

upstream server {
server 172.20.58.236:8080;
}

upstream client {
server 172.19.59.36;
}


server {
listen 80;

root /var/www;
index index.html index.htm;

add_header 'Access-Control-Allow-Origin' 'http://172.19.59.36';
add_header 'Access-Control-Allow_Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';


location /api {
proxy_pass http://server;
}

location ~* .(?:manifest|appcache|html?|xml|json)$ {
expires -1;
}

location / {
try_files $uri $uri/ /index.html;
}

location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}

location ~* .(?:css|js)$ {
try_files $uri =404;
expires 1y;
access_log off;
add_header Cache-Control "public";
}

location ~ ^.+..+$ {
try_files $uri =404;
}

location /static/ {
root /var/www;
}
}
}


I referred this & this for the configuration file.



My Dockerfile for the back-end,



FROM openjdk:8-jdk-alpine
ADD target/dependency-graph-service.jar dependency-graph-service.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "./dependency-graph-service.jar"]


Here is a sample route in my Spring Boot back-end,



@CrossOrigin(origins = "*")
@RequestMapping(
value = "/api/greet",
method = RequestMethod.GET
)
public String getHealthCheck(){
String greet = "Get Works!!";
return greet;
}


A typical request from the react front-end would look like,



sendRequest = () => {
axios.get(`http://127.0.0.1/api/greet`)
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
})
};


As mentioned ip addresses for my resources are as follows,
client - 172.19.59.36
server - 172.20.58.236



From browser I can talk to the back-end server through client app.
This route works,



http://172.19.59.36/api/greet


A screenshot of the browser



And as expected I can get inside of the container and wget the same url and it works.



A screenshot of the terminal




The problem is when the same request is sent from the react
application or the compiled javascript chunk, I get a CORS error, as
it is generated inside the container I guess. This request does not hit the server.




Screenshot of the error



In the index.html by the script tag I have added a library and browser does not download it as well.



Library in script
404 on the library URL



Please guide me to correct this issue, I tried some nginx configs to resolve the CORS block but nothing worked.



When running the containers port mappings are as follows,



client(172.19.59.36) - 80:80
server(172.20.58.236) - 8080:8080


I referred this as well.



Thanks in advance.










share|improve this question































    2















    I have two docker containers, which have the separated front-end and back-end of my application.



    In the first container I have already built reactjs code and a nginx web server.
    Here is the Dockerfile,



    FROM nginx:1.15.2-alpine
    COPY ./build /var/www
    COPY nginx.conf /etc/nginx/nginx.conf
    EXPOSE 80
    ENTRYPOINT ["nginx","-g","daemon off;"]


    This is my nginx.conf file,



    worker_processes 1;
    events {
    worker_connections 1024;
    }

    http {
    server_tokens off;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    upstream server {
    server 172.20.58.236:8080;
    }

    upstream client {
    server 172.19.59.36;
    }


    server {
    listen 80;

    root /var/www;
    index index.html index.htm;

    add_header 'Access-Control-Allow-Origin' 'http://172.19.59.36';
    add_header 'Access-Control-Allow_Credentials' 'true';
    add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
    add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';


    location /api {
    proxy_pass http://server;
    }

    location ~* .(?:manifest|appcache|html?|xml|json)$ {
    expires -1;
    }

    location / {
    try_files $uri $uri/ /index.html;
    }

    location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
    expires 1M;
    access_log off;
    add_header Cache-Control "public";
    }

    location ~* .(?:css|js)$ {
    try_files $uri =404;
    expires 1y;
    access_log off;
    add_header Cache-Control "public";
    }

    location ~ ^.+..+$ {
    try_files $uri =404;
    }

    location /static/ {
    root /var/www;
    }
    }
    }


    I referred this & this for the configuration file.



    My Dockerfile for the back-end,



    FROM openjdk:8-jdk-alpine
    ADD target/dependency-graph-service.jar dependency-graph-service.jar
    EXPOSE 8080
    ENTRYPOINT ["java", "-jar", "./dependency-graph-service.jar"]


    Here is a sample route in my Spring Boot back-end,



    @CrossOrigin(origins = "*")
    @RequestMapping(
    value = "/api/greet",
    method = RequestMethod.GET
    )
    public String getHealthCheck(){
    String greet = "Get Works!!";
    return greet;
    }


    A typical request from the react front-end would look like,



    sendRequest = () => {
    axios.get(`http://127.0.0.1/api/greet`)
    .then(res => {
    console.log(res)
    })
    .catch(err => {
    console.log(err)
    })
    };


    As mentioned ip addresses for my resources are as follows,
    client - 172.19.59.36
    server - 172.20.58.236



    From browser I can talk to the back-end server through client app.
    This route works,



    http://172.19.59.36/api/greet


    A screenshot of the browser



    And as expected I can get inside of the container and wget the same url and it works.



    A screenshot of the terminal




    The problem is when the same request is sent from the react
    application or the compiled javascript chunk, I get a CORS error, as
    it is generated inside the container I guess. This request does not hit the server.




    Screenshot of the error



    In the index.html by the script tag I have added a library and browser does not download it as well.



    Library in script
    404 on the library URL



    Please guide me to correct this issue, I tried some nginx configs to resolve the CORS block but nothing worked.



    When running the containers port mappings are as follows,



    client(172.19.59.36) - 80:80
    server(172.20.58.236) - 8080:8080


    I referred this as well.



    Thanks in advance.










    share|improve this question



























      2












      2








      2


      0






      I have two docker containers, which have the separated front-end and back-end of my application.



      In the first container I have already built reactjs code and a nginx web server.
      Here is the Dockerfile,



      FROM nginx:1.15.2-alpine
      COPY ./build /var/www
      COPY nginx.conf /etc/nginx/nginx.conf
      EXPOSE 80
      ENTRYPOINT ["nginx","-g","daemon off;"]


      This is my nginx.conf file,



      worker_processes 1;
      events {
      worker_connections 1024;
      }

      http {
      server_tokens off;
      include /etc/nginx/mime.types;
      default_type application/octet-stream;

      upstream server {
      server 172.20.58.236:8080;
      }

      upstream client {
      server 172.19.59.36;
      }


      server {
      listen 80;

      root /var/www;
      index index.html index.htm;

      add_header 'Access-Control-Allow-Origin' 'http://172.19.59.36';
      add_header 'Access-Control-Allow_Credentials' 'true';
      add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
      add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';


      location /api {
      proxy_pass http://server;
      }

      location ~* .(?:manifest|appcache|html?|xml|json)$ {
      expires -1;
      }

      location / {
      try_files $uri $uri/ /index.html;
      }

      location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
      expires 1M;
      access_log off;
      add_header Cache-Control "public";
      }

      location ~* .(?:css|js)$ {
      try_files $uri =404;
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
      }

      location ~ ^.+..+$ {
      try_files $uri =404;
      }

      location /static/ {
      root /var/www;
      }
      }
      }


      I referred this & this for the configuration file.



      My Dockerfile for the back-end,



      FROM openjdk:8-jdk-alpine
      ADD target/dependency-graph-service.jar dependency-graph-service.jar
      EXPOSE 8080
      ENTRYPOINT ["java", "-jar", "./dependency-graph-service.jar"]


      Here is a sample route in my Spring Boot back-end,



      @CrossOrigin(origins = "*")
      @RequestMapping(
      value = "/api/greet",
      method = RequestMethod.GET
      )
      public String getHealthCheck(){
      String greet = "Get Works!!";
      return greet;
      }


      A typical request from the react front-end would look like,



      sendRequest = () => {
      axios.get(`http://127.0.0.1/api/greet`)
      .then(res => {
      console.log(res)
      })
      .catch(err => {
      console.log(err)
      })
      };


      As mentioned ip addresses for my resources are as follows,
      client - 172.19.59.36
      server - 172.20.58.236



      From browser I can talk to the back-end server through client app.
      This route works,



      http://172.19.59.36/api/greet


      A screenshot of the browser



      And as expected I can get inside of the container and wget the same url and it works.



      A screenshot of the terminal




      The problem is when the same request is sent from the react
      application or the compiled javascript chunk, I get a CORS error, as
      it is generated inside the container I guess. This request does not hit the server.




      Screenshot of the error



      In the index.html by the script tag I have added a library and browser does not download it as well.



      Library in script
      404 on the library URL



      Please guide me to correct this issue, I tried some nginx configs to resolve the CORS block but nothing worked.



      When running the containers port mappings are as follows,



      client(172.19.59.36) - 80:80
      server(172.20.58.236) - 8080:8080


      I referred this as well.



      Thanks in advance.










      share|improve this question
















      I have two docker containers, which have the separated front-end and back-end of my application.



      In the first container I have already built reactjs code and a nginx web server.
      Here is the Dockerfile,



      FROM nginx:1.15.2-alpine
      COPY ./build /var/www
      COPY nginx.conf /etc/nginx/nginx.conf
      EXPOSE 80
      ENTRYPOINT ["nginx","-g","daemon off;"]


      This is my nginx.conf file,



      worker_processes 1;
      events {
      worker_connections 1024;
      }

      http {
      server_tokens off;
      include /etc/nginx/mime.types;
      default_type application/octet-stream;

      upstream server {
      server 172.20.58.236:8080;
      }

      upstream client {
      server 172.19.59.36;
      }


      server {
      listen 80;

      root /var/www;
      index index.html index.htm;

      add_header 'Access-Control-Allow-Origin' 'http://172.19.59.36';
      add_header 'Access-Control-Allow_Credentials' 'true';
      add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
      add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';


      location /api {
      proxy_pass http://server;
      }

      location ~* .(?:manifest|appcache|html?|xml|json)$ {
      expires -1;
      }

      location / {
      try_files $uri $uri/ /index.html;
      }

      location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
      expires 1M;
      access_log off;
      add_header Cache-Control "public";
      }

      location ~* .(?:css|js)$ {
      try_files $uri =404;
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
      }

      location ~ ^.+..+$ {
      try_files $uri =404;
      }

      location /static/ {
      root /var/www;
      }
      }
      }


      I referred this & this for the configuration file.



      My Dockerfile for the back-end,



      FROM openjdk:8-jdk-alpine
      ADD target/dependency-graph-service.jar dependency-graph-service.jar
      EXPOSE 8080
      ENTRYPOINT ["java", "-jar", "./dependency-graph-service.jar"]


      Here is a sample route in my Spring Boot back-end,



      @CrossOrigin(origins = "*")
      @RequestMapping(
      value = "/api/greet",
      method = RequestMethod.GET
      )
      public String getHealthCheck(){
      String greet = "Get Works!!";
      return greet;
      }


      A typical request from the react front-end would look like,



      sendRequest = () => {
      axios.get(`http://127.0.0.1/api/greet`)
      .then(res => {
      console.log(res)
      })
      .catch(err => {
      console.log(err)
      })
      };


      As mentioned ip addresses for my resources are as follows,
      client - 172.19.59.36
      server - 172.20.58.236



      From browser I can talk to the back-end server through client app.
      This route works,



      http://172.19.59.36/api/greet


      A screenshot of the browser



      And as expected I can get inside of the container and wget the same url and it works.



      A screenshot of the terminal




      The problem is when the same request is sent from the react
      application or the compiled javascript chunk, I get a CORS error, as
      it is generated inside the container I guess. This request does not hit the server.




      Screenshot of the error



      In the index.html by the script tag I have added a library and browser does not download it as well.



      Library in script
      404 on the library URL



      Please guide me to correct this issue, I tried some nginx configs to resolve the CORS block but nothing worked.



      When running the containers port mappings are as follows,



      client(172.19.59.36) - 80:80
      server(172.20.58.236) - 8080:8080


      I referred this as well.



      Thanks in advance.







      reactjs spring-boot docker nginx cors






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 10:29







      Thisun Pathirage

















      asked Jan 3 at 8:59









      Thisun PathirageThisun Pathirage

      142




      142
























          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019062%2fhow-to-send-requests-from-nginx-react-container-to-a-spring-boot-container%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019062%2fhow-to-send-requests-from-nginx-react-container-to-a-spring-boot-container%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith