Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

287
Visualizações
Nginx redirect to Node.js backend

I want to redirect a URL e.g domain.com/api/ to a specific Node.js server, the root URL shows my website. At the moment I use this config:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        server_name _;

        location / {

                try_files $uri $uri/ =404;
        }

        location /api {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://localhost:3031/;
            proxy_ssl_session_reuse off;
            proxy_set_header Host $http_host;
            proxy_cache_bypass $http_upgrade;
            proxy_redirect off;
}

but it does not work.

What has gone wrong?

Thanks for help and best regards :)

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You fogot closing bracket after location /api section. Your config working at my machine.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    server_name _;

    location / {

            try_files $uri $uri/ =404;
    }

    location /api {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:3031/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
     }
  }

Also your nodejs backend must handle '/api' requests.

over 4 years ago · Santiago Trujillo Relatório

0

Please note that proxy_pass directive gets argument for backend and optional URI.

That means proxy_pass http://localhost:3031; will get the URI from the user, e.g /api/res.json so final URL for the NODE JS is: http://localhost:3031/api/res.json

But when you provide the URI to the directive itself, it overrides the requested URI in the matching location. e.g location /api and proxy_pass http://localhost:3031/; (note the suffixed slash). so the part /api is replaced by / and final URL is: http://localhost:3031/res.json.

from the NGINX docs:

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:

location /name/ {
  proxy_pass http://127.0.0.1/remote/;
}

So it's important to understand that the part of the requested URI /name/ is replaced by /remote/ and then the rest of the requested URI is added to the final sent URI.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda