2

I'm trying to change displayed urls to keep all sub domain urls relative to the main domain, example maindomain.com/some_page would be served from sub.maindomain.com/some_page but to keep the displayed url in browser to maindomain.com/some_page. Considering that the main domain and the sub are hosted on different servers as explained hereunder:

  1. maindomain.com, is wordpress powered by LEMP server with following nginx.conf:

    server {
    listen 80;
    server_name maindomain.com;
    root /var/www/html;
    index index.php index.html index.htm;
    include /var/www/html/nginx.conf;
    client_max_body_size 65M;
    
    error_page 404 @sub;
    
    location / {
            try_files $uri $uri/ /index.php;
    }
    
    location /xmlrpc.php {
            deny all;
    }
    
    location = /xmlrpc.php {
            deny all;
            access_log off;
            log_not_found off;
            return 444;
    }
    
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_intercept_errors on;
    }
    
    location ~* ^.+\.(?:css|cur|js|jpe?g|gif|htc|bmp|ico|rtf|png|xml|otf|ttf|eot|woff|mid|midi|woff2|svg|png|ico|zip|tgz|gz|rar|bz2)$ {
            expires max;
            log_not_found off;
    }
    
    # redirect all not found pages to the sub domain
    location @sub {
            rewrite ^ http://sub.maindomain.com$request_uri redirect;
    }
    }
    
  2. sub.maindomain.com, is Node.js app with nginx as proxy server.

    upstream app {
      server 127.0.0.1:9000 fail_timeout=120;
    }
    
    server {
      listen 80;
      server_name sub.maindomain.com;
      root /var/webapp;
      # Compression
      gzip on;
      gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
      gzip_buffers 16 8k;
      gzip_disable "MSIE [1-6]\.";
      # Error pages
      error_page 404 /404.html;
      location /404.html { root /app/public; }
      error_page 500 /500.html;
      location /500.html { root /app/public; }
      # Client configuration
      client_max_body_size 4G;
      keepalive_timeout 120;
    
      # Logs
      access_log no;
    
      # Serve static assets
      location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
      }
    
      # Try physical files first then proxy to running application
      try_files $uri/index.html $uri @app;
      location @app {
        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-Url-Scheme $scheme;
        proxy_redirect off;
        rewrite ^/(.*) http://maindomain.com$1 break;
        proxy_pass http://app;
      }
    }
    

I've tried different variations of rewriting rules but nothing doing the trick.

RmK
  • 117
  • 7
  • Just proxy pass to the upstream. `rewrite` will, as the name suggests, rewrite the url – miknik Jul 28 '18 at 15:17
  • @miknik you mean i don't need this line rewrite ^/(.*) http://maindomain.com$1 break; and only proxy pass to upstream but in this case the ned result will be redirecting from maindomain to the sub and the url will be sub.maindomain.com – RmK Jul 28 '18 at 15:59
  • 2
    Rewrite is usually used to perform `internal` redirection - meaning `inside` nginX (so the client is `usually` not aware of the rewrite - unless you rewrite to something which starts with `http://` or `https://`), while `return 301` is used when you want to make the client aware of the redirection. In your case you can not do what you want directly - you will have to use either AJAX or frames/iframes, otherwise the real URL will be exposed in the address bar. – IVO GELOV Jul 28 '18 at 16:02

0 Answers0