Home > other >  How to mask sub-domain using nginx
How to mask sub-domain using nginx

Time:10-06

I have nodejs application working with a main domain, and its connecting thought nginx proxy pass to nodejs application, with redirection rule , as per the redirection rule if request is coming from mobile, it will redirect into a sub-domain, it is working fine.

both main domain and sub-domain are different version ,and running deferent server.

Now I want to mask this sub-domain , I don't want to see my sub-domain in browser, how can I do this.

Below mentioned my nginx conf.

server {
        listen         80;
        server_name    mydomain;
        return         301 https://$server_name$request_uri;
}
include mime.types;
server {
        listen 443 ssl;
    server_name mydomain;
        ssl_certificate /etc/ssl/CA_Bundle.ca-bundle;
        ssl_certificate_key /etc/ssl/2021/server.key;
    location / {
                add_header Access-Control-Allow-Origin *;
                proxy_set_header Host $host;
                proxy_pass http://172.17.0.1:8080;
                proxy_redirect off;
                expires off;

      set $agentflag 0;

      if ( $http_user_agent ~* "(Mobile)" ){
                set $agentflag 1;
      }

      if ($request_uri ~ "user-agent"){
              set $agentflag 0;
      }


      if ( $agentflag = 1 ) {
              rewrite ^/(.*)/$ /$1 permanent;
              return 307 https://mobile.mydomain$request_uri;
      }
        }
}


server {
        listen         80;
        server_name    mobile.mydomain;
        return         301 https://$server_name$request_uri;
}
include mime.types;
server {
        listen 443 ssl;
    server_name mobile.mydomain;
        ssl_certificate /etc/ssl/CA_Bundle.ca-bundle;
        ssl_certificate_key /etc/ssl/2021/server.key;
    location / {
                add_header Access-Control-Allow-Origin *;
                proxy_set_header Host $host;
                proxy_pass http://172.17.22.1:8081;
                proxy_redirect off;
                expires off;
    }
}

CodePudding user response:

It is working fine now,

I have used proxy_pass , instead of rewrite.

  if ( $agentflag = 1 ) {
          proxy_pass http://IP-address-mobile-app;
  • Related