Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx URL masking to a different domain

Tags:

nginx

rewrite

There's a few similar questions on SO, but none exactly mine, and I've had no luck trying to adapt their answers so far.

I want to map the URL http://sub.example.com to https://123.12.12.12/path, such that the browser still shows the URL http://sub.example.com.

My Nginx config file looks like,

server {
    listen 80;
    server_name sub.example.com;

    location / {
        proxy_pass https://123.12.12.12;
        rewrite ^/$ /path last;
    }
}

The routing works here, but the URL displayed is http://sub.example.com/path. How do I make it display only http://sub.example.com?

like image 963
kennysong Avatar asked Dec 25 '14 07:12

kennysong


People also ask

How do I mask a domain to another domain?

Domain masking can also be facilitated using DNS settings on your hosting provider. To redirect the domain you need to select your domain name and set a (masked) 301 redirect which hides the actual domain name from the address on the user's browser.

Is there a way to mask a URL?

There are many ways to mask a URL. One of these ways is to embed an HTML frame in the main website, redirecting a user to a completely different site. Others include virtual hosting or rewriting a URL. Doing this masks the original URL displaying the masked version instead.


2 Answers

server {
    listen 80;
    server_name sub.example.com;

    location / {
        proxy_pass https://123.12.12.12/path;
    }
}

Thats how it works. If proxy_pass contains locations part - current location will be replaced to specified. http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

But it's help only for http request and http redirects. If application create html with links https://123.12.12.12 - it's still unchanged. In this case you can try ngx_http_sub_module.

like image 169
Dmitry MiksIr Avatar answered Oct 22 '22 17:10

Dmitry MiksIr


I did like this:

server {
    listen 80;
    listen [::]:80;
    listen 443 http2 ssl;
    listen [::]:443 http2 ssl;

    server_name domain1;

    if ($request_method ~* OPTIONS|GET|HEAD) {
        return 301 https://domain2$request_uri;
    }

    location ~* api {
        proxy_pass https://domain2$request_uri;
    }
}

Because post-requests will cause a 405 error when redirecting.

like image 45
Ilya Degtyarenko Avatar answered Oct 22 '22 18:10

Ilya Degtyarenko