Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

node.js - Frontend to backend requests (react, nodejs) do not go through nginx. How can I make nginx intercept the traffic?

I have a nodejs server listening on port 2000 and a react development server listening on port 4000.

In my config file for nginx, I realized that I can never create location blocks for a location beginning with "/api/" (basically all my requests) because network requests from the frontend to the backend aren't accepted through nginx (I thought they were).

I'm not sure how to make nginx intercept requests. I was fooled because the loadbalancing (for the frontend only as I now see) seemed to work.

Accesses to any page would be split among the servers, BUT the frontend to backend requests are not going through nginx for reasons I do not understand.

In the configuration file for nginx (not nginx.conf) below, the only location block that "works" is the first (location /).

In other words, it's as if the blocks beginning with /api/ do not exist because nginx does not seem to be handling backend requests at any point.

I want to have backend requests go through nginx, so I can create location blocks from /api/ and implement things like rate limiting.

Please, I am very lost and have spent countless hours trying to figure this out. I could really use some guidance and answers!

limit_req_zone $binary_remote_addr zone=zonenot:15m rate=1r/m;


log_format upstreamlog '$server_name to: $upstream_addr [$request] '
    'upstream_response_time $upstream_response_time '
    'msec $msec request_time $request_time'
    '$status $body_bytes_sent "$http_referer"'
    '"$http_user_agent" "$http_x_forwarded_for"';
    

upstream lbfrontend {
    least_conn;
    # frontendservers

    server xxx.xxx.xxx:4000;
    server kkk.kkk.kkk:4000;

    
}

upstream lbbackend {
    least_conn;

    # backendservers
    server xxx.xxx.xxx:2000;
    server kkk.kkk.kkk:2000;

}

    
    
    server {

        listen 80;
        server_name xxx.xxx.xxx;

        access_log /var/log/nginx/access.log upstreamlog;

        location / {
            proxy_pass http://lbfrontend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Host $host;
        }

        location /api/notification/getnotifications {

            limit_req zone=zonenot;

            proxy_pass http://lbbackend;

            

        }
        location ^~ /api/ {

            proxy_pass http://lbbackend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Host $host;
            
        }



        
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...