Score:0

Nginx server under load balancer, when /folder is found redirects to /folder/ but changes the host url

yt flag

I have nginx server setup in google cloud. Server is being accessed from a host domain through gcloud pointing.

Like this:

https://server.com/test/ points to instance group with one of instances with ip http://39.99.99.99.55/project/

And everything works well unless, you enter

https://server.com/test/folder without the leading slash

When you do that the host server redirects to https://server.com/project/folder/ which is not an assigned folder on the host domain.

But if you enter it properly: https://server.com/test/folder/ it works properly resolving to the ip server and getting the result.

So the server with nginx tries to find the folder and does the redirect.

Yet, I do not want it to redirect using the server's relative path, i want it to have the load balanced host path like this: https://server.com/test/folder should redirect to https://server.com/test/folder/ not https://server.com/project/folder/

Here is my nginx config file:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /home/.www;

    # Add index.php to the list if you are using PHP
    index index.php index.html;

    server_name _;

    # autoindex on;

    # serve static files directly
    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        #expires max;
    }

    error_log /home/.log/xerror.log;

    access_log /home/.log/xaccess.log compression;

    # include project
    location ^~ /project {
      alias /home/.www/project/public;
      try_files $uri $uri/  @project;

      location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_read_timeout 180;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
      }
    }

    location @project {
        rewrite /project/(.*)$ /project/index.php last;
    }



    location / {

        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php?$args;

    }

    # pass PHP scripts to FastCGI server
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_read_timeout 180;
        # With php-cgi (or other tcp sockets):
        # fastcgi_pass 127.0.0.1:9000;
    }
} 

How to make the server not redirect like that upon finding the folder?

Edit 1 @Ivan Shatsky

asked to get curl -v server.com/test/folder output from the requset, here it is:

*   Trying 35.***.160.34:443...
* TCP_NODELAY set
* Connected to server.com (35.***.160.34) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=server.com
*  start date: Apr 25 19:19:01 2022 GMT
*  expire date: Jul 24 19:19:00 2022 GMT
*  subjectAltName: host "server.com" matched cert's "server.com"
*  issuer: C=US; O=Google Trust Services LLC; CN=GTS CA 1D4
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x556617a0f2f0)
> GET /test/quasar HTTP/2
> Host: server.com
> user-agent: curl/7.68.0
> accept: */*
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!
< HTTP/2 301
< server: nginx/1.20.2
< date: Mon, 23 May 2022 21:04:50 GMT
< content-type: text/html
< content-length: 169
< location: http://server.com/project/folder/
< via: 1.1 google
< alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
<
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.2</center>
</body>
</html>
* Connection #0 to host server.com left intact

So it looks like it does get to my nginx server and it returns 301 redirect but under the folder that it has internally /project/folder/ folder which is correct for that server, but becomes incorrect once it propagates to the host server which should be server.com/test/folder/ and not server.com/project/folder/.

Ivan Shatsky avatar
gr flag
I don't see anything in your config that can produce that one kind of redirect. Can you run `curl -v https://server.com/test/folder` and add the output to your question?
yt flag
@IvanShatsky, just added the `curl -v` result
Ivan Shatsky avatar
gr flag
I'm completely lost. First, you complain that `https://server.com/test/folder` redirects to `https://server.com/project/folder/`. Next, you show the request where `https://server.com/project/folder` is being redirected to `https://server.com/project/folder/` (which is an expected behavior if that `/home/.www/project/folder` is an existing directory on your local filesystem. What is your question actually is?
yt flag
@IvanShatsky Sorry I edited the edit, i did request to `https://server.com/test/folder` and it redirects to `https://server.com/project/folder/`. I did not make mistake in my curl request, just when i typed the answer to you.
yt flag
@IvanShatsky, any ideas would be appreciated, thank you
Richard Smith avatar
jp flag
The simplest solution us to use `test` instead of `project` in your Nginx configuration, so that gcloud points `/test` to `/test`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.