F
F
felsme2020-04-23 03:19:12
Nginx
felsme, 2020-04-23 03:19:12

React app and api server on the same hosting?

Hello, during the development of react, the application was running on localhost:3000 and the api server was running on localhost:1820
and it seemed like they saw each other and everything worked, but if you upload it to the hosting and bind the domain, then react starts referring to its site address. ru/api/message without finding anything, and the server also lives nearby and is only available at the ip address from vps:1820/api/message, but if the react is forced to specify the desired ip with the port, then it refuses to go to this request with an error CORS
so far comes from solutions only to make some kind of redirect request in nginx to the api server

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Sviridov, 2020-04-23
@felsme

In the Nginx host settings, configure something like this:

location ~ ^/api {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass      http://127.0.0.1:1820;
    }

    location / {
        autoindex on;
        root /var/www/dist;
    }

A
Andrey Gavrilov, 2020-04-23
@thexaver

Use nginx

D
Dmitry, 2020-04-23
@crystalbit

as already written, you can do it through nginx reverse proxy - this is the best option
second option (worse, but without a reverse proxy) - send a cors header in the response on the server side, for example Access-Control-Allow-Origin: front domain

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question