PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 1, 2022

[FIXED] How to mask sub-domain using nginx

 September 01, 2022     nginx, nginx-config, nginx-reverse-proxy, url-rewriting     No comments   

Issue

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

Solution

It is working fine now,

I have used proxy_pass , instead of rewrite.

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


Answered By - shufilkhan
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing