0

I want to achieve url rewrite as such:
https://documentation.domain.com/login to https://app.domain.com/login
https://api.domain.com/login to https://app.domain.com/login
, any sudomain with url {anything}.domain.com/login to https://app.domain.com/login

https://app.domain.com/sdks to https://documentation.domain.com/sdks (with all query parameters preserved)
https://api.domain.com/sdks to https://documentation.domain.com/sdks (with all query parameters preserved)
, any sudomain with url {anything}.domain.com/sdks to https://documentation.domain.com/sdks

https://app.domain.com/restApi to https://documentation.domain.com/restApi (with all query parameters preserved)
https://api.domain.com/restApi to https://documentation.domain.com/restApi (with all query parameters preserved)
, any sudomain with url {anything}.domain.com/restApi to https://documentation.domain.com/restApi

This is what I currently have:

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

        ssl_certificate     /home/company/server/ssl/domain_cloudflare.pem;
        ssl_certificate_key /home/company/server/ssl/domain_cloudflare_privatekey.key;

        server_name www.*.domain.com *.domain.com;

        client_body_buffer_size     32k;
        client_header_buffer_size   8k;
        large_client_header_buffers 4 16k;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass        https://localhost:8443;
                proxy_set_header  X-Real-IP $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header  Host $http_host;
        }

        location /login {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass        https://localhost:8443/login;
                proxy_set_header  X-Real-IP $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header  Host $http_host;
                return 302 https://app.domain.com/login;
        }

        location /restApi {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass        https://localhost:8443/restApi;
                proxy_set_header  X-Real-IP $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header  Host $http_host;
                return 302 https://app.domain.com/restApi;   
        }


        location /sdks {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass        https://localhost:8443/sdks;
                proxy_set_header  X-Real-IP $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header  Host $http_host;
                return 302 https://app.domain.com/sdks;
        }


        location /api {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass        https://localhost:8443/api;
                proxy_set_header  X-Real-IP $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header  Host $http_host;
                return 302 https://api.domain.com/api;
        }
}

Cloudflare SSL i set to Full Strict. Changing to Fulll/Flexible changes nothing. This setup mostly works. This works but results in ERR_TOO_MANY_REDIRECTS How can I correct this ? what is wrong here ? Please help me with this

EDIT:

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


        ssl_certificate     /home/company/domain/ssl/domain_cloudflare.pem;
        ssl_certificate_key /home/company/domain/ssl/domain_cloudflare_privatekey.key;


        server_name app.domain.com;

        client_body_buffer_size     32k;
        client_header_buffer_size   8k;
        large_client_header_buffers 4 16k;

        location / {
               proxy_pass        https://localhost:8443/login;
                proxy_set_header  X-Real-IP $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header  X-Forwarded-Host $http_host;
               proxy_set_header  X-Forwarded-Server $host;
               proxy_set_header  Host $http_host;
               proxy_set_header  X-Forwarded-Proto https;
#               return 302 https://app.domain.com/login;
        }

}

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


        ssl_certificate     /home/company/domain/ssl/domain_cloudflare.pem;
        ssl_certificate_key /home/company/domain/ssl/domain_cloudflare_privatekey.key;


        server_name documentation.domain.com;

        client_body_buffer_size     32k;
        client_header_buffer_size   8k;
        large_client_header_buffers 4 16k;

        location /sdks {
               proxy_pass        https://localhost:8443/sdks;
                proxy_set_header  X-Real-IP $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header  X-Forwarded-Host $http_host;
               proxy_set_header  X-Forwarded-Server $host;
               proxy_set_header  Host $http_host;
               proxy_set_header  X-Forwarded-Proto https;
#              return 302 https://documentation.domain.com/sdks;
        }

        location /restApi {
               proxy_pass        https://localhost:8443/restApi;
                proxy_set_header  X-Real-IP $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header  X-Forwarded-Host $http_host;
               proxy_set_header  X-Forwarded-Server $host;
               proxy_set_header  Host $http_host;
               proxy_set_header  X-Forwarded-Proto https;
#               return 302 https://documentation.domain.com/restApi;
        }

}

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


        ssl_certificate     /home/company/domain/ssl/domain_cloudflare.pem;
        ssl_certificate_key /home/company/domain/ssl/domain_cloudflare_privatekey.key;


        server_name api.domain.com;

        client_body_buffer_size     32k;
        client_header_buffer_size   8k;
        large_client_header_buffers 4 16k;

        location /api {
               proxy_pass        https://localhost:8443/;
                proxy_set_header  X-Real-IP $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header  X-Forwarded-Host $http_host;
               proxy_set_header  X-Forwarded-Server $host;
               proxy_set_header  Host $http_host;
               proxy_set_header  X-Forwarded-Proto https;
#               return 302 https://api.domain.com/;
        }

}
8
  • You should declare distinct server blocks for app, api, documentation and anything.
    – Alexey Ten
    Sep 12 at 10:08
  • @AlexeyTen separate server blocks with location blocks inside them ? Sep 12 at 10:28
  • Yes. Right now you request to app.domain.com/login lands to the same location and sends redirect.
    – Alexey Ten
    Sep 12 at 10:34
  • Ok but how to do this ? should server files be placed in separate domain named files ? please explain this to me. What about proxy_set_headers ? Sep 12 at 10:37
  • @AlexeyTen I edited the main. Is this ok ? it seems to cause the same problem Sep 12 at 11:22

0

You must log in to answer this question.

Browse other questions tagged .