0

I'm configuring Nginx to handle redirects for a domain, but I'm encountering an issue with preserving the URL for a specific path. Here's my scenario:

I have a domain, let's say landing.immancrze.us, and I want all requests to this domain to be redirected to app.immancrze.us, except for requests to the path landing.immancrze.us/potential-ownership. For the latter, I want to serve the content without redirection and ensure that the URL remains unchanged.

for example:

  1. Requests to landing.immancrze.us will be redirected to app.immancrze.us.

  2. Requests to landing.immancrze.us/potential-ownership will be served directly without redirection and the URL in the browser will remain unchanged like http://landing.immancrze.us/potential-ownership. but it will getting changed to http://app.immancrze.us/potential-ownership

Here's the Nginx configuration I've tried:

server {
    listen 80;
    server_name landing.immancrze.us;

    # Redirect root path to app.immancrze.us
    location = / {
        return 301 http://app.immancrze.us$request_uri;
    }

    # Serve content for /potential-ownership without redirection
    location = /potential-ownership {
        root /usr/share/nginx/html/;
        index index.html;
        try_files $uri $uri/ /index.html;
    }

    # Serve JavaScript files with custom headers
    location ~ \.js$ {
        sendfile off;
        add_header Last-Modified $date_gmt;
        add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        if_modified_since off;
        expires off;
        etag off;
        #proxy_no_cache 1;
        #proxy_cache_bypass 1;
    }

    # Handle all other requests by redirecting to app.complycta.us
    location / {
        return 301 http://app.immancrze.us$request_uri;
    }
}
6
  • Is /usr/share/nginx/html/potential-ownership a directory containing index.html? Commented May 14, 2024 at 9:12
  • no index.html contained in this /usr/share/nginx/html Commented May 14, 2024 at 9:35
  • To serve index.html from /usr/share/nginx/html/ you need to use try_files /index.html =404; Commented May 14, 2024 at 10:01
  • If index.html also requires resource files (JS, CSS, JPEG) you may have a problem. You will need to identify the URLs for those resource files and add exceptions for those too. Commented May 14, 2024 at 10:03
  • yes getting ERR_ABORTED 404 for resource files (JS, CSS, JPEG) Commented May 14, 2024 at 10:16

1 Answer 1

0

location = / and location = /potential-ownership places in one level and nginx terminates right after the first comparison. location = / cannot obviously contain nested locations

Try to move your block

 # Redirect root path to app.immancrze.us
    location = / {
        return 301 http://app.immancrze.us$request_uri;
    }

after this block

# Serve content for /potential-ownership without redirection
    location = /potential-ownership {
        root /usr/share/nginx/html/;
        index index.html;
        try_files $uri $uri/ /index.html;
    }

and try don't use = for location /potential-ownership (expl. location /potential-ownership, location /)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.