Report this

What is the reason for this report?

Issue in proxy pass from nginx to angular application

Posted on May 7, 2021

I have created an angular application that displays a page with “Hello World” on ‘/hello’ route. url for angular: http://localhost:4200/hello When I make request from browser to nginx on ‘/greet’: http://localhost/greet that should be redirected to /hello route of angular application so that the page with “Hello World” is displayed. But if the request is http://localhost/hello , an err should appear

The nginx conf:

server {
listen 80;
server_name _;
location /greet {
proxy_pass http://localhost:4200/hello;
}
location /hello {
return 404;
}
}

The issue is that while redirecting, the route is not getting modified as ‘/hello’ And since there is no ‘/greet’ route in angular, we get a not found.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.

The issue seems to happen because when you’re trying to proxy pass to route ‘/hello’, there’s a ‘return 404;’ defined for ‘/hello’ location in your Nginx configuration.

A way to solve your problem can be add a trailing slash after ‘/hello’ inside ‘proxy_pass’ like this:

  1. location /greet {
  2. proxy_pass http://localhost:4200/hello/;
  3. }

After this, you need to restart your nginx server. Some routes can be sensitive to trailing slashes and adding a slash can make a difference.

Also, it’s worth noting that with your current configuration, any visit to ‘http://localhost/hello’ directly will return a 404 as per your explicit configuration:

  1. location /hello {
  2. return 404;
  3. }

If you want to change this behavior, adjust the /hello location block accordingly.

Please check this link for more useful information regarding Nginx proxying.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.