Nginx not redirecting on named route - nginx-reverse-proxy

I'm trying to setup a reverse proxy to a sentry relay using Nginx. Config file as follows:
events {
worker_connections 768;
}
http {
server {
listen 0.0.0.0:80;
location /sentry-relay {
proxy_pass http://127.0.0.1:3001;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
Browsing directly to the relay server on port 3001 works fine:
However using the location path set in Nginx fails:
I've also put the redirect onto the default path: location / and it works fine. Why won't this custom path redirect properly?

I worked it out.
Nginx will append the location prefix to the proxy server request unless this prefix is replaced.
Thus to fix I changed:
proxy_pass http://127.0.0.1:3001;
to
proxy_pass http://127.0.0.1:3001/;
The extra slash is used to replace the sentry-relay path.

Related

how to proxy to my domain with port from request urls that start with api?

I have a loopback 3 set up listening on the port 3000. And my front end app is built with Vue JS. ( I uploaded dist files to the server). Whenever I make an api call (https://example.com/api/xxx), I need to proxy to (https://example.com:3000/api/xxx) in order to avoid cors issues.
How do I resolve this?
FYI, the loopback and vueJS are hosted on the same web server (apache, centos8)
Use NGINX Reverse Proxy
https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/
http://localhost:5000 - points to your Vue.js app
http: //localhost:3000 - points to your REST API app
server {
listen 80;
server_name example.org;
location / {
proxy_pass http://localhost:5000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
}
location ^ ~/api {
proxy_pass http: //localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
}
}

nginx proxy_pass for get request api call with parameters

I have simple flask rest api that fetches data from database and gives result to user in json file. flask server gets parameters from html input fileds upon submit and these parameters are used to fetch data from database. It is working fine with flask inbuilt WSGI server. I wanted to deploy in production with nginx as my web server and gunicorn as application server. When I run docker container and access the root url, i can get html form for the user to input parameters. When I click on submit, another api resource call gets invoked, but I get either 'URL not found' or 'internal server error'. This surely problem with nginx location configuration for my get request api call with parameters in URL. Please help me how to configure nginx proxy_pass URL for this kind of request.
My browser request URL look something like this when I submit form.
http://IP address/api/v1/service?key=12345&name=abc&id=1234
HTML (form.html)
<form name="device" action="/api/v1/service">
Python view functions
#app.route('/')
def my_form():
return render_template('form.html')
#app.route('/api/v1/service', methods=['GET'])
def my_form_get():
..............
.............
nginx server
server {
listen 80;
location / {
proxy_pass http://localhost:5000/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /api/v1/service {
proxy_pass http://localhost:5000/api/v1/service;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Gunicorn configuration
[program:gunicorn]
command=/usr/bin/gunicorn run:app -b localhost:5000
directory=/deploy/app
You can use the following nginx configuration:
upstream v1service {
server v1servicecontainer:8080;
}
server {
listen 80;
location ~ ^/api/v1/service(.*)$ {
resolver 127.0.0.1;
proxy_pass http://v1service/api/$1$is_args$args;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}

Loadbalancing with Nginx error not redirecting properly

I was able to get the load balancer to work only on inception. It seems that when I do any clicks on the page, it renders http://backend , and not the actual web address.
here is my config on the downstream
upstream backend {
server unix:///var/www/my_app/shared/tmp/sockets/puma.sock;
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

Customize URL location for loadbalancing nginx

I am new to nginx.
I am having WCF Rest Service listening in the following url,
127.0.0.1:portHere/Service1.svc/RemainingRestURLTemplate.
Here is the config I am having.
http {
upstream servers_customserver {
server 127.0.0.1:62133;
server 127.0.0.1:62134;
server 127.0.0.1:62135;
}
server {
listen 8090;
server_name localhost;
location /two/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host #server_name;
proxy_pass http://servers_customserver;
}
}
}
Upon entering localhost:8090/two/, I thought the upstream will work,but the browser reports problem, as in the image.
But the browser works fine when I have removed the "/two/" from my location, as below.
So, how to make my upstream to work, only when the user typed the url ends with "/two/".
Could some one share some input on it.
Thx in advance.
Finally after some hrs, found that trailing slash did the work.
proxy_pass http://servers_customserver/ works instead of proxy_pass http://servers_customserver Correct me if am wrong.
Thx.

Running Apache Zeppelin with nginx as reverse proxy

In our current architecture we have two apache front servers, in front of them, we have an nginx load balancer. And in front of that an nginx reverse proxy.
My problem is that i'm trying to run Apache Zeppelin through the reverse proxy, and i'm having some problems with the websockets.
I get an error like this : 400 HTTP method GET is not supported by this URL
And here is a screenshot of what the Chrome's Networks tab shows :
I add my reverse proxy config for Zeppelin:
error_log /var/log/nginx/nginx_error.log warn;
server {
listen 80;
server_name localhost;
location /zeppelin/ {
proxy_pass http://zeppelin:8080/;
proxy_http_version 1.1;
proxy_set_header Upgrade websocket;
proxy_set_header Connection upgrade;
}
# fallback
location / {
return 301 http://ci.blablalablab.com/app/;
}
}
Zeppelin is running inside a docker container, and i have exposes the 8080 port, its host name is : zeppelin.
If you have any questions on the architecture or so, don't hesitate to ask.
Thank you very much guys !
you can add to your reverse proxy configuration
location /ws { # For websocket support
proxy_pass http://zeppelin:8080/ws;
proxy_http_version 1.1;
proxy_set_header Upgrade websocket;
proxy_set_header Connection upgrade;
proxy_read_timeout 86400;
}
Reference: Zeppelin 0.7 auth docs
After a lot of digging around, i ended up with this configuration :
location /zeppelin/ {
proxy_pass http://zeppelin:8080/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
location /zeppelin/ws {
proxy_pass http://zeppelin:8080/ws;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
This is working pretty good, thank you everyone for your efforts ;)