Nginx reverse proxy to two services - nginx

I'm running nodeJS script..
on localhost:9001
it is running behind nginx reverse proxy
it accept request in the form of /v{{ version }}/{{ lang }}/...
So for example:
domain.com/api/v1/en/news
domain.com/api/v2/fr/news
domain.com/api/v3/en/news
Until now I had this is nginx
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:9001/;
}
and everything work.
My goal now is to run additional (identical) script on localhost:9002 which will accept v4 requests. And v3, v2 and v1 will be still 'processed' by localhost:9001
So I want that request domain.com/api/v4/en/news is routed to localhost:9002
I put this above current rule like this
location ~* /api/v4/(.*)$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:9002/v4/$1;
}
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:9001/;
}
Request /v3/.. is routed to localhost:9001 (as expected) but /v4/.. returns 502.
Any pointers?

location /api/v4/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:9002/v4/;
}
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://localhost:9001/;
}

Related

Nginx proxy pass not rendering

I'm trying to serve some services as a subdomain but it looks like nginx is not doing it right.
I have 3 docker containers serving zabbix, webmin and mediawiki.
example.com/zabbix (works fine)
example.com/webmin (not rendering)
example.com/wiki (not rendering)
server {
listen 80;
server_name example.com;
root /var/www/html/;
location /zabbix {
proxy_pass http://192.168.0.71:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /wiki {
proxy_pass http://192.168.0.71:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /webmin {
proxy_pass http://192.168.0.71:10000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Also add in /etc/webmin/config:
webprefix=/webmin
webprefixnoredir=1
and LocalSettings.php
$wgServer = "http://example.com";
$wgArticlePath = "/wiki/$1";
$wgUsePathInfo = true;
wiki pages and webmin looks weird:
wiki screenshot
Any help will be appreciated
thanks!

How to proxy pass to a certain url depends on the request url by nginx?

I have two hello servers in server1(1.1.1.1) and server2(1.1.1.2). Now I want to use Nginx(example.com) to proxy pass request to the certain server like this:
Request Url:http://example.com/hello1
proxy pass:http://1.1.1.1/hello
Reqeust Url:http://example.com/hello2
proxy pass:http://1.1.1.2/hello
Just add the location block to example.com config. This should work.
location ^~ /hello1 {
proxy_set_header Proxy "";
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_pass http://1.1.1.1/hello;
proxy_redirect off;
}
location ^~ /hello2 {
proxy_set_header Proxy "";
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_pass http://1.1.1.2/hello;
proxy_redirect off;
}

Rewrite a subdomain to a backend proxy whith nginx

if have a subdomain on my nginx webserver configuration: sub.mydomain.com
and i have a backend server which listen on port 5000: http://127.0.0.1:5000
is it possible to pass all subdomain calls to the backend?
Like: https://sub.mydomain.com/list to http://127.0.0.1:5000/sub/list
This should work with all methods: POST, PUT, GET, DELETE
UPDATE:
when i call my server: https://mysubdomain.mydomain.com
with the following configuration:
upstream http_backend {
server 127.0.0.1:5000;
}
server_name ~^(?<subdomain>[^.]+)\.mydomain\.com;
This does not work (error: 404):
location / {
proxy_pass http://http_backend/$subdomain/;
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-Proto https;
}
This works fine:
location / {
proxy_pass http://http_backend/mysubdomain/;
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-Proto https;
}
When i log the $subdomain variable in the access_log, it seems to be correct.
nginx version: nginx/1.9.15
To pass all subdomains you need to set it in server name by putting dot before domain.
server_name .mydomain.com;
Yes, you can use variables in proxy_pass. And you can extract part of domain using regexp server name.
server {
server_name ~^(?<sub>[^.]+)\.example\.com;
# now subdomain of example.com placed to $sub
# please, note, this rule do not work for http://example.com
location / {
proxy_pass http://127.0.0.1:5000/$sub/;
# Path part of proxy_par URI will replace path
# part of location directive (so / -> /$sub/, /xxxx/ -> /$sub/xxxx/)
}
}
Thats all :)
It seems nginx does not add the $uri to the proxy_pass if i use the $subdomain variable.
The following solution works:
location / {
proxy_pass http://http_backend/$subdomain/$uri;
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-Proto https;
}

Redirect Local Port to Outside Network using Nginx

I want to redirect my http://127.0.0.1:9090/data/admin/ to http://94.162.152.12:9090/admin/ that can be access outside in my network. I already open port 9090 in my router.
Take a look my nginx config
server {
listen 9090;
server_name 94.162.152.12;
location ~* /data/admin/ {
proxy_pass http://127.0.0.1:9090;
proxy_redirect off;
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;
}
}
In case you mean proxying from http://94.162.152.12:9090/admin/ to http://127.0.0.1:9090/data/admin/ you might want to do it as follow:
server {
listen 9090;
server_name 94.162.152.12;
location /admin {
rewrite ^/admin/(.*) https://127.0.0.1:443/data/admin/$1 permanent;
proxy_pass http://127.0.0.1:9090;
proxy_redirect off;
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;
}
}
This will rewrite all requests for /admin/* to /data/admin/* and call localhost as backend.

nginx proxy path with prefix for multiple apps

I've got a problem in configuration nginx proxy_pass. There's a situation like this:
Server 1 - xx.xx.xx.xx - proxy server
Server 2 with application A - yy.yy.yy.yy
Server 3 with application B - zz.zz.zz.zz
Configuration listed below:
server {
listen 80; ## listen for ipv4; this line is default and implied
location /app_a {
rewrite /app_a/(.*)$ /$1 break;
access_log off;
proxy_pass http://yy.yy.yy.yy/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Upstream $remote_addr;
}
location /app_b {
rewrite /app_b/(.*)$ /$1 break;
access_log off;
proxy_pass http://zz.zz.zz.zz;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Upstream $remote_addr;
}
}
Then when I go with my browser xx.xx.xx.xx/app_a or xx.xx.xx.xx/app_b there is no static files loaded (404) and all links are without application prefix (xx.xx.xx.xx/login)
How I can configure nginx so the links will be displayed like:
xx.xx.xx.xx/app_a/login
xx.xx.xx.xx/app_b/login

Resources