nginx reverse proxy doesn't load application resources - nginx

I have 2 web services running on my machine (inside docker containers, but I think it doesn't matter), they are reachable by following addresses:
my.ip:1111
my.ip:2222
I launched nginx server on port 80 and I would like to configure reverse proxy to access my services by following aliases:
my.ip/app1
my.ip/app2
So, I added these entries to my /etc/nginx/conf.d/default.conf:
location /app1/ {
proxy_pass http://my.ip:1111/;
}
location /app2/ {
proxy_pass http://my.ip:2222/;
}
Proxy redirects corectly, because the main pages of web services are visible, so I know that the correct service tries to launch, but all .css/.js/etc resources are unavailable. Services get 404 on all resources, because they try to download them from "/" path, instead of "/app1/" (or :1111/)
Example from devtools console:
GET http://my.ip/app.js net::ERR_ABORTED 404 (Not Found)
(this resource is available on http://my.ip/app1/app.js or http://my.ip:1111/app.js)
I have read a lot of answers about this topic here, but none solution works for me. Any help will be appreciated, thanks!

Related

Basic NGINX proxy_pass example is serving 404's

I have an very basic out-of-the-box NGINX server running on a Windows machine at http://10.0.15.19:80. I'm attempting to use it as a proxy to get around a CORS restriction. I have the following nginx.conf, which from every example I've seen sounds like it should redirect any traffic from http://10.0.15.19 to http://10.0.1.2:3000.
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name 10.0.15.19;
location / {
proxy_pass http://10.0.1.2:3000/;
}
}
}
Instead of serving the content from http://10.0.1.2:3000 I get the default index.html page inside the included html folder. Similarly, if I were to go to http://10.0.15.19/any/path I get a 404 even though http://10.0.1.2:3000/any/path works correctly.
EDIT: I've noticed that even after commenting out the entire server block of my configuration, it's still serving content from the included html folder. This makes me think there is another server configuration running that I'm not aware of, but I'm not sure where. I downloaded NGINX from here, and I assume all configuration files exist within this folder.
It turns out this was because simply closing the window that pops up when you open nginx.exe doesn't actually kill the process. And in Windows you can apparently have multiple services bound to the same port, so I had many servers serving on port 80. Killing all of these processes and relaunching with the originally posted config fixed my problem.

nginx ignores Port in redirect and logs 499

I want to setup an nginx proxy for a legacy systems api. My barebone Setup for the redirect does work for redirects without ports:
# test redirecting to html
location /api {
proxy_pass https://my.host.de/main.html;
}
But some test systems do have Ports inside their HTTPS location e.g. https://my.host.de:8441/.../
# test redirecting to html
location /api {
proxy_pass https://my.host.de:8441/main.html;
}
So if I add a rule like this the nginx will log a 499 error and the Requesting Browser will endlessly loop, and the Target System wont show any logs (so no request was made).
I tried using proxy_redirect http://my.host.de:8441/ http://my.host.de/ (and the other way arouund) which didnt show any improvments. My guess is that the Port is not used? Since there is no request recorded on the target system.
Any Ideas what the problem could be?

Using namerd for DNS? (Or: how to configure an nginx ingress service with Linkerd)

I currently have a hello world service deployed on /svc/hello, and I've added a dentry to my namerd internal dtab as /svc/app => /svc/hello.
I've also deployed an nginx service that will serve as my ingress controller, and forward all traffic to the relevant services. Eventually it's going to do header stripping, exposing admin services only to developers in whitelisted ip ranges etc, but for now I've kept it really simple with the following config:
server {
location / {
proxy_pass http://app;
}
}
However, those nginx pods fail to start, with the error
nginx: [emerg] host not found in upstream "app" in /etc/nginx/conf.d/default.conf:3
What do I need to do to get the nginx services to be able to forward to the app service via linkerd?
I'am not sure that this is possible, use linkerd with nginx ingress.
Look on this case, https://buoyant.io/2017/04/06/a-service-mesh-for-kubernetes-part-viii-linkerd-as-an-ingress-controller/
May be it can help you.
I was actually able to solve this by looking at a different post in the same series as what Girgoriev Nick shared:
proxy_pass http://l5d.default.svc.cluster.local;
That address does cluster-local name resolution in Kubernetes, and successfully finds the Linkerd service.

Nginx reverse proxy returning 404 when server is down

I'm reverse proxying all traffic for a subdomain to a local server.
Using the following directive works great when the proxied server is up.
location / {
proxy_pass http://localhost:8900;
}
When localhost:8900 is down, nginx returns a 404. Is there a way to return a 502 or equivalent?
Turns out when I migrated my nginx.confs to a new server, the error page locations changed and nginx couldn't find the error page thus serving a 404.

nginx as load balancer server out 404 page based on HTTP response from app server

I'm using Nginx as a load balancer with app servers behind it. If the app servers return a response of 404 not found, I would like nginx to then server out a 404 page that is local on that server. I would like to do this so my app servers don't get tied up serving a static HTML file and can instead just quickly return a response and let nginx handle serving the static stuff. Does anyone know how I can accomplish this? Basically I need some kind of conditional check based on the HTTP response. Is this possible? Thanks!
Simply set the proxy_intercept_errors option to on and create an nginx error page for it.
error_page 404 /404.html;
proxy_intercept_errors on;
To ensure that nginx will serve the static file from it’s document root you also have to specify the following:
location /404.html {
internal;
}
I'm assuming here that nginx is configured to talk with your app servers as proxy, because that is the usual way and your question does not mention any of this.

Resources