简体   繁体   中英

Redirect in nginx form port 80 to port 3000 but keeping the url the same

I have a NodeJS backend running on a server on port 3000 the requests are arriving on port 80 now in /etc/nginx/sites-available/node I have the following config

server {

listen 80 default_server;


server_name name.example;


return 301 http://195.x.x.x:3000;

}

when a request reach the server it redirects to port 3000 but when a request like http://195.xxx/api/test/test it also redirects to http://195.xxx:3000 how can I redirect to port 3000 and keep the same url like http://195.xxx:3000/api/test/test

Why are you returning a 301 code, if you just want to forward requests and return them through your server you would be better using proxy_pass why are you redirecting them?

You can setup a reverse proxy using NGINX like this:

server {
    listen 80;
    server_name name.example;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://195.x.x.x:3000;
    }
}

However if you truly do want to redirect them, you should change return 301 http://195.xxx:3000; to return 301 http://195.xxx:3000$request_uri;

Found the answer right after posting the question it was $request_uri I should have but

server {

listen 80 default_server;


server_name name.example;


return 301 http://195.x.x.x:3000$request_uri;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM