18

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?

kennysong
  • 2,044
  • 6
  • 24
  • 37

2 Answers2

36
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.

Dmitry MiksIr
  • 4,245
  • 1
  • 18
  • 29
  • 1
    What if I want to pass HOST header to the backend and there is no DNS name for backend host? Is that possible as well? – VsMaX Sep 02 '17 at 15:04
  • 1
    @VsMaX you can use `proxy_set_header Host somevalue.com;` or, if you need to pass frontend host, pass $host variable `proxy_set_header Host $host;` – Dmitry MiksIr Sep 02 '17 at 15:36
  • 1
    the problem is that when I set `proxy_set_header Host somevalue.com;` it still does DNS query for that hostname, which doesn't exist. And then nginx fails with erorr DNS name not found... – VsMaX Sep 03 '17 at 11:57
  • 1
    @VsMaX Is your proxy_pass with ip address? proxy_set_header should not resolve domain, check other parts of config – Dmitry MiksIr Sep 03 '17 at 13:01
  • @DmitryMiksIr I have similar problem, tried your solution but when the content from `proxy_pass` domain is showed, it is also using `/path` which does not exist on that domain so I get page not found. How can I stop sending `/path` to the new masked domain?. To be more specific, Im trying to do masked redirect from `example.com/path` to `path.example.com` (wordpress running here) – mrRobot Oct 03 '21 at 13:49
  • There is two solutions. First and correct one is to learn your application to construct correct URI. Many backends allow to specify basePath in config. Second solution is to modify body on the fly and it should be used only if backend code can not be modified at all. For this you can use ngx_http_sub_module which allow you to replace any strings inside the body. – Dmitry MiksIr Oct 04 '21 at 14:05
  • @DmitryMiksIr you are best! Changed site url in wordpress config and it works like a charm, no need to use `ngx_http_sub_module` at all.. still have some CORS problems there but thats some other problem... – mrRobot Oct 04 '21 at 15:12
1

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.

Ilya Degtyarenko
  • 1,359
  • 8
  • 16