How make working configuration for nginx local after proxy_pass? - nginx

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?

Related

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 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;
}
}

Handling multiple applications from a single domain in 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 ?

How to configure nginx to support signalr3 under cloudflare?

I am struggling with signalr3 and nginx reverse proxy configuration, my nginx cfg looks like this:
server {
listen 80;
server_name my.customdomain.com;
location / {
root /pages/my.customdomain.com;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
## send request back to kestrel ##
location /proxy/ {
proxy_pass http://xxxxxxxxxx.westeurope.cloudapp.azure.com/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
What do I miss here?
When I browse my page, I receive OK for
GET /proxy/notifications/negotiate
and
GET /proxy/notifications?id=uFQtMDg1dXib6LGvUssQhQ
but 404 for POST
POST proxy/notifications?id=uFQtMDg1dXib6LGvUssQhQ
pls halp!
ps. my Hub is very simple...
[AllowAnonymous]
public class NotificationHub : Hub
{
}
This is a websocket based app so you need additional nginx config
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
#proxy_set_header X-Forwarded-Proto https;
proxy_pass 127.0.0.1:8080;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}

keystonejs behind nginx proxy

I got stack wiht keystonejs behind nginx .
the nginx .conf :
server {
listen 8080;
server_name localhost;
location /wanghuan/ {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:3000/;
}
location ~ .*\.(img|gif|jpg|jpeg|png|bmp|swf|js|css)$ {
root /Users/macmini/Desktop/test/wanghuan/public;
}
but keystone admin Ui still block ,the static file can't be find ,
how can i set the admin ui static file?
You should just setup a proxy pass to pass all of your arguments to keystone like so:
upstream keystone {
server localhost:3000
}
server {
listen 8080;
server_name localhost;
location / {
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_pass http://keystone;
proxy_redirect off;
}
location ~ .*\.(img|gif|jpg|jpeg|png|bmp|swf|js|css)$ {
root /Users/macmini/Desktop/test/wanghuan/public;
}
Not sure you are trying to put all of this under {domain}/wanghuan or just {domain} but this nginx config should work if you want the first option just change the location to be /wanghuan
You need to turn keystone.js init block with this additional option
'trust proxy' : true
and your nginx proxy code block just as simple as:
server {
listen 8080;
server_name localhost;
location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:3000/;
}
}

Resources