Search code examples
angularnginxopenshiftnginx-reverse-proxy

Openshift nginx proxy_pass not redirecting from current host to another


This is my DockerFile

FROM docker-enterprise.net/developers-nginx/nginx1.22/rhel8/nginx-rhel8:latest
USER root
RUN   mkdir /.ionicsecurity && \
      chmod 775 /.ionicsecurity && \
      mkdir /nginxapps && \
      mkdir /nginxapps/html && \
      mkdir /nginxapps/config
COPY nginx.conf /opt/middleware/nginx/latest/nginx/nginx.conf    
ADD . /nginxapps/html
USER 1001
EXPOSE 51000/tcp
CMD ["/tmp/scripts/nginx-entrypoint.sh"]

And this is my nginx.conf

user cloudngx root;
worker_processes 1;

error_log  /opt/middleware/nginx/latest/log/nginx/error.log warn;
pid        /opt/middleware/nginx/latest/run/nginx.pid;
events { worker_connections 1024; }
 
http {
  charset utf-8;
  log_format cloudfoundry '$http_x_forwarded_for - $http_referer - [$time_local] "$request" $status $body_bytes_sent';
  access_log /opt/middleware/nginx/latest/log/nginx/access.log cloudfoundry;
 
  default_type application/octet-stream;
  client_max_body_size 3G;
  include mime.types;
   
  server {
    #listen 4200;
    listen 51000 ssl;
    server_name *.ecs.dyn.net;
    underscores_in_headers on;

    ssl_certificate      /opt/middleware/nginx/latest/certs/server.crt;
    ssl_password_file   /opt/middleware/nginx/latest/certs/global.pass;
    ssl_certificate_key  /opt/middleware/nginx/latest/certs/server.key;
    
    location /login/ {          
        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_http_version 1.1;
        proxy_pass https://springbootapi.ecs.dyn.net;
    }
        
    location /entitlement/ {                
        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_http_version 1.1;       
        proxy_pass https://springbootapi.ecs.dyn.net;
    }
    
    location / {
        root /nginxapps/html/<%= ENV["APP_ROOT"] %>/public;
        add_header Access-Control-Allow-Origin *;
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';        
        index index.html;
        try_files $uri $uri/ /index.html;
    }
  }
}

I could launch my application using https://angularapp.ecs.dyn.net, so to me it looks like nginx.conf is picked and what ever specified here is picked location / { }

When i trigger any action from the landing page say login as below

authenticate(): Observable<ConfigModel> {    
    return this.http.get<ConfigModel>("/login/authenticate", this.httpOption);
  }

it should have redirected to proxy_pass specified in location /login/ { proxy_pass https://springbootapi.ecs.dyn.net } but it is redirecting within angular like below with error

HttpErrorResponse {headers: HttpHeaders, status: 503, statusText: 'Service Unavailable', url: 'https://angularapp.ecs.dyn.net/login/getSecurityKeyVal', ok: false, …}

Tried all possible options

location /login/ { 
proxy_pass https://springbootapi.ecs.dyn.net;
}

location /login/* { 
proxy_pass https://springbootapi.ecs.dyn.net;
}

location /login/ { 
proxy_pass https://springbootapi.ecs.dyn.net/login/;
}

But it is not even picking the url i mentioning in proxy_pass.

But when i do this, it is working. I have 100s of similar navigation and dont want to hardcode. Angular/Nginx is running in 51000 and spring boot app in 8443

authenticate(): Observable<ConfigModel> {    
        return this.http.get<ConfigModel>("https://springbootapi.ecs.dyn.net"+"/login/authenticate", this.httpOption);
      }

Solution

  • issue resolved, after adding port number and adding "proxy_ssl_name" and "proxy_ssl_server_name"

    location /login/ { 
       proxy_pass https://springbootapi.ecs.dyn.net:443;
       proxy_set_header Host springbootapi.ecs.dyn.net; 
       proxy_ssl_name $host;
       proxy_ssl_server_name on;
    }