The front end uses the nginx proxy to solve the cross-domain problem. How to configure the nginx of the back-end server?

the front-end project is proxied through nginx. It is always reported to 500 when requesting the backend server interface, but you can request the API directly using the domain name. It"s like going through two nginx proxies, and now I don"t know how to configure the nginx of the back-end server.

it seems that every request from the front end will be redirected to access the interface through ip, but the server can only access the interface through the domain name.
for example, if the address of the redirected interface in the frontend is 10.20.30.40 username login, it will report a 500 error and can be accessed normally through the domain name test.api.com/user/login.
I don"t know if my background server is not set up to access the interface directly through ip.

Nov.04,2021

centos install nginx see my blog: https://www.cnblogs.com/dshvv.

tell me more about how to configure it after installation:
sometimes we cross-domain with the help of browser support for Access-Control-Allow-Origin.
but some browsers do not support it, so this is not the best solution.
now let's use nginx to satisfy the browser's homology policy through reverse proxy.
it is possible to set the header information by simulating the client request through the server
take QQ Music as an example. QQ Music does not allow other sites to access this interface, so we have to simulate the header information (referer, host), but the browser forbids it. What are we going to do?
add some agent information to the configuration file: / usr/local/nginx/conf/nginx.conf

server {
           
        listen       8081;
        server_name  localhost;

        -sharpcharset koi8-r;

        access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location ^~ /proxy_qqmusic/ {
              add_header Access-Control-Allow-Origin *;
              add_header Access-Control-Allow-Headers Origin,X-Requested-Width,Content-Type,Accept;
              proxy_set_header referer "https://c.y.qq.com";
              proxy_set_header host "c.y.qq.com";
            proxy_pass https://c.y.qq.com/;
        }
...

if we visit again, we can get the data


    :http://www.dshvv.com:8081/proxy_qqmusic/
QQapi:splcloud/fcgi-bin/fcg_get_diss_tag_conf.fcg?g_tk=678987435&jsonpCallback=getPlaylistTags&loginUin=960423114&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0&callback=getPlaylistTags&_=1535526854117

clipboard.png


just call the request to the corresponding port, and the things in the backend are not visible relative to the frontend, so to put it simply, you can generally proxy directly to the ip+ port number (Aliyun can directly use the private network ip), of the security group. It is not necessary for you to go 80 by default without adding the port number.

Menu