Handling multiple applications from a single domain in Nginx - nginx

Im trying to configure Nginx to handle multiple applications (backends) to work off a single domain (server_name) and I'm running into a weird problem.
What I'm trying to do is,
www.test.com -------------> serving the main application (127.0.0.1:7888)
|
-------------> management application (127.0.0.1:2666)
The initial configuration I have is as follows,
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name test.com;
## SSL settings
ssl on;
location / {
proxy_pass http://127.0.0.1:7888;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
}
location /management {
rewrite ^/management/(.*)$ /$1 break;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass https://127.0.0.1:2666;
}
}
This works well for the main application. It can also route to the /management application however it is trying to access management static files via the main application and it throws 404.
To rectify this I modified the configuration as,
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name test.com;
## SSL settings
ssl on;
location / {
proxy_pass http://127.0.0.1:7888;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
try_files $uri $uri/ #fallback;
}
location /management {
rewrite ^/management/(.*)$ /$1 break;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass https://127.0.0.1:2666;
}
location #fallback {
proxy_pass https://127.0.0.1:2666;
}
}
Now /management app works well but not the main application due to the try_files config.
Is there anyway to fix this issue ?

Related

How make working configuration for nginx local after proxy_pass?

I have two containers with Nginx
First container is proxy server
It has configuration for proxy
https://test.cloud.com/platform to http://platform-ui:8181
server {
listen 443 ssl;
server_name test.cloud.com;
location /platform/ {
proxy_pass http://platform-ui:8181/;
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Second container with nginx and UI WEB platform has this configuration
location / {
index index.html;
root /app;
try_files $uri $uri/ /index.html;
}
location #app {
proxy_pass http://platform-api:6080;
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
proxing has problem
when WEB server try to start UI WEB platform
its find static html on URL
https://test.cloud.com/index.html
but need
https://test.cloud.com/platform/index.html
How solve this problem?

Nginx Reverse Proxy: Proxying 2 different servers with using 1 server

I have 1 Nginx proxy server but I have 2 different normal server. I want to proxying this normal server using just 1 nginx proxy server. It may be simple but I couldn't find it anywhere.
Here my code in /etc/nginx/sites-available/default (With this code I can proxy just 1 server):
server {
listen 80;
#server_name 1ndwebsite.com;
root /usr/share/nginx/html;
location / {
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_pass https://122.122.122.123;
}
}
I tried this for 2nd server but it didn't work.
server {
listen 80;
#server_name 1ndwebsite.com;
root /usr/share/nginx/html;
location / {
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_pass https://122.122.122.123;
}
}
server {
listen 80;
#server_name 2ndwebsite.com;
root /usr/share/nginx/html;
location / {
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_pass https://211.211.211.213;
}
}
So interesting but I solve this issue with add include /etc/nginx/proxy_params; after the location tag.

How to send query parameter in proxy_pass in nginx

I have questions while using nginx's proxy_pass, I am trying to use the proxy_pass directive to do the following:
if the request is http://example1.com/test?number=1 it should be proxied to http://example2.com/test?number=1
Can any one help me with this?
here is my configuration in inginx
server {
listen 80;
listen [::]:80;
server_name example1.com;
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location /test {
proxy_pass http://example2.com;
}
}

How to proxy_pass to a sub path

I have a host with two containers:
nginx
check_mk
the check_mk interface is accessible by http://172.17.0.2:5000/cmk
I have proxy_pass rule set up in nginx:
server {
listen 80;
server_name cmk.domain.com;
location / {
proxy_pass http://172.17.0.2:5000;
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;
}
}
When I hit the nginx on port 80 with cmk.domain.com/cmk it works.
What I want is that when hitting the server_name cmk.domain.com, the /cmk would be added automatically.
I tried doing proxy_pass http://172.17.0.2:5000/cmk; but then I get a page not found error.
What am I missing here?
Try this
server {
listen 80;
server_name cmk.domain.com;
location /cmk {
proxy_pass http://172.17.0.2:5000;
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;
}
location / {
return 301 http://cmk.domain.com/cmk$request_uri;
}
}

Reverse proxy nginx to application

I have a synology and I want to link a subdomain to one of this application. This is the URL I usually use to access to this service:
192.168.0.17:5001//index.cgi?launchApp=SYNO.SDS.App.FileStation3.Instance&launchParam=openfile%3D%252FOliver%252F
But this is too long and I want to access it directly this mydomaine.com
How should I configure the server ?
I tried this but it doesn't work :
server {
listen 80;
listen [::]:80;
resolver 89.2.0.1;
server_name test.fr;
location / {
proxy_set_header Host $http_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 $scheme;
proxy_intercept_errors on;
proxy_http_version 1.1;
proxy_pass http://192.168.0.17:5000/index.cgi?launchApp=SYNO.SDS.App.FileStation3.Instance&launchParam=openfile%3D%252FOliver%252F;
}
}
Thanks a lot for your help!

Resources