Nginx reverse proxy return 404 - nginx

My Nginx installed and running, below is the config from /etc/nginx/nginx.conf , I want to forward all /api/* to my tomcat server, which is running on the same server at port 9100(type http://myhost:9100/api/apps works) , otherwise, serve static file under '/usr/share/nginx/html'. Now I type http://myhost/api/apps give an 404. What's the problem here?
upstream myserver {
server localhost:9100 weight=1;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location ^~ /api/ {
proxy_pass http://myserver/;
}
location / {
}
}

The proxy_pass statement may optionally modify the URI before passing it upstream. See this document for details.
In this form:
location ^~ /api/ {
proxy_pass http://myserver/;
}
The URI /api/foo is passed to http://myserver/foo.
By deleting the trailing / from the proxy_pass statement:
location ^~ /api/ {
proxy_pass http://myserver;
}
The URI /api/foo is now passed to http://myserver/api/foo.

Related

multiple location blocks in nginx and proxy pass

I am trying to run 2 applications behind an NGINX server.
one is listening on 3000 (grafana) and one is listening on 9090 (prometheus)
My current server block nginx.conf looks like this:
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
proxy_pass http://localhost:3000/;
}
location /prometheus{
proxy_pass http://localhost:9090/;
}
}
Now for Grafana, this works perfectly and everything works in the dashboard.
But when typing Domain/prometheus it still redirects me to grafana instead of Prometheus.
Do I need todo something specific to have it working in this setup that everything besides /prometheus is redirected to grafana?
By default, http://localhost:9090 will be redirected to http://localhost:9090/graph, so the request is being redirected as below:
# the origin request
http://Domain/prometheus
# the request redirect by nginx
http://localhost:9090/
# the request redirect by prometheus
http://Domain/graph
# the request redirect by nginx again
http://localhost:3000/graph
You can check this by using F12 in Chrome.
To fix this, I recommend you separate the domain into two domains:
server {
listen 80;
server_name Domain-Grafana; # Domain for Grafana
root /usr/share/nginx/html;
index index.html index.htm;
location / {
proxy_pass http://localhost:3000/;
}
}
server {
listen 80;
server_name Domain-Prometheus; # Domain for Prometheus
root /usr/share/nginx/html;
index index.html index.htm;
location / {
proxy_pass http://localhost:9090/;
}
}
So I solved it now by doing this:
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm;
location /prometheus {
proxy_pass http://localhost:9090/prometheus;
}
location / {
proxy_pass http://localhost:3000/;
}
}

How to redirect traffic nginx in dynamic mode

In my nginx server , I want to do this:
mydomain.com/api/xxxx
redirect to mynewdomain.com/api/testing/xxxx
I try to do by different ways but no ones works for me, always return a 502 error;
First way
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/ {
return 301 https://mynewdomain.com/api/testing/$uri;
}
}
Second way
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/(.*) {
proxy_pass https://mynewdomain.com/api/testing/$args_v;
}
}
And I tryed to only redirect to specific direction and this works, but i don't want to hardcode all petitions to redirect,
Static redirect
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/ {
proxy_pass https://mynewdomain.com/api/testing/redirect1;
}
Any help for make this dynamic?
location ~ ^/api/(.*) {
return 301 https://mynewdomain.com/api/testing/$1;
}
Try this, because $uri also includes /api.

Nginx configuration doesn't work properly on Tomcat

Hi I am facing issue when configuring nginx as proxy server to redirect request to my tomcat server. I have 3 tomcat server running on different machine & different port like this
192.168.51.115:8115
192.168.51.120:8120
192.168.51.130:8130
Now I want to config nginx to pass request to my three server sequentially like this
www.example.com/app1
www.example.com/app2
www.example.com/app3
Real IP: 123.123.123.123
This is my configuration under - site-enabled
server {
listen 80;
server_name example.com www.example.com;
location /app1 {
proxy_pass "http://192.168.51.115:8115";
}
location /app2 {
proxy_pass "http://192.168.51.120:8120";
}
location /app3 {
proxy_pass http://192.168.51.130:8130;
}
}
Note: When i put location directive placing just / then it works but doesn't work on /* like app1,app2 or app3
Can you try using ^~ as modifier in your location block ?
like
server {
listen 80;
server_name example.com www.example.com;
location ^~ /app1 {
proxy_pass "http://192.168.51.115:8115";
}
location ^~ /app2 {
proxy_pass "http://192.168.51.120:8120";
}
location ^~ /app3 {
proxy_pass http://192.168.51.130:8130;
}
}

Ngrok not tunneling properly Nginx

I have my flask application deployed on Nginx over my VM.
Everything is deployed Ok and I can request my apis on http://my.ip.number (I have a public IP)
But when I run Ngrok (I need https and I don't have a domain name to generate a SSL certificate), the URL https//number.ngrok.io shows me the Nginx home page (Welcome to Nginx) instead my webapp.
Why is this happening?
P.D: When I run "curl localhost" I get the Nginx Welcome Page but when I exec "curl -4 localhost" I get my webapp home page
etc/nginx/site-available/myproject
server {
listen 80;
server_name 0.0.0.0;
location / {
include proxy_params;
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name public.ip;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
Any request coming in from ngrok, has the Host header set to the ngrok URL. The behaviour of nginx would be to try and match one of the server blocks in your configuration above, and default to the first one if no server_name matches the Host header.
However, I'm guessing there's another configuration file at /etc/nginx/conf.d/default.conf or /etc/nginx/sites-enabled/0-default which has a listen directive with default_server set. That will be catching these requests and serving the "Welcome to nginx!" page.
I suggest you look for that file, and remove it which should solve the issue.
However you could also simplify the above configuration and simply have:
server {
listen 80;
server_name localhost;
location / {
include proxy_params;
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
Provided there's not another server block hiding somewhere else in the configuration with a directive like listen 80 default_server; then this should catch all requests.
For more info see: How nginx processes a request

Nginx proxy_next_upstream doesn't work

I want nginx to search my local host for the file first and on a 404 error it should search server 1.1.1.1.
I am able to fetch the file that is located on local host, but not able to get from server 1.1.1.1.
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/access.log main;
location /products/ {
proxy_next_upstream http_404;
root /var/foo;
}
}
server {
listen 80;
server_name 1.1.1.1;
location /products/ {
#########
}
}
I guess proxy_next_upstream is not switching to the server.
Any help on this would be appreciated.
The proxy_next_upstream directive is a configuration directive to control re-request from a group of upstream servers by a proxy_pass if request to one of them fails. It doesn't make sense without proxy_pass and an upstream block defined. You may use it if you proxy to multiple upstream servers like this:
upstream backends {
server 192.2.0.1;
server 192.2.0.2;
...
}
server {
...
location / {
proxy_pass http://backends;
proxy_next_upstream error timeout http_404;
}
}
If you want nginx to search for a file on disk, and if it's not found - proxy request to another server, configure it e.g. using try_files fallback instead:
location / {
root /path/to/root;
try_files $uri #fallback;
}
location #fallback {
proxy_pass http://...
}
See http://nginx.org/r/try_files for more info about the try_files directive.

Resources